Link to home
Start Free TrialLog in
Avatar of Victor Nares
Victor NaresFlag for United States of America

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
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

SELECT invNO, MAX(CASE WHEN Dflagcount = 1 THEN rowID END) AS rowID
FROM table_name
GROUP BY invNO
HAVING SUM(CASE WHEN Dflagcount = 1 THEN 1 ELSE 0 END) = 1
Avatar of Victor Nares

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 ()
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.
Excellent explanation.

This worked great!
This works great!
Thanks again.