我努力讓我的SQL查詢工作。當我的IN FAILS找到一行時,我想返回一行,就像圖片中的行一樣,但是我想將ifuserholdscertificate設置爲「NO」。T-sql case返回錯誤值
SELECT tuc.id,
tu.firstName,
tuc.uid,
tuc.value,
tc.name,
count(tuc.value) over (PARTITION BY tuc.uid) AS 'amount',
'certificate DESIRABLE' AS 'typeofthing' ,
'YES' AS 'HasorNot',
ifuserholdscertificate = CASE
WHEN count(tuc.value) = 0 THEN 'NO'
ELSE 'YES'
END
FROM t_user_certificates tuc,
t_certificates tc,
t_users tu
WHERE tuc.value IN (4,
12,
31)
AND tuc.value = tc.id
AND tu.id = tuc.uid
GROUP BY tuc.id,
tu.firstName,
tuc.uid,
tuc.value,
tc.name
這是查詢生成的數據!
正如你可以看到,即使有些人只有在金額行中得到2,它仍然不會獲取一行並將ifuserholdscertificate設置爲'NO'。
根據要求更新!
select tuc.id,
count(tuc.value) as 'counten',
tu.firstName,
tuc.uid,
tuc.value,
tc.name,
count(tuc.value) over (PARTITION BY tuc.uid) as 'amount',
'certificateDESIRABLE' as 'typeofthing' ,
'YES' as 'HasorNot',
HasOrders = CASE
WHEN count(tuc.value) = 0 THEN 'NO'
ELSE 'YES'
END
from t_user_certificates tuc
left outer join t_certificates tc
on tuc.value = tc.id
left outer join t_users tu
on tu.id = tuc.uid
GROUP BY tuc.id, tu.firstName, tuc.uid, tuc.value, tc.name
Alwyas一個1伯爵始終 'YES'
添加圖片最初沒有顯示 – 2013-03-18 21:54:25
您的查詢可能不太好...你可以用正常的連接子句連接表......如果你顯示所有三個表和你想要的結果,會更好。 – Justin 2013-03-18 22:02:00
downvote寫一個壞的查詢?這是苛刻的..所以有一個問題可以賺你一票。 – 2013-03-18 22:08:18