Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
cardnal0602
Starting Member
11 Posts |
Posted - 2012-09-14 : 10:51:36
|
HeyI have a table that has invoice and batch columns, and our old system sometimes shoots our the same invoices into multiple batches. (which is bad because we could end up double-charging our customers).So the data would simply be:InvoiceID BatchID---------- --------12345 4444422345 5555533345 6666644445 7777712345 88888So using the data above, the result of the query I need to write will output the following:InvoiceID BatchID---------- ---------12345 4444412345 88888I have tried using "having count(batchID) > 1" in my where clause of my test queries, but it's not quite right.Can you help?Thanks! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-14 : 10:56:32
|
[code]SELECT InvoiceID,BatchIDFROM(SELECT COUNT(BatchID) OVER (PARTITION BY InvoiceID) AS Occ,*FROM table)tWHERE Occ > 1 orSELECT t.InvoiceID,t.BatchIDFROM Table tINNER JOIN (SELECT InvoiceID FROM Table GROUP BY InvoiceID HAVING COUNT(BatchID) >1 )t1ON t1.InvoiceID = t.InvoiceID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
cardnal0602
Starting Member
11 Posts |
Posted - 2012-09-14 : 10:59:35
|
Thanks! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-14 : 11:01:05
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
cardnal0602
Starting Member
11 Posts |
Posted - 2012-09-14 : 11:44:19
|
Thanks for your help. I think I left out one particular part about the batch numbers. I am trying to find all invoice numbers(invoiceID) that are found in 'different' batches(batchID).Is there a way to modify your query to only return results where the a particular invoiceID is found in more than one batchID?Your queries are very close, they just end up pulling all invoiceIDs and batchID's without weeding out the good ones.So if I had an InvoiceID of 12345, I would like it to show me how many different batchID's it's in.But the trick is to only show me data where that invoiceID is in more than one batchID.Thanks again for your help. You have gotten me way closer than I was! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-14 : 16:34:59
|
[code]SELECT t.InvoiceID,t.BatchIDFROM Table tINNER JOIN (SELECT InvoiceID FROM Table GROUP BY InvoiceID HAVING COUNT(DISTINCT BatchID) >1 )t1ON t1.InvoiceID = t.InvoiceID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|