簡體   English   中英

基於觸發器的多列上的row_number

[英]row_number over multiple columns based on trigger

我正在嘗試將多個條件納入正在使用的數據集中。 Row_number似乎是在第二個查詢中使用滯后函數的方法,但我不太能100%得到它。

這是我的數據的結構:

CREATE TABLE emailhell(
   mainID  INTEGER  NOT NULL PRIMARY KEY 
  ,acctID  VARCHAR(4) NOT NULL
  ,emailID VARCHAR(2) NOT NULL
  ,type    INTEGER  NOT NULL
  ,created DATETIME  NOT NULL
);
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (1,'1234','1',6,'1/1/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (2,'1234','1',11,'1/1/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (3,'1234','2',6,'1/2/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (4,'1234','3',6,'1/3/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (5,'1234','4',6,'1/4/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (6,'ABC','89',6,'1/5/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (7,'ABC','90',6,'1/6/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (8,'ABC','90',11,'1/7/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (9,'258','22',6,'1/7/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (10,'258','1',6,'1/10/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (11,'258','2',6,'1/30/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (12,'258','3',6,'1/31/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (13,'258','29',6,'2/15/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (14,'258','29',11,'2/16/2018');
INSERT INTO emailhell(mainID,acctID,emailID,type,created) VALUES (15,'258','31',6,'3/1/2018');

和我想要的輸出

   +--------+--------+---------+------+-----------+-------+------------+
| mainID | acctID | emailID | type |  created  | index | touchcount |
+--------+--------+---------+------+-----------+-------+------------+
|      1 | 1234   |       1 |    6 | 1/1/2018  |     1 |            |
|      2 | 1234   |       1 |   11 | 1/1/2018  |     2 |          1 |
|      3 | 1234   |       2 |    6 | 1/2/2018  |     1 |            |
|      4 | 1234   |       3 |    6 | 1/3/2018  |     2 |            |
|      5 | 1234   |       4 |    6 | 1/4/2018  |     3 |            |
|      6 | ABC    |      89 |    6 | 1/5/2018  |     1 |            |
|      7 | ABC    |      90 |    6 | 1/6/2018  |     2 |            |
|      8 | ABC    |      90 |   11 | 1/7/2018  |     3 |          2 |
|      9 | 258    |      22 |    6 | 1/7/2018  |     1 |            |
|     10 | 258    |       1 |    6 | 1/10/2018 |     2 |            |
|     11 | 258    |       2 |    6 | 1/30/2018 |     3 |            |
|     12 | 258    |       3 |    6 | 1/31/2018 |     4 |            |
|     13 | 258    |      29 |    6 | 2/15/2018 |     5 |            |
|     14 | 258    |      29 |   11 | 2/16/2018 |     6 |          5 |
|     15 | 258    |      31 |    6 | 3/1/2018  |     1 |            |
+--------+--------+---------+------+-----------+-------+------------+

這是我正在使用的工具,但是由於某種原因,當活動看起來像類型6,后跟11,后跟6、11等時,它會出現問題。這是我查詢的開始,我敢肯定有更好的方法做這個。 然后,我使用LAG函數進行類似的查詢,以獲取類型11出現的時間。

SELECT dm.TABLE.*, 
       row_number() over(partition by dm.acctId, dm.type order by dm.acctId, dm.created_date) as index into dm.table2 
from dm.TABLE with (NOLOCK)

您正在通過acctId11定義組。 然后,對於11 s,您希望比組的大小小一。 因此,累計總和和其他一些東西:

select t.*,
       row_number() over (partition by acctId, grp order by mainId) as index,
       (case when type = 11
             then count(*) over (partition by acctId, grp ) - 1
        end) as touchcount
from (select t.*,
             sum(case when type = 11 then 1 else 0 end) over (partition by acctId order by mainId desc) as grp
      from t
     ) t;

我應該指出,組的定義需要倒數而不是前數。 這是因為11包含在“上一個”組中,而不是“下一個”組中的第一個記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM