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

x
?
Solved

SQL grouping question

Posted on 2009-07-01
5
Medium Priority
?
239 Views
Last Modified: 2012-05-07
I have a table with these fields.  (item_num, location, and value)  

Each item_num might have many entries, each with a different location for the item.

Only one of the item locations should have a value of true (1).  All others should have
a value of false (0).  This lets me know the primary location for the item.  All the other
locations for that item will have a 0 and be secondary locations.

What I want to know is which item_num (item numbers) have all location values of 0 and none with 1.

So:

Show me all item_num where there is no 1 value set for any of the locations.

Can someone tell me the SQL statement for this please?  
0
Comment
Question by:jstorm13
[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
  • 2
  • 2
5 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24755927
Assuming that [value] is a bit data type...

SELECT item_num
FROM SomeTable
GROUP BY item_num
WHERE MAX(CAST([value] AS int)) = 0
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24755933
If [value] is already [tiny][small]int, then this simpler construction should work:

SELECT item_num
FROM SomeTable
GROUP BY item_num
WHERE MAX([value]) = 0
0
 

Author Comment

by:jstorm13
ID: 24755975
It complains about putting a Where after a group by.

If I revers the group by and where then it is in proper order however i get the following error.

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause
0
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 24755982
SELECT item_num
FROM SomeTable
GROUP BY item_num
having MAX([value]) = 0
0
 

Author Comment

by:jstorm13
ID: 24756010
That worked ralmada Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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