?
Solved

Help with a Group By Query

Posted on 2016-10-21
5
Medium Priority
?
48 Views
Last Modified: 2016-10-24
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
Comment
Question by:Victor Nares
  • 3
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
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

by:Victor Nares
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

by:
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

by:Victor Nares
ID: 41854652
Perfect.
Excellent explanation.

This worked great!
0
 

Author Closing Comment

by:Victor Nares
ID: 41857221
This works great!
Thanks again.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

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

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

Join & Ask a Question