?
Solved

Help with a Group By Query

Posted on 2016-10-21
5
Medium Priority
?
44 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
[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
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 69

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 69

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

762 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