![](/img/trans.png)
[英]SQL Server : create a column based on the first occurrence of a value in another column
[英]How to occurrence of specific value in a column in SQL Server
我有一張這樣的桌子:
name col2 cnt
B5 2.00000000 62
B6 2.00000000 9
B4 3.00000000 1
B5 3.00000000 75
B6 3.00000000 62
B5 4.00000000 9
B6 4.00000000 11
B5 5.00000000 102
B6 5.00000000 37
我想作為輸出: B4
即所有在那里2.00000000從未出現過的名字col2
。 我怎么做? 我正在使用SQL Server。
您可以使用EXISTS
和相關子查詢來檢查name
2.00000000
存在2.00000000
的值。
SELECT DISTINCT
t1.name
FROM elbat t1
WHERE NOT EXISTS (SELECT *
FROM elbat t2
WHERE t2.name = t1.name
AND t2.col2 = 2.00000000);
(我假設2.00000000
是數字。如果是字符串,請在其2.00000000
加上單引號。)
或者,您可以使用條件聚合來計算2.00000000
的出現。 使用HAVING
僅保留此計數為0
的行。
SELECT t1.name
FROM elbat t1
GROUP BY t1.name
HAVING count(CASE
WHEN t.col1 = 2.00000000 THEN
1
END) = 0
select distinct t1.name
from yourtable t1
where t1.name not in (
select t2.name
from yourtable t2
where t2.col2 = 2.00000000)
如果2.00000000
是最小可能值,則可以執行以下操作:
select name
from t
group by name
where min(col2) > 2.00000000;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.