linq到SQL在内部连接中选择[英] linq to sql select inside an inner join

本文是小编为大家收集整理的关于linq到SQL在内部连接中选择的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是linq到SQL的初学者,我想知道在内部连接中选择的语法是什么:

 inner join ( select CCL_TMA_ID as SecurityIdMax ,
                                max(CCL_DATE) as DateMax
                         from   dbo.usrCOURSCLOTURE
                         where  CCL_DONNEE is not null
                                and CCL_DATE <= @d
                         group by CCL_TMA_ID
                       )

完成查询:

 declare @d datetime
 select @d = getdate()

 select t0.CCL_TMA_ID as SecurityId ,
        t0.CCL_DATE as Date ,
        t0.CCL_DONNEE as Price ,
        t1.CCL_DONNEE as CurrencyPrice
 from   dbo.usrCOURSCLOTURE as t0
        inner join dbo.usrCOURSCLOTURE as t1 on t0.CCL_DEV_DONNEE = t1.CCL_TMA_ID
                                                and t0.CCL_DATE = t1.CCL_DATE
                                                and t1.CCL_DONNEE is not null

        inner join ( select CCL_TMA_ID as SecurityIdMax ,
                            max(CCL_DATE) as DateMax
                     from   dbo.usrCOURSCLOTURE
                     where  CCL_DONNEE is not null
                            and CCL_DATE <= @d
                     group by CCL_TMA_ID
                   ) cMax on t0.CCL_TMA_ID = SecurityIdMax
                             and t0.CCL_DATE <= DateMax
                             and t0.CCL_DATE >= DateMax-10
where t0.CCL_DATE > dateadd(year,-1,@d)

推荐答案

我在下面为您做了一个查询,并提供了一些评论来解释一些功能. 请注意,您不能基于<=进行多重条件加入 例如

on t0.CCL_TMA_ID = SecurityIdMax
                             and t0.CCL_DATE <= DateMax
                             and t0.CCL_DATE >= DateMax-10

您必须在第一个条件下加入,然后用后面的Where

过滤出来

例如

Datetime d = Datetime.Now;
Datetime lastYear = d.AddYears(-1);
var q = from t0 in db.usrCOURSCLOTURE
        join t1 in db.usrCOURSCLOTURE.where(z => z.CCL_DONNEE.HasValue) 
        on new {a = t0.CCL_DEV_DONNEE, b = t0.CCL_DATE} equals new {a = t1.CCL_TMA_ID, b = t1.CCL_DATE}
            // the above is how to do a join on multiple conditions
        join t2 in (
            from x0 in db.usrCOURSCLOTURE.where(z => z.CCL_DONNEE.HasValue && z.CCL_DATE < d)
            .GroupBy(z => z.CCL_TMA_ID)
            select new {SecurityIdMax = x0.Key, DateMax = x0.Max(z => z.CCL_DATE)}
            //this is how you get your groupby subquery
        )
        on t0.CCL_TMA_ID equals t2.SecurityIdMax
        where
          t0.CCL_DATE  > lastYear
          && t0.CCL_DATE <= t2.DateMax
          && t0.CCL_DATE >= SqlFunctions.DateAdd("DAY", -10, t2.DateMax) //nb not sure on the interval - correct this!
        select new {SecurityId = t0.CCL_TMA_ID,
                    Date = t0.CCL_DATE,
                    Price = t0.CCL_DONNEE,
                    CurrencyPrice = t1.CCL_DONNEE};

还要注意," sqlfunctions"类在命名空间system.data.objects.sqlclient中.

其他推荐答案

通过将语句汇总到括号中,您可以创建一个数据子集,在您的情况下,该子集将从dbo.usrCOURSCLOTURE CCL_TMA_ID列分组.

分组.

使它更清楚,让我们以不同的方式说:

   @subsetOfData = select CCL_TMA_ID as SecurityIdMax, max(CCL_DATE) as DateMax
                      from   dbo.usrCOURSCLOTURE
                     where  CCL_DONNEE is not null and CCL_DATE <= @d
                  group by CCL_TMA_ID

,然后

select t0.CCL_TMA_ID as SecurityId ,
        t0.CCL_DATE as Date ,
        t0.CCL_DONNEE as Price ,
        t1.CCL_DONNEE as CurrencyPrice
 from   dbo.usrCOURSCLOTURE as t0
        inner join dbo.usrCOURSCLOTURE as t1 on t0.CCL_DEV_DONNEE = t1.CCL_TMA_ID
                                                and t0.CCL_DATE = t1.CCL_DATE
                                                and t1.CCL_DONNEE is not null

        inner join @subsetOfData as cMax on t0.CCL_TMA_ID = SecurityIdMax
                             and t0.CCL_DATE <= DateMax
                             and t0.CCL_DATE >= DateMax-10
