我正在使用 T-sql。我需要一些关于我的 SQL 的帮助。我正在使用 IN 语句,例如,我查找 1,32,21 现在某些记录将只有两个数字是否可以以某种方式返回,如果它在列表中,则为“是”和“否” ' 如果不是,那么如果是,则数数。
更新!!
id firstName uid value name amount typeofthing
161 sture 10470 1 Engineer Officer Class 1 1 certificate
444 kumba 10472 1 Engineer Officer Class 1 3 certificate
这是我的数据!如您所见,UID 10470
的用户仅持有 1 个证书。我想要实现的是像这样对我的数据有一个新的行
id firstName uid value name amount typeofthing HasorNot
161 sture 10470 1 Engineer Officer Class 1 1 certificate YES
161 sture 10470 32 Engineer Officer Class 2 1 certificate NO
161 sture 10470 21 Engineer Officer Class 3 1 certificate NO
并且该数量是根据每种类型的 YES:es 数量计算的。我想让这个得到他们没有的课程/证书的名称?我使用的 SQL 如下。谢谢
select
tuc.id, tu.firstName, tuc.uid, tuc.value, tc.name,
count(tuc.value) over (PARTITION BY tuc.uid) as 'amount', 'certificate' as 'typeofthing'
from
t_user_certificates tuc, t_certificates tc, t_users tu
where
tuc.[value] IN (1,32,21)
AND tuc.value = tc.id
AND tu.id = tuc.uid
union
select
tuc.id, tu.firstName, tuc.uid, tuc.value, tc.name,
count(tuc.value) over (PARTITION BY tuc.uid) as 'amount',
'courses' as 'typeofthing'
from
t_user_courses tuc, t_courses tc, t_users tu
where
tuc.[value] IN(2,16,21)
AND tuc.value = tc.id
AND tu.id = tuc.uid