group by逗号分隔的TSQL中的ID列表无法正常工作

时间:2017-05-11 09:53:25

标签: sql sql-server tsql

我正在创建一个存储过程。我有这种格式的数据

InteractiionId(pk)EmployeeId(fk)原因

1                   107           66
2                   107           68
3                   107           66,69
4                   98           71
5                   98           71
6                   98           69,71,68 
7                   90           68 
8                   90           69,68 
9                   90           66,71,68 

我需要为每个员工找到这样的原因

EmployeeID ReasonsCount

  107    66(2)
    107     68(1)
    107     69(1) 
    98     71(3)
    98     68(1)
    98     69(1)
    90     68(3)
    90     69(1)
    90     66(1)
    90     71(1)

我想这样做:

select IdEmployee, E.FirstName,E.LastName,  count(reasons), Reasons FROM Interaction I
 LEFT JOIN Employee E
 ON I.IdEmployee = E.Id


-- where
 --    IdEmployee = 95 OR 
--    IdEmployee = 98 OR
  --  IdEmployee = 107
group by  IdEmployee, E.FirstName,E.LastName, Reasons

但它不起作用。请提出解决方案。

感谢。

3 个答案:

答案 0 :(得分:0)

;with cte
as
(
select * from #temp t
cross apply
SplitStrings_Numbers(c,',') b
)
select b,cast(item as varchar(10))+' ('+ cast(count(item) as varchar(10))+')' as cnt
from
cte
group by b,item

我使用split strings function来分割值

答案 1 :(得分:0)

您可以像这样使用内联拆分字符串

DECLARE @SampleData AS TABLE
(
   InteractiionId int,
   EmployeeId int ,
   Reasons varchar(200)
)

INSERT INTO @SampleData
VALUES
  (1, 107, '66'),
  (2, 107, '68'),
  (3, 107, '66,69'),
  (4, 98 , '71'),
  (5, 98 , '71'),
  (6, 98 , '69,71,68'), 
  (7, 90 , '68'), 
  (8, 90 , '69,68'), 
  (9, 90 , '66,71,68')

;WITH temp AS 
(
   SELECT  *,
         CAST('<X>' + replace(sd.Reasons, ',','</X><X>') +'</X>'  AS XML ) AS XmlValue
   FROM @SampleData sd
)
SELECT  t.EmployeeId, CONCAT(c.[Value], '(', count(*), ')') 
FROM temp t
CROSS APPLY
(
   SELECT t.x.value('.', 'varchar(100)') AS Value
   FROM t.XmlValue.nodes('X') AS t(x)
) c
GROUP BY t.EmployeeId, c.[Value]
ORDER BY t.EmployeeId desc

演示链接:http://rextester.com/EHL21890

答案 2 :(得分:0)

;With cte(InteractiionId, EmployeeId, Reasons)
 AS
(
SELECT 1, 107,  '66'        UNION ALL
SELECT 2, 107,  '68'        UNION ALL
SELECT 3, 107,  '66,69'     UNION ALL
SELECT 4, 98 , '71'         UNION ALL
SELECT 5, 98 , '71'         UNION ALL
SELECT 6, 98 , '69,71,68'   UNION ALL
SELECT 7, 90 , '68'         UNION ALL
SELECT 8, 90 , '69,68'      UNION ALL
SELECT 9, 90 , '66,71,68' 
)

,CteCount AS (
    SELECT a.InteractiionId
        ,a.EmployeeId
        ,Split.a.value('.', 'VARCHAR(100)') AS Reasons
    FROM (
        SELECT InteractiionId
            ,EmployeeId
            ,CAST('<M>' + REPLACE(Reasons, ',', '</M><M>') + '</M>' AS XML) AS Reasons
        FROM cte
        ) AS A
    CROSS APPLY Reasons.nodes('/M') AS Split(a)
    )

    SELECT EmployeeId
    ,CONCAT(Reasons,'(' ,ReasonCounts,')') AS ReasonsCount
     FROM (
    SELECT EmployeeId,Reasons,ReasonCounts FROM 
       (
        SELECT DISTINCT EmployeeId,Reasons
            ,COUNT(Reasons) OVER (PARTITION BY EmployeeId,Reasons ORDER BY EmployeeId
                ) AS ReasonCounts
        FROM CteCount
        ) Dt
    ) Final
ORDER BY Final.EmployeeId DESC

输出

EmployeeId    ReasonsCount
------------------------
107             66(2)
107             68(1)
107             69(1)
98              68(1)
98              69(1)
98              71(3)
90              66(1)
90              68(3)
90              69(1)
90              71(1)