带有左连接,Where子句和Sum()的SQL语句?

时间:2016-07-07 17:55:47

标签: sql sum left-join where

我真的在努力学习下面的SQL。我尝试了很多不同的东西,但我无法让它们发挥作用。

我基本上需要做的是LEFT OUTER JOIN这个有效的SQL语句:

SELECT   "TABLE1"."Sheet Number",
         "TABLE1"."ID Number",
         "TABLE1"."Identification",
         "TABLE1"."Job Date",
         "TABLE1"."p2c",
         "TABLE2"."Range",
         "TABLE1"."bcr",
         "TABLE1"."Dataset ID",
         "TABLE1"."ACC",
         "TABLE1"."GNC",
         "TABLE1"."Year",
         "TABLE1"."Period",
         "TABLE1"."Week",
         "TABLE1"."Job_ID"
FROM     "DATABASE"."dbo"."TABLE2" "TABLE2" LEFT OUTER JOIN "DATABASE"."dbo"."TABLE1" "TABLE1"
         ON (((("TABLE2"."Contract"="TABLE1"."GNC")
         AND ("TABLE2"."FromPeriod"="TABLE1"."Period"))
         AND ("TABLE2"."FromWeek"="TABLE1"."Week"))
         AND ("TABLE2"."FromYear"="TABLE1"."Year"))
WHERE    "TABLE1"."ACC"='ACCOUNT57' 
         AND "TABLE1"."Dataset ID"=5 
         AND "TABLE1"."bcr"=1 
         AND "TABLE2"."Range"='Week'
ORDER BY "TABLE1"."Sheet Number"

这一个:

SELECT  "SALES"."JobId",
        "SALES"."Total",
        SUM("SALES"."Total") AS JOBTOTAL
FROM   "DATABASE"."dbo"."SALES" "SALES"
GROUP BY "SALES"."JobId"
ON "SALES"."JobId"="TABLE1"."Job_ID"

但在尝试实施我在网上找到的解决方案时,其他联接让我感到非常困惑/沮丧。我已经读过它可能是WHERE和GROUP BY语句不好玩了吗?

但我似乎找不到任何我在网上找到的解决方案。我试过的一个解决方案看起来只是用括号内的SUM()和GROUP BY(我的底部语句)包装了SQL语句并加入了它?

有人能帮忙吗?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

SELECT
    t1.Sheet Number,
    t1.ID Number,
    t1.Identification,
    t1."Job Date",
    t1.p2c,
    t1."Range",
    t1.bcr,
    t1.Dataset ID,
    t1.ACC,
    t1.GNC,
    t1."Year",
    t1.Period,
    t1."Week",
    t1.Job_ID,
    SUM(s.Total) AS JOBTOTAL
FROM    
    dbo.TABLE1 t1
    LEFT JOIN dbo.Sales s
       ON t.Job_Id = s.Job_id
WHERE
    t1.ACC='ACCOUNT57' 
    AND t1.Dataset ID=5 
    AND t1.bcr=1 
    AND EXISTS (SELECT *
             FROM
                dbo.TABLE2 t2
             WHERE
                t1.GNC = t2.Contract
                AND t1.Period = t2.FromPeriod
                AND t1."Week" = t2.FromWeek
                AND t1."Year" = t2.FromYear
                AND t2."Range"='Week')
GROUP BY
    t1.Sheet Number,
    t1.ID Number,
    t1.Identification,
    t1."Job Date",
    t1.p2c,
    t1."Range",
    t1.bcr,
    t1.Dataset ID,
    t1.ACC,
    t1.GNC,
    t1."Year",
    t1.Period,
    t1."Week",
    t1.Job_ID
ORDER BY
    t1.Sheet Number

你也可以这样做:

SELECT DISTINCT
    t1.Sheet Number,
    t1.ID Number,
    t1.Identification,
    t1."Job Date",
    t1.p2c,
    t1."Range",
    t1.bcr,
    t1.Dataset ID,
    t1.ACC,
    t1.GNC,
    t1."Year",
    t1.Period,
    t1."Week",
    t1.Job_ID,
    s.JOBTOTAL
FROM    
    dbo.TABLE1 t1

    INNER JOIN dbo.TABLE2 t2
    ON t1.GNC = t2.Contract
    AND t1.Period = t2.FromPeriod
    AND t1."Week" = t2.FromWeek
    AND t1."Year" = t2.FromYear
    AND t2."Range"='Week'

    LEFT JOIN ( SELECT
                SUM(s.Total) AS JOBTOTAL
             FROM
                dbo.Sales s
             WHERE
                t.Job_Id = s.Job_id) s
WHERE
    t1.ACC='ACCOUNT57' 
    AND t1.Dataset ID=5 
    AND t1.bcr=1 
ORDER BY
    t1.Sheet Number