Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择项目在“in”子句中的次数_Sql_Sql Server - Fatal编程技术网

Sql 选择项目在“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

下面的图片取自SQLServerManagementStudio,我想显示该数字在“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
这就是目前的情况:

使用表值构造函数和左联接

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