SQL计数在列中更改

时间:2015-01-15 17:13:26

标签: sql sql-server tsql

我需要计算故障单上已分配组的更改。问题是我的日志还计算了同一组中受让人的变化。

以下是一些示例数据

 ticket_id | assigned_group  | assignee   | date
----------------------------------------------------
 1001      | group A         | john       | 1-1-15
 1001      | group A         | michael    | 1-2-15
 1001      | group A         | jacob      | 1-3-15
 1001      | group B         | eddie      | 1-4-15
 1002      | group A         | john       | 1-1-15
 1002      | group B         | eddie      | 1-2-15
 1002      | group A         | john       | 1-3-15
 1002      | group B         | eddie      | 1-4-15
 1002      | group A         | john       | 1-5-15

我需要这个回来

ticket_id | count 
--------------------
10001     | 2
10002     | 4

我的查询就像这样

select ticket_id, assigned_group, count(*) from mytable group by ticket_id, assigned_group

但那给了我

ticket_id | count 
--------------------
10001     | 4
10002     | 5

修改

如果我使用

select ticket_id, count(Distinct assigned_group) as [Count] from mytable group by ticket_id

我只得到

ticket_id | count 
--------------------
10001     | 2
10002     | 2

有什么建议吗?

5 个答案:

答案 0 :(得分:1)

使用Distinct Count获取结果

select ticket_id, count(Distinct assigned_group) as [Count] 
from mytable 
group by ticket_id

答案 1 :(得分:0)

试试这个..

with temp as
(
select ticket_id, assigned_group, count(*) as count,date from mytable group by ticket_id, assigned_group,date
)

select ticket_id, count from temp

答案 2 :(得分:0)

您可以使用Row_number()函数查看下一条记录的值。

with tbl as (select *, row_number() over(partition by ticket_id order by 1) from table)

select a.ticket_id, a.assigned_group, a.assignee_name, a.date,
count(case when a.assigned_group <> b.assigned_group then 1 else 0 end) as No_of_change
from tbl as a
left join tbl as b
on a.rn = b.rn + 1

答案 3 :(得分:0)

如果您使用的是SQL Server 2012,则可以使用LAG功能轻松确定以前分配的组。然后,如果先前分配的组与当前分配的组不同,则可以增加计数,如下所示:

WITH previous_groups AS
(
  SELECT
    ticket_id,
    assign_date,
    assigned_group,
    LAG(assigned_group, 1, NULL) OVER (PARTITION BY ticket_id ORDER BY assign_date) AS prev_assign_group
  FROM mytable
)
SELECT
    ticket_id,
    SUM(CASE
        WHEN assigned_group <> prev_assign_group THEN 1
        ELSE 0
    END) AS count
FROM previous_groups
WHERE prev_assign_group IS NOT NULL
GROUP BY ticket_id
ORDER BY ticket_id;

如果您使用的是SQL Server 2008或更早版本,则需要额外的步骤来确定以前分配的组,如下所示:

WITH previous_assign_dates AS
(
  SELECT
    mt1.ticket_id,
    mt1.assign_date,
    MAX(mt2.assign_date) AS prev_assign_date
  FROM mytable mt1
  LEFT JOIN mytable mt2
  ON mt1.ticket_id = mt2.ticket_id
  AND mt2.assign_date < mt1.assign_date
  GROUP BY
    mt1.ticket_id,
    mt1.assign_date
),
previous_groups AS
(
    SELECT
        mt1.*,
        mt2.assigned_group AS prev_assign_group
    FROM mytable mt1
    INNER JOIN previous_assign_dates pad
    ON mt1.ticket_id = pad.ticket_id
    AND mt1.assign_date = pad.assign_date
    LEFT JOIN mytable mt2
    ON pad.ticket_id = mt2.ticket_id
    AND pad.prev_assign_date = mt2.assign_date
)
SELECT
    ticket_id,
    SUM(CASE
        WHEN assigned_group <> prev_assign_group THEN 1
        ELSE 0
    END) AS count
FROM previous_groups
WHERE prev_assign_group IS NOT NULL
GROUP BY ticket_id
ORDER BY ticket_id;

SQL Fiddle demo

<强>参考The LAG function on MSDN

答案 4 :(得分:0)

在故障单中添加序号,然后组中不同的自连接和连续序数应该有效:

SELECT t1.ticket_id, COUNT(*) FROM 
(SELECT *, ROW_NUMBER() OVER(PARTITION BY ticket_id ORDER BY date) ordinal 
FROM mytable) t1 
JOIN 
(SELECT *, ROW_NUMBER() OVER(PARTITION BY ticket_id ORDER BY date) ordinal FROM nytable) t2
ON t1.ticket_id=t2.ticket_id AND t1.assigned_group<>t2.assigned_group AND t1.ordinal+1=t2.ordinal
GROUP BY t1.ticket_id