Solved

# Help with a Group By Query

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

Thank you in advance!

-vn
0
Question by:Victor Nares
• 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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differeâ€¦
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
###### Suggested Courses
Course of the Month15 days, 2 hours left to enroll

#### 597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.