Sql 基于触发器的多列上的行数
我试图将多个条件考虑到我正在使用的数据集中。行数似乎是第二次查询中使用lag函数的方式,但我不能100%地得到它 以下是我的数据的结构:Sql 基于触发器的多列上的行数,sql,sql-server,Sql,Sql Server,我试图将多个条件考虑到我正在使用的数据集中。行数似乎是第二次查询中使用lag函数的方式,但我不能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
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)
您正在通过
acctId
和11
定义组。然后对于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
包含在“上一个”组中,而不是“下一个”组中的第一条记录。因此您要计算类型值为6的记录数?NOLOCK
意味着在获取更多锁的同时返回脏数据。这不是一个快速的暗示。如果你认为你需要它,那就意味着你有一个性能问题。我想在每个帐户和电子邮件ID显示11时计算前面6个的数量。NOLOCK习惯于从生产数据存储中提取数据。我不担心无锁或脏数据,因为我是表的唯一用户。查询在几秒钟内完成,我没有性能问题。我们真正需要的是一些细节。表结构、示例数据和所需输出。这是一个很好的起点@瑞安·威尔逊不,那是不对的。阅读我发布的关于NOLOCK的文章。一些打字错误,例如应该是[index]
,而不仅仅是index
,touchcount
表达式应该是count(*)over(按acctId,grp划分)-1
@GordonLinoff看看这个,从逻辑上讲它是有意义的。老实说,我不知道SQL能得到如此强大的功能(SaS./R家伙)。有一件事我搞不清楚;如果我可以单独运行下半部分来建立“grp”,它就可以正常工作。但是,当我尝试运行整个块时,它似乎不喜欢初始的t.*和t;最后。至少红色下划线在哪里。这可能是版本问题吗?没关系,想清楚。别名的语法问题。这太棒了,我感谢大家的帮助!