Victor Nares
asked on
Help with a Group By Query
Thank you all in advance. I suspect this will be an easy one.
we have:
invNO rowID DFlagcount
100 1 1
100 2 null
100 3 1
101 4 Null
101 5 Null
101 6 1
101 7 null
We have 2 invoices. We want to pull back in our result set only the row id that corresponds to the invoice that has a Dflagcount =1.
In this case, invoice 101 RowID 6.
Thank you in advance!
-vn
we have:
invNO rowID DFlagcount
100 1 1
100 2 null
100 3 1
101 4 Null
101 5 Null
101 6 1
101 7 null
We have 2 invoices. We want to pull back in our result set only the row id that corresponds to the invoice that has a Dflagcount =1.
In this case, invoice 101 RowID 6.
Thank you in advance!
-vn
ASKER
That's works great. However, can you talke about:
SUM(CASE WHEN Dflagcount = 1 THEN 1 ELSE 0 END) = 1
I understand the having is used to filter data returned by Group Bys but not clear on the Case logic...especially the =1 outsited the ()
SUM(CASE WHEN Dflagcount = 1 THEN 1 ELSE 0 END) = 1
I understand the having is used to filter data returned by Group Bys but not clear on the Case logic...especially the =1 outsited the ()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect.
Excellent explanation.
This worked great!
Excellent explanation.
This worked great!
ASKER
This works great!
Thanks again.
Thanks again.
FROM table_name
GROUP BY invNO
HAVING SUM(CASE WHEN Dflagcount = 1 THEN 1 ELSE 0 END) = 1