where t0.CCL_DATE > dateadd(year,-1,@d)

本文地址:https://www.itbaoku.cn/post/1557095.html

问题描述

I am a beginner in linq to sql, I am wondering what is the syntax in for a select inside a inner join :

 inner join ( select CCL_TMA_ID as SecurityIdMax ,
                                max(CCL_DATE) as DateMax
                         from   dbo.usrCOURSCLOTURE
                         where  CCL_DONNEE is not null
                                and CCL_DATE <= @d
                         group by CCL_TMA_ID
                       )

complete query :

 declare @d datetime
 select @d = getdate()

 select t0.CCL_TMA_ID as SecurityId ,
        t0.CCL_DATE as Date ,
        t0.CCL_DONNEE as Price ,
        t1.CCL_DONNEE as CurrencyPrice
 from   dbo.usrCOURSCLOTURE as t0
        inner join dbo.usrCOURSCLOTURE as t1 on t0.CCL_DEV_DONNEE = t1.CCL_TMA_ID
                                                and t0.CCL_DATE = t1.CCL_DATE
                                                and t1.CCL_DONNEE is not null

        inner join ( select CCL_TMA_ID as SecurityIdMax ,
                            max(CCL_DATE) as DateMax
                     from   dbo.usrCOURSCLOTURE
                     where  CCL_DONNEE is not null
                            and CCL_DATE <= @d
                     group by CCL_TMA_ID
                   ) cMax on t0.CCL_TMA_ID = SecurityIdMax
                             and t0.CCL_DATE <= DateMax
                             and t0.CCL_DATE >= DateMax-10
where t0.CCL_DATE > dateadd(year,-1,@d)

推荐答案

I've done a query for you below with a few comments explaining some of the features. Note that you can't do a multiple condition join based on <= such as

on t0.CCL_TMA_ID = SecurityIdMax
                             and t0.CCL_DATE <= DateMax
                             and t0.CCL_DATE >= DateMax-10

you'd have to join on the first condition and then filter them out with a where afterwards

eg

Datetime d = Datetime.Now;
Datetime lastYear = d.AddYears(-1);
var q = from t0 in db.usrCOURSCLOTURE
        join t1 in db.usrCOURSCLOTURE.where(z => z.CCL_DONNEE.HasValue) 
        on new {a = t0.CCL_DEV_DONNEE, b = t0.CCL_DATE} equals new {a = t1.CCL_TMA_ID, b = t1.CCL_DATE}
            // the above is how to do a join on multiple conditions
        join t2 in (
            from x0 in db.usrCOURSCLOTURE.where(z => z.CCL_DONNEE.HasValue && z.CCL_DATE < d)
            .GroupBy(z => z.CCL_TMA_ID)
            select new {SecurityIdMax = x0.Key, DateMax = x0.Max(z => z.CCL_DATE)}
            //this is how you get your groupby subquery
        )
        on t0.CCL_TMA_ID equals t2.SecurityIdMax
        where
          t0.CCL_DATE  > lastYear
          && t0.CCL_DATE <= t2.DateMax
          && t0.CCL_DATE >= SqlFunctions.DateAdd("DAY", -10, t2.DateMax) //nb not sure on the interval - correct this!
        select new {SecurityId = t0.CCL_TMA_ID,
                    Date = t0.CCL_DATE,
                    Price = t0.CCL_DONNEE,
                    CurrencyPrice = t1.CCL_DONNEE};

Also note that the "SqlFunctions" class is in the namespace System.Data.Objects.SqlClient in the System.Data.Entity assembly.

其他推荐答案

By enclosing statement into brackets you create a subset of data which in your case will be selection from dbo.usrCOURSCLOTURE grouped by CCL_TMA_ID column.

To make it clearer lets put it in different way:

   @subsetOfData = select CCL_TMA_ID as SecurityIdMax, max(CCL_DATE) as DateMax
                      from   dbo.usrCOURSCLOTURE
                     where  CCL_DONNEE is not null and CCL_DATE <= @d
                  group by CCL_TMA_ID

and then

select t0.CCL_TMA_ID as SecurityId ,
        t0.CCL_DATE as Date ,
        t0.CCL_DONNEE as Price ,
        t1.CCL_DONNEE as CurrencyPrice
 from   dbo.usrCOURSCLOTURE as t0
        inner join dbo.usrCOURSCLOTURE as t1 on t0.CCL_DEV_DONNEE = t1.CCL_TMA_ID
                                                and t0.CCL_DATE = t1.CCL_DATE
                                                and t1.CCL_DONNEE is not null

        inner join @subsetOfData as cMax on t0.CCL_TMA_ID = SecurityIdMax
                             and t0.CCL_DATE <= DateMax
                             and t0.CCL_DATE >= DateMax-10
where t0.CCL_DATE > dateadd(year,-1,@d)