2015-01-15 41 views
0

我需要計算票證上已分配組的更改。問題是我的日誌還記錄了在同一組中的受讓人的更改。列中的SQL計數更改

下面是一些樣本數據

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 

有什麼建議?

+0

你怎麼算,從A組變爲B組門票將A組分配給B組?它會是2還是3? – Jayvee 2015-01-15 17:34:58

+0

@Jayvee那將是3.每個變化都會計數,即使它之前已經到了那個組。 – Hoopdady 2015-01-15 18:28:02

+0

不應該票10001有計數1 – Santhosh 2015-01-15 18:32:27

回答

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 
1

使用Distinct Count得到結果

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

這不起作用,因爲如果我爲1002重新分配給組B,添加了其他條目,它仍然只有2。 – Hoopdady 2015-01-15 18:08:53

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 
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

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