Author |
Topic |
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2007-09-18 : 06:03:09
|
HiI run this query and get:select count(*), ISNULL(i.invoicestatus, ist.[ID]) AS 'InvoiceStatus', ist.[description], ist.[ID] from invoicestatus istleft join invoices i on ist.[ID] = i.InvoiceStatusgroup by ISNULL(invoicestatus, [ID]), ist.[ID], ist.[description]order by ist.[ID]5490 0 No Status 0139 1 Day 21 - £10k Plus - New Call 149 2 Day 35 - Under £10k - New Call 2689 3 Day 45 - Follow Up Call 393 4 Day 45 - Letter One (<£500) + copy invoice 416 5 Day 65 - Final Call + email 1 to PM 525 6 Day 70 - Final Demand 6968 7 Day 77 - email 2 to PM re: legal 71 8 ** NOT IN USE ** Day 80 - email 3 to GB to commence Summary sheet for legal file 81 9 ** NOT IN USE ** Day 87 - email 4 to PM - no response 91 10 ** NOT IN USE ** Day 90 - email 5 to GB to issue claim 103 11 Payment promised 1115 12 Under query 121 13 Legal 131 14 Paid / unpaid exception 141 15 No invoice number exception 151 16 Part promised 161 17 Query resolved (follow up call) 17As soon as I add a where clause, it skips out all the zero counts.How do I get SQL server to display a count of 0. I've tried swapping the joins and nothing seems to work:select count(*), ISNULL(i.invoicestatus, ist.[ID]) AS 'InvoiceStatus', ist.[description], ist.[ID] from invoicestatus istleft join invoices i on ist.[ID] = i.InvoiceStatuswhere paidflag = 0group by ISNULL(invoicestatus, [ID]), ist.[ID], ist.[description]order by ist.[ID]1808 0 No Status 0139 1 Day 21 - £10k Plus - New Call 149 2 Day 35 - Under £10k - New Call 2689 3 Day 45 - Follow Up Call 393 4 Day 45 - Letter One (<£500) + copy invoice 416 5 Day 65 - Final Call + email 1 to PM 525 6 Day 70 - Final Demand 6968 7 Day 77 - email 2 to PM re: legal 73 11 Payment promised 1115 12 Under query 12 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 06:10:08
|
ALWAYS prefix your columns with table name when having more than one table in a query.It makes it easier for you (and us) to debug.I assume PaidFlag is a column in the Invoices table, not InvoiceStatus table?from invoicestatus istleft join invoices i on ist.[ID] = i.InvoiceStatuswhere paidflag = 0from invoicestatus istleft join invoices i on ist.[ID] = i.InvoiceStatusand i.paidflag = 0 E 12°55'05.25"N 56°04'39.16" |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-18 : 06:10:25
|
is [paidflag] in the [invoices] table?If so your WHERE clause is effectively changing the LEFT JOIN into an INNER JOIN.And if that is the case then you could dowhere paidflag = 0 OR ist.[ID] IS NULLwhich will include invoices only where paidflag = 0, or where the [ID] is NULL - and assuming that cannot happen in the data itself (e.g. its a PK / NOT NULL column) then that would only happen if there was no Invoice record - i.e. omitted by the LEFT JOIN.Probably better still would be to move the criteria to the JOIN:FROM invoicestatus AS ist LEFT OUTER JOIN invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.paidflag = 0 Kristen |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 06:16:41
|
One thing to have in mind when dealing with NULL's in the WHERE clause -- Prepare sample dataDECLARE @Table1 TABLE (RowID INT, Value INT)INSERT @Table1SELECT 1, 1 UNION ALLSELECT 2, 3DECLARE @Table2 TABLE (RowID INT, Status INT)INSERT @Table2SELECT 1, 0 UNION ALLSELECT 3, 1-- Kristen's first querySELECT t1.RowID, t1.Value, t2.StatusFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.RowID = t1.RowIDWHERE t2.RowID IS NULL OR t2.Status = 1-- Peso's querySELECT t1.RowID, t1.Value, t2.StatusFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.RowID = t1.RowID AND t2.Status = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2007-09-18 : 06:20:05
|
okay but now another issue:select count(i.invoice_no), ist.[description], ist.[ID] from invoices iright OUTER JOIN invoicestatus AS ist ON i.InvoiceStatus = ist.[ID]and i.paidflag = 0and (i.amendedfollowupdate <= getdate() or i.amendedfollowupdate is NULL)and (i.systemfollowupdate <= getdate() or i.amendedfollowupdate < systemfollowupdate)and ist.[ID] NOT IN (8,9,10)group by ist.[ID], ist.[description]order by ist.[ID]0 No Status 0132 Day 21 - £10k Plus - New Call 149 Day 35 - Under £10k - New Call 2688 Day 45 - Follow Up Call 393 Day 45 - Letter One (<£500) + copy invoice 416 Day 65 - Final Call + email 1 to PM 525 Day 70 - Final Demand 6968 Day 77 - email 2 to PM re: legal 70 ** NOT IN USE ** Day 80 - email 3 to GB to commence Summary sheet for legal file 80 ** NOT IN USE ** Day 87 - email 4 to PM - no response 90 ** NOT IN USE ** Day 90 - email 5 to GB to issue claim 100 Payment promised 113 Under query 120 Legal 130 Paid / unpaid exception 140 No invoice number exception 150 Part promised 160 Query resolved (follow up call) 17The ones with no status should actually be a count of 1808 but it's coming up as 0.Also, it still seems to be including statuses of 8,9, and 10 which I omitted in the join? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 06:23:36
|
Not only have you managed to rewrite query with RIGHT JOIN instead of the original LEFT JOIN, you have also managed to put the additonal wheres on the wrong place!How about you posted some accurate and proper sample data together with your expected output? E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 06:27:07
|
[code]SELECT COUNT(i.Invoice_No), ist.[Description], ist.[ID]FROM InvoiceStatus AS ist LEFT JOIN Invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.PaidFlag = 0 AND (i.AmendedFollowUpdate <= GETDATE() OR i.AmendedFollowUpdate IS NULL) AND (i.SystemFollowUpdate <= GETDATE() OR i.SystemFollowUpdate > i.AmendedFollowUpdate)WHERE ist.[ID] NOT IN (8, 9, 10)GROUP BY ist.[ID], ist.[description]ORDER BY ist.[ID][/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2007-09-18 : 06:42:04
|
Sorry.Am now using:SELECT COUNT(i.Invoice_No), ist.[Description], ist.[ID]FROM InvoiceStatus AS ist LEFT JOIN Invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.PaidFlag = 0 AND (i.AmendedFollowUpdate <= GETDATE() OR i.AmendedFollowUpdate IS NULL) --AND (i.SystemFollowUpdate <= GETDATE() OR i.SystemFollowUpdate > i.AmendedFollowUpdate)WHERE ist.[ID] NOT IN (8, 9, 10)GROUP BY ist.[ID], ist.[description]ORDER BY ist.[ID]In the Invoices table:Today's date is 2007-09-18if SystemFollowupDate is less than that it needs to be countedif AmendedFollowUpDate is less than that it needs to be countedAmendedFollowUpDate takes priority if it is not NULL |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 06:45:25
|
[code]SELECT COUNT(i.Invoice_No), ist.[Description], ist.[ID]FROM InvoiceStatus AS ist LEFT JOIN Invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.PaidFlag = 0 AND (i.AmendedFollowUpdate <= CURRENT_TIMESTAMP OR i.SystemFollowUpdate <= CURRENT_TIMESTAMP)WHERE ist.[ID] NOT IN (8, 9, 10)GROUP BY ist.[ID], ist.[description]ORDER BY ist.[ID][/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2007-09-18 : 07:04:18
|
These are the problem records:SELECT --COUNT(i.Invoice_No), *, ist.[Description], ist.[ID]FROM InvoiceStatus AS ist LEFT JOIN Invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.PaidFlag = 0 AND (i.AmendedFollowUpDate<=getdate() OR i.AmendedFollowUpDate Is Null OR i.SystemFollowUpDate<=getdate() OR i.SystemFollowUpDate Is Null)WHERE ist.[ID] NOT IN (8, 9, 10)GROUP BY ist.[ID], ist.[description]ORDER BY ist.[ID]it is picking up records where the systemfollowupdate is less than today but the amendedfollowupdate is in a few days time.SO I somehow need to make amendedfollowupdate more important in the clauses.? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 07:09:27
|
Then post a truth table matrix of how you want combinations of dates to be handled!Help us help you! It takes a lot of time to guess what you want.Today AmendedFollowUpdate SystemFollowUpdate Is valid?-------- ------------------- ------------------ ---------20070918 20070916 NULL20070918 20070916 2007091520070918 20070916 2007091720070918 20070916 2007091820070918 20070916 2007091920070918 NULL NULL20070918 NULL 2007091720070918 NULL 2007091820070918 NULL 2007091920070918 20070920 NULL20070918 20070920 2007091720070918 20070920 2007091820070918 20070920 2007091920070918 20070920 2007092020070918 20070920 20070921 E 12°55'05.25"N 56°04'39.16" |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2007-09-18 : 07:42:48
|
Honestly, I would have if I could have done the formatting:Today AmendedFollowUpdate SystemFollowUpdate Is valid?-------- ------------------- ------------------ ---------20070918 20070916 NULL yes20070918 20070916 20070917 yes20070918 20070916 20070918 yes20070918 20070916 20070919 yes20070918 NULL NULL only if status = 0 but then all the others would have dates otherwise20070918 NULL 20070917 yes20070918 NULL 20070918 yes20070918 NULL 20070919 no20070918 20070920 NULL no20070918 20070920 20070917 no20070918 20070920 20070918 no20070918 20070920 20070919 no20070918 20070920 20070920 no20070918 20070920 20070921 no |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-18 : 07:46:59
|
quote: Originally posted by qwertyjjj
Today AmendedFollowUpdate SystemFollowUpdate Is valid?-------- ------------------- ------------------ ---------20070918 20070916 NULL yes20070918 20070916 20070917 yes20070918 20070916 20070918 yes20070918 20070916 20070919 yes20070918 NULL NULL only if status = 0 but then all the others would have dates otherwise20070918 NULL 20070917 yes20070918 NULL 20070918 yes20070918 NULL 20070919 no20070918 20070920 NULL no20070918 20070920 20070917 no20070918 20070920 20070918 no20070918 20070920 20070919 no20070918 20070920 20070920 no20070918 20070920 20070921 no
Then this query will do.What and where is Status column?SELECT COUNT(i.Invoice_No), ist.[Description], ist.[ID]FROM InvoiceStatus AS ist LEFT JOIN Invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.PaidFlag = 0 AND (i.AmendedFollowUpdate <= CURRENT_TIMESTAMP OR i.AmendedFollowUpdate IS NULL) AND (i.SystemFollowUpdate <= CURRENT_TIMESTAMP OR i.SystemFollowUpdate IS NULL)WHERE ist.[ID] NOT IN (8, 9, 10)GROUP BY ist.[ID], ist.[description]ORDER BY ist.[ID] E 12°55'05.25"N 56°04'39.16" |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2007-09-18 : 07:47:21
|
i think:SELECT COUNT(i.Invoice_No), ist.[Description], ist.[ID]FROM InvoiceStatus AS ist LEFT JOIN Invoices AS i ON i.InvoiceStatus = ist.[ID] AND i.PaidFlag = 0 AND (i.AmendedFollowUpDate<=getdate() OR (i.AmendedFollowUpDate Is Null AND i.SystemFollowUpDate<=getdate()) OR i.SystemFollowUpDate Is Null)WHERE ist.[ID] NOT IN (8, 9, 10)GROUP BY ist.[Description], ist.[ID]ORDER BY ist.[ID] |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-18 : 08:32:31
|
"I would have if I could have done the formatting"See: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=How%20to%20format%20your%20question%20or%20a%20reply |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-18 : 23:14:06
|
Thanks for spotting that, now fixed. There's that's the last bug fixed ... |
 |
|
|