如果在失败的地方t-sql返回一个值
我正在使用T-sql。我需要一些关于我的SQL的帮助。我正在使用IN语句,作为一个例子,我寻找1,32,21现在一些记录将只有两个数字在那里我可以返回,'是'如果它是在列表中,'是' '如果它不是,那么如果'是'就算数字。如果在失败的地方t-sql返回一个值
更新!
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
这量算的上是量:每种类型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
如果我读你的权利,好像你可能有一些像CASE WHEN count(tuc.value) = 0 THEN 'YES' ELSE 'NO' END
在选择列表
还是不行,看到数据更新之后。我认为这样做:
第1步是使用tu和tuc表之间的外连接。请参阅@ marc_s的评论。
第2步是摆脱输出中的“id”列,或允许它为空。看起来,如果用户没有给定的证书,tuc中将没有行,所以这个列对所有行都没有意义。
第3步是添加一列CASE WHEN tuc.value IS NULL THEN 'NO' ELSE 'YES' END AS 'HasOrNot'
我应该如何把它放到我的SQL? – 2013-03-18 21:34:20
编辑答案... – joelt 2013-03-19 00:18:02
这是不是很清楚你以后是什么。您能否提供一些示例数据和预期结果? – 2013-03-18 02:33:55
[踢坏的习惯:使用旧式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 使用ANSI - ** 92 ** SQL标准(** 20年前**!)废止旧式*逗号分隔的表*样式列表。 ***请停止使用它 – 2013-03-18 05:45:56