2016-02-24 44 views
0

請幫我這個:SQL Server的子查詢/加入基於數值計數

表我有以下數據:

Menu     dateTime     CLI 

START     2016-02-23 14:08:52.047 4001 
WELCOME     2016-02-23 14:08:52.047 4001 
LANG_SEL    2016-02-23 14:08:52.047 4001 
SERVICE_MENU   2016-02-23 14:08:52.047 4001 
CUSTOMER_ACCOUNT  2016-02-23 14:08:52.047 4001 
BILLING_MENU   2016-02-23 14:08:52.047 4001 
STOP     2016-02-23 14:08:52.047 4001 
START     2016-02-23 14:08:52.047 2000 
WELCOME     2016-02-23 14:08:52.047 2000 
LANG_SEL    2016-02-23 14:08:52.047 2000 
BILLING_MENU   2016-02-23 14:08:52.047 2000 
STOP     2016-02-23 14:08:52.047 2000 
START     2016-02-23 14:08:52.047 34543 
WELCOME     2016-02-23 14:08:52.047 34543 
LANG_SEL    2016-02-23 14:08:52.047 34543 
SERVICE_MENU   2016-02-23 14:08:52.047 34543 
BillComplaintSelection 2016-02-23 14:08:52.047 34543 
VerbalizeBilltkt  2016-02-23 14:08:52.047 34543 
STOP     2016-02-23 14:08:52.047 34543 
START     2016-02-23 14:08:52.047 4001 
WELCOME     2016-02-23 14:08:52.047 4001 
LANG_SEL    2016-02-23 14:08:52.047 4001 
BILLING_MENU   2016-02-23 14:08:52.047 4001 
VerbalizebillDetails 2016-02-23 14:08:52.047 4001 
BILLING_MENU   2016-02-23 14:08:52.047 4001 
STOP     2016-02-23 14:08:52.047 4001 

我的會議開始與價值START和結束的與值停止我想計算菜單名稱在停止之前發生的每一天。

就像從上面的數據,我必須得到以下輸出:

Menu      Count  DateTime 

BILLING_MENU    3  2016-02-23 14:08:52.047 
VerbalizeBilltkt   1  2016-02-23 14:08:52.047 
+0

其中兩個輸出變化的一個是正確的? –

+0

你應該使用一個整數值作爲ID。 – bmsqldev

+0

請再次閱讀該問題。爲了更好的理解,我改變了相應的問題。謝謝 – BilalAhmed

回答

2

只要dateTime字段包含在上升離散datetime值訂單,您可以使用以下查詢來確定每個訂單的開始/結束時間:START - STOP區間:

SELECT t1.`dateTime` AS start_time, 
     (SELECT t2.`dateTime` 
     FROM mytable AS t2 
     WHERE t2.ID = 'STOP' AND t1.CLI = t2.CLI AND 
      t1.`dateTime` < t2.`dateTime` 
     ORDER BY t2.`dateTime` ASC LIMIT 1) AS end_time 
FROM mytable AS t1 
WHERE t1.ID = 'START' 

使用上面可以實現類似下面的計算所有間隔當中的ID出場次數:如果你只是

SELECT ID, 
     SUM(CASE WHEN cnt >= 1 THEN 1 END) AS cnt, 
     DATE(start_time) 
FROM (  
    SELECT x1.ID, COUNT(*) AS cnt, x2.start_time 
    FROM mytable AS x1 
    INNER JOIN (
    SELECT t1.`dateTime` AS start_time, 
      (SELECT t2.`dateTime` 
      FROM mytable AS t2 
      WHERE t2.ID = 'STOP' AND 
       t1.CLI = t2.CLI AND 
       t1.`dateTime` < t2.`dateTime` 
      ORDER BY t2.`dateTime` ASC LIMIT 1) AS end_time 
    FROM mytable AS t1 
    WHERE t1.ID = 'START' 
) AS x2 ON x1.`dateTime` BETWEEN x2.start_time AND x2.end_time 
    WHERE x1.ID <> 'START' AND x1.ID <> 'STOP' AND x1.ID <> 'WELCOME' 
    GROUP BY ID, x2.start_time, x2.end_time) AS t 
GROUP BY ID, DATE(start_time) 

Demo here

編輯要計算每個STOP記錄之前發生的記錄,那麼您可以使用以下曲線ERY:

SELECT DATE(`dateTime`), ID, COUNT(*) 
FROM (
    SELECT t1.`dateTime`, 
     (SELECT ID 
     FROM mytable AS t2 
     WHERE t1.CLI = t2.CLI AND 
       t2.`dateTime` < t1.`dateTime` 
     ORDER BY t2.`dateTime` DESC LIMIT 1) AS ID  
    FROM mytable AS t1 
    WHERE ID = 'STOP') AS t 
    GROUP BY DATE(`dateTime`), ID 

Demo here

+0

你是冠軍先生,請幫我準備那些在** STOP **之前出現的記錄? – BilalAhmed

+0

@BilalAhmed那些記錄呢? –

+0

我需要數他們先生。 – BilalAhmed

-1

這可能會回答你的問題......

SELECT ID, COUNT(ID) AS TotalCount, -- You cannot use count as a field name CAST(DateTime AS DATE) AS DateOnly FROM TableName WHERE ID <> 'Start' or ID <> 'Stop' GROUP BY ID, CAST(DateTime AS DATE)

+0

我很抱歉,但這不是親愛的。 – BilalAhmed