Solved

SQL grouping question

Posted on 2009-07-01
5
186 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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

16 Experts available now in Live!

Get 1:1 Help Now