Dark Dark - 5 months ago 7
SQL Question

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

I'm really struggling with the below 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:

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??

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?

Is anyone out there able to help?

Thanks for your time.

Answer
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