日期:2014-05-18 浏览次数:20377 次
CREATE TABLE 类别表 ( id VARCHAR(10), name VARCHAR(10), value INT ) GO INSERT INTO 类别表 SELECT '001', '类别0', 0 UNION SELECT '002', '类别1', 1 UNION SELECT '003', '类别2', 2 UNION SELECT '004', '类别3', 3 UNION SELECT '005', '类别4', 4 GO CREATE TABLE 数据表 ( id VARCHAR(10), typeValue VARCHAR(100) ) GO INSERT INTO 数据表 SELECT '0001', '2' UNION SELECT '0002', '0' UNION SELECT '0003', '3' UNION SELECT '0004', '0,2' UNION SELECT '0005', '0,1,2' GO SELECT A.ID,name,value,COUNT(1) AS Num FROM 类别表 AS A INNER JOIN 数据表 AS B ON CHARINDEX(',' + CAST(A.value AS VARCHAR(10)) + ',',',' + B.typeValue + ',') > 0 GROUP BY A.ID,name,value ID name value Num 001 类别0 0 3 002 类别1 1 1 003 类别2 2 3 004 类别3 3 1