簡體   English   中英

linq to sql在內部聯接中選擇

[英]linq to sql select inside an inner join

我是linq to sql的初學者,我想知道內部連接內的select的語法是什么:

 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.Entity程序集中的命名空間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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM