Solved

Help with a Group By Query

Posted on 2016-10-21
5
31 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 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
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
Comment Utility
Perfect.
Excellent explanation.

This worked great!
0
 

Author Closing Comment

by:Victor Nares
Comment Utility
This works great!
Thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
query help 18 50
Need Counts 11 40
MS Dynamics AX 2009 2012 Database/Report Optimization 3 26
Distinct values from two tables 14 16
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now