views:

98

answers:

5

I have two tables, one for invoices and one for incoming payments. An incoming payment can be joined to an invoice by a foreign key like so:

from invoices t1 inner join incoming_payments t2 on t1.receiptnum = t2.docnum

The question: I want to return all invoices which have more than one payment posted against them. For each invoice I want to return its docnum, which is just a unique id for the invoice.

Here are some things I tried which did not work:

select t0.docnum
from invoices t0 inner join incoming_payments t1 on t0.receiptnum = t1.docentry 
group by t0.docnum
having count(t0.docnum) > 1

and

select t0.docnum
from invoices t0 inner join incoming_payments t1 on t0.receiptnum = t1.docentry
group by t0.receiptnum, t0.docnum
having count(t0.receiptnum) > 1

Any ideas?

+1  A: 

I'm not sure why the first one didn't work... It really should've. Can you expand on "it didn't work"?

Try:

select t0.docnum, *
from invoices t0 
  inner join incoming_payments t1 on t0.receiptnum = t1.docentry

And

select t0.docnum, count(*)
from invoices t0 
  inner join incoming_payments t1 on t0.receiptnum = t1.docentry 
group by t0.docnum

to help you figure out what's going on.

Rob

Rob Farley
That was quick, thank you. The second query demonstrates that there are no rows with count(*) > 1 as I had posed the query. Again, this will be a helpful data point. I need to dig deeper into the schema since I know that there are invoices with more than one incoming payment posted against them.
m7d
Sure. You solve that, and your first query should work. Don't group by receiptnum, that'll never work.
Rob Farley
+1  A: 
select t0.docnum, count(*)
from invoices t0 inner join incoming_payments t1 on t0.receiptnum = t1.docentry 
group by t0.docnum
having count(*) > 1

RC
Thanks, this one returns no rows. So that is another data point I can use to debug this.
m7d
You don't need to return COUNT(*) to be able to filter on it.
Rob Farley
Yea, I just included it so he would be able to verify his results.
RC
+1  A: 

What SQL error do you get? Did you group by all fields that were required to be grouped?

Johnny Lamho
No errors, just not getting any invoices returned and I have evidence that invoices which have more than one incoming payment posted against them exist. I might expand my question with another table that could possibly be involved.
m7d
A: 

I think your example SQL is inconsistent (when compared with the 1st SQL you provided).
i.e. in 1st SQL you are using docnum for comparison and in subsequent SQL, you are using docentry.

select t0.docnum, count(t1.*)
from invoices t0 inner join incoming_payments t1 on t0.receiptnum = t1.docentry
group by t0.docnum
having count(t1.*) > 1

EDIT: What is the matching field for comparison?
t0.receiptnum = t1.docnum OR t0.receiptnum = t1.docentry?

EDIT2: See the modified SQL above.

shahkalpesh
Yup - which is why I wanted him to start with a "select *". If the join's wrong, it'll be very obvious.
Rob Farley
Thanks for the heads up. That was a typing error on my part. The join column is docentry. docnum is just another column in invoices.
m7d
A: 

The answer ended up being much more complex given the schema I was working with. I am not including the actual code here, but I solved the problem a while ago. Just wanted to follow up and thank everyone for helping.

m7d