![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
What are you trying to do? Just a SELECT statement or an UPDATE? If UPDATE then which column to update and what expression is the source? It is always best to provide CREATE TABLE statements, INSERT statements for sample data, desired results, and the required logic. -- Plamen Ratchevhttp://www.SQLStudio.com |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
I am not sure if you just need to insert the total. Then this will do: INSERT INTO PendingDiary (doc, totovrs, totpnds, totovr, totpnd, weekdat, dowrdat) SELECT doc, totovrs, totpnds, totovr, SUM(totpnd), weekdat, dowrdat FROM PendingDiary GROUP BY doc, totovrs, totpnds, totovr, weekdat, dowrdat; If you need to change your query, then try this: INSERT INTO PendingDiary (doc, totovrs, totpnds, totovr, totpnd, weekdat, dowrdat) SELECT B.doc, * * * *COALESCE(totos, 0), * * * *COALESCE(totps, 0), * * * *COALESCE(toto, 0), * * * *COALESCE(totp, 0), * * * *SUM(totpnd), * * * *B.weekdat, * * * *B.dowrdat FROM ( SELECT doc, COUNT(doc) AS totos, '0' AS toto FROM #temppend WHERE clear = 'O' * AND SDW = 'Y' GROUP BY doc ) AS A RIGHT JOIN ( SELECT doc, COUNT(doc) AS totps, '0' AS totp, weekdat, dowrdat FROM #temppend WHERE SDW = 'Y' GROUP BY doc, weekdat, dowrdat ) AS B * *ON B.doc = A.doc GROUP BY B.doc, * * * * *COALESCE(totos, 0), * * * * *COALESCE(totps, 0), * * * * *COALESCE(toto, 0), * * * * *COALESCE(totp, 0), * * * * *B.weekdat, * * * * *B.dowrdat; -- Plamen Ratchevhttp://www.SQLStudio.com |
#6
| |||
| |||
|
|
On May 18, 4:54*pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote: I am not sure if you just need to insert the total. Then this will do: INSERT INTO PendingDiary (doc, totovrs, totpnds, totovr, totpnd, weekdat, dowrdat) SELECT doc, totovrs, totpnds, totovr, SUM(totpnd), weekdat, dowrdat FROM PendingDiary GROUP BY doc, totovrs, totpnds, totovr, weekdat, dowrdat; If you need to change your query, then try this: INSERT INTO PendingDiary (doc, totovrs, totpnds, totovr, totpnd, weekdat, dowrdat) SELECT B.doc, * * * *COALESCE(totos, 0), * * * *COALESCE(totps, 0), * * * *COALESCE(toto, 0), * * * *COALESCE(totp, 0), * * * *SUM(totpnd), * * * *B.weekdat, * * * *B.dowrdat FROM ( SELECT doc, COUNT(doc) AS totos, '0' AS toto FROM #temppend WHERE clear = 'O' * AND SDW = 'Y' GROUP BY doc ) AS A RIGHT JOIN ( SELECT doc, COUNT(doc) AS totps, '0' AS totp, weekdat, dowrdat FROM #temppend WHERE SDW = 'Y' GROUP BY doc, weekdat, dowrdat ) AS B * *ON B.doc = A.doc GROUP BY B.doc, * * * * *COALESCE(totos, 0), * * * * *COALESCE(totps, 0), * * * * *COALESCE(toto, 0), * * * * *COALESCE(totp, 0), * * * * *B.weekdat, * * * * *B.dowrdat; -- Plamen Ratchevhttp://www.SQLStudio.com Thank you I will try this in the morning. *I do need to change the query- Hide quoted text - - Show quoted text - |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
The column does not exist in the source tables, this is why you get an error. In the SQL you posted that was an alias from a prior column and then used in the SUM expression. This is something that is not allowed in SQL Server... I suggest to post you CREATE TABLE statements, INSERTS for sample data, and desired results. -- Plamen Ratchevhttp://www.SQLStudio.com |
#9
| |||
| |||
|
|
On May 19, 12:46*pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote: The column does not exist in the source tables, this is why you get an error. In the SQL you posted that was an alias from a prior column and then used in the SUM expression. This is something that is not allowed in SQL Server... I suggest to post you CREATE TABLE statements, INSERTS for sample data, and desired results. -- Plamen Ratchevhttp://www.SQLStudio.com Okay thanks getting everything together will send to you. |
#10
| |||
| |||
|
|
So when I got rid of that it was okay but now I'm getting: Invalid column name 'totpnd'. Insert Error: Column name or number of supplied values does not match table definition. ... It doesn't like the sum column |
|
This is what I have: INSERT INTO PendingDiary SELECT B.doc, COALESCE(totos, 0), COALESCE(totps, 0), COALESCE(toto, 0), COALESCE(totp, 0), SUM(totpnd), B.weekdat, B.dowrdat FROM ( SELECT doc, COUNT(doc) AS totos, '0' AS toto FROM #temppend WHERE clear = 'O' AND SDW = 'Y' GROUP BY doc ) AS A RIGHT JOIN ( SELECT doc, COUNT(doc) AS totps, '0' AS totp, weekdat, dowrdat FROM #temppend WHERE SDW = 'Y' GROUP BY doc, weekdat, dowrdat ) AS B ON B.doc = A.doc GROUP BY B.doc, COALESCE(totos, 0), COALESCE(totps, 0), COALESCE(toto, 0), COALESCE(totp, 0), B.weekdat, B.dowrdat; |
![]() |
| Thread Tools | |
| Display Modes | |
| |