我有这个
id number stock
1 555 1
2 555 0
3 444 0
当所有值在同一个数字上为0时,我想返回true。 编号555是假的,因为库存有1个。数字3为真,因为所有股票值都为0。
我该怎么做?
答案 0 :(得分:3)
你可以这样做:
select
number,
case
when min(stock) = 0 and max(stock) = 0 then 'true'
else 'false'
end as TrueOrFalse
from TheTable
group by number
查询将返回number
列中的所有不同值以及相应的true / false状态。
这是一个现场演示:http://www.sqlfiddle.com/#!9/61c82/1
答案 1 :(得分:1)
如果要显示所有行的指示符,请尝试此
WITH cte
AS (SELECT Count(CASE WHEN stock = 0 THEN 1 END)OVER(partition BY number) f_count,
Count(1)OVER(partition BY number) AS t_count,*
FROM Yourtable)
SELECT id,
number,
stock,
Indicator=CASE WHEN f_count <> t_count THEN 'False' ELSE 'True' END
FROM cte
答案 2 :(得分:1)
SELECT number,
CASE WHEN COUNT(CASE WHEN stock <> 0 THEN 1 END) = 0
THEN 'true' ELSE 'false' END
FROM dbo.TableName
GROUP BY number
答案 3 :(得分:0)
您可以使用此
select number,stock,
case when sum(stock)=0 then 'true' else 'false' end AS Staus
from #temp group by number,stock