Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have the following table structure.

ingredientid nvalue
1 9
1 0
1 5
2 0
2 0
3 4
3 5
3 6
4 0
4 0

I have the above table structure. Now I want to display the ingredient s only when all the n value are zero. For example in my above table when I write a query I should get the output as

INGREDIENTID
2
4

since 2 and 4 all the values are zero. How can I perform this operation?
Posted
Updated 14-Jan-14 12:47pm
v4
Comments
Please don't use Capital letters. Use proper formatting, otherwise it will be considered shouting on the internet, and rude.

If nvalue can't be negative, you can use this simple query:
SQL
SELECT  ingredientid
FROM    MyTable
GROUP BY ingredientid
HAVING SUM(Abs(nvalue)) = 0

Otherwise I'd recommend Maciejs solution
<Edit>Updated to reflect CGs suggestion</edit>
 
Share this answer
 
v2
Comments
Christian Graus 14-Jan-14 16:31pm    
You could use ABS to get rid of the negative issue. Then I think this is the nicest way to do it.
Jörgen Andersson 14-Jan-14 23:48pm    
Didn't think about that. :doh:
select distinct INGREDIENTID from [TableName] where nvalue = (select distinct nvalue from [TableName] where nvalue ! = 1 or nvalue ! = 2 or nvalue ! = 3 or nvalue ! = 4 or nvalue ! = 5 or nvalue ! = 6 or nvalue ! = 7 or nvalue ! = 8 or nvalue ! = 9)
 
Share this answer
 
v4
Comments
kparun86 14-Jan-14 2:28am    
HI CENARJUN. I KNOW TAT WAY. BUT I DONT NEED ALL THE INGREDIENTS WHICH HAVE N VALUE AS ZERO. WAT I NEED IS FOR INGREDIENT 2 AND 4 ALL THE VALUES ARE ZERO. I NEED OLY THAT PARTICULAR INGREDIENT ID WHERE ALL THE N VALUE ARE ZERO. IF I WRITE THE ABOVE QUERY I LL EVEN GET 1 AND 3 ALSO IN TAT LIST.
Cenarjun 14-Jan-14 2:32am    
Check out the updated soln..that ll work..dnt forget to rate
CHill60 14-Jan-14 18:45pm    
Seriously ... turn your caps lock OFF. CAPITALS are considered shouting on the internet = rude
phil.o 14-Jan-14 18:59pm    
Gently put your caps lock key on the floor, and hands up!
kparun86 14-Jan-14 2:34am    
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I'M GETTING TAT ERROR.
Have a look at example:
SQL
DECLARE @tmp TABLE (ingredientid INT, nvalue INT)

INSERT INTO @tmp (ingredientid, nvalue)
VALUES (1, 9), (1, 0),
(1, 5), (2, 0),
(2, 0), (3, 4),
(3, 5), (3, 6),
(4, 0), (4, 0)

SELECT DISTINCT ingredientid
FROM @tmp
WHERE nvalue = 0

returns:
1
2
4


SQL
SELECT T.ingredientid
FROM (
    SELECT ingredientid, COUNT(ingredientid) AS CountOfIid
    FROM @tmp
    GROUP BY ingredientid
    ) AS T INNER JOIN @tmp AS D ON T.ingredientid = D.ingredientid
WHERE D.nvalue = 0
GROUP BY T.ingredientid, T.CountOfIid
HAVING COUNT(T.ingredientid) = T.CountOfIid

returns:
2
4
 
Share this answer
 
Try this..


SQL
select distinct * from @table where ingredientid not in (
select distinct ingredientid   from @table group by ingredientid ,nvalue
 having SUM( nvalue ) > 0 )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900