鉴于以下从SQL Server Management Studio中获取的图像,我想显示该数字出现在' in'中的次数。条款。
这是理想的结果:
internalid | contactmethod | count
---------------------------------------
113 | 0 | 2 -- 113 appears 2 times in the 'in' clause
142 | 0 | 1 -- 142 appears 1 time in the 'in' clause
150 | 4 | 3 -- 150 appears 3 times in the 'in' clause
这是目前的情况:
答案 0 :(得分:3)
使用table valued constructor
和Left Join
SELECT tc.internalid,
b.contactmethod,
Count(tc.internalid)
FROM (VALUES (150),(150),(150),(113),(113),(142)) tc(internalid)
LEFT JOIN Bp_contactmethod(3) b
ON tc.internalid = b.internalid
GROUP BY tc.internalid,
b.contactmethod