Sql 选择项目在“in”子句中的次数
下面的图片取自SQLServerManagementStudio,我想显示该数字在“in”子句中出现的次数 这是理想的结果:Sql 选择项目在“in”子句中的次数,sql,sql-server,Sql,Sql Server,下面的图片取自SQLServerManagementStudio,我想显示该数字在“in”子句中出现的次数 这是理想的结果: internalid | contactmethod | count --------------------------------------- 113 | 0 | 2 -- 113 appears 2 times in the 'in' clause 142 | 0
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
这就是目前的情况:
使用表值构造函数和左联接
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