简体   繁体   English

带有左联接,何处子句和Sum()的SQL语句?

[英]SQL Statement with Left Joins, Where Clause, and a Sum()?

I'm really struggling with the below SQL. 我真的很努力与下面的SQL。 I have tried so many different things, and I can't get any of them to work. 我尝试了很多不同的东西,但我无法使它们工作。

What I basically need to do is LEFT OUTER JOIN this working SQL statement: 我基本上需要做的是在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"

with this one: 与此:

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"

But the other joins are causing me a great deal of confusion/frustration when trying to implement solutions I've found online. 但是,在尝试实施我在网上找到的解决方案时,其他联接给我造成了很多困惑/沮丧。 I've read that it may be the WHERE and GROUP BY statements not playing nice?? 我读过它可能是WHERE和GROUP BY语句玩的不好?

But I can't seem to get any solution I find online to work. 但是我似乎无法获得网上找到的任何解决方案。 One solution I tried looked like it just wrapped the SQL statement with the SUM() and GROUP BY (my bottom statement) inside brackets and joined that? 我尝试过的一种解决方案看起来像是将SQL语句用SUM()和GROUP BY(我的最下面的语句)包装在方括号中并加入了吗?

Is anyone out there able to help? 有没有人可以提供帮助?

Thanks for your time. 谢谢你的时间。

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

You can also do it this way: 您也可以这样操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM