Solved

SQL grouping question

Posted on 2009-07-01
5
214 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
  • 2
  • 2
5 Comments
 
LVL 92

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 92

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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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