Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Help with a Group By Query

Posted on 2016-10-21
Medium Priority
47 Views
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.

-vn
0
Question by:Victor Nares
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 70

Expert Comment

ID: 41854588
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
0

Author Comment

ID: 41854640
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 ()
0

LVL 70

Accepted Solution

Scott Pletcher earned 2000 total points
ID: 41854648
That was based on my understanding of your requirements for the query:
I thought you only wanted to return invNO's that had exactly 1 Dflagcount value of 1: if there were no rows or 2+ rows for that invNO with the Dflagcount = 1 then don't list it.

Thus, what this code does:
SUM(CASE WHEN Dflagcount = 1 THEN 1 ELSE 0 END) = 1
is test the value of Dflagcount for 1: if that's true, it adds 1 to a counter, else it adds 0.  After all the rows are counted that way, it checks the counter, and if the counter is 1 (meaning exactly 1 matching row was found for that invNO throughout the table), then the invNO group is selected, otherwise it's not.

Because of that HAVING check, we know there is only one row with the Dflagcount = 1.  Therefore, this code:
MAX(CASE WHEN Dflagcount = 1 THEN rowID END) AS rowID
will find exactly one rowID and pull its value too, i.e., the rowID that corresponds to the row with Dflagcount = 1.
0

Author Comment

ID: 41854652
Perfect.
Excellent explanation.

This worked great!
0

Author Closing Comment

ID: 41857221
This works great!
Thanks again.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backupâ€¦
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
###### Suggested Courses
Course of the Month10 days, 3 hours left to enroll