group by having

qq_20039471 2014-09-01 09:13:59

create table test(c1 varchar(10),c2 datetime,c3 datetime,c4 int)
go
insert into test
select 'A',dateadd(mm,-20,getdate()),getdate(),1 union all
select 'A',dateadd(mm,-15,getdate()),getdate(),2 union all
select 'B',dateadd(mm,0,getdate()),getdate(),3 union all
select 'C',dateadd(mm,0,getdate()),getdate(),3 union all
select 'C',dateadd(mm,0,getdate()),getdate(),4 union all
select 'D',dateadd(mm,0,getdate()),getdate(),4 union all
select 'E',null,getdate(),5

/**根据c1分组,c3排序,c4不等于5
排除超过3条的记录和3条以下且c2距离当前时间为20分钟以内(2条记录取max(c2))(c2为null的不在这范围内)
**/
...全文
147 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiger_Zhao 2014-09-01
  • 打赏
  • 举报
回复
WITH test (id,c1,c2,c3,c4) AS(
select 1,'A',dateadd(mm,-40,getdate()),getdate(),1 union all
select 2,'A',dateadd(mm,-21,getdate()),getdate(),2 union all
select 3,'B',dateadd(mm,0,getdate()),getdate(),3 union all
select 4,'C',dateadd(mm,-60,getdate()),getdate(),3 union all
select 5,'C',dateadd(mm,-40,getdate()),getdate(),4 union all
select 6,'C',dateadd(mm,-10,getdate()),getdate(),3 union all
select 7,'D',dateadd(mm,0,getdate()),getdate(),4 union all
select 8,'E',null,getdate(),4 union all
select 9,'F',null,getdate(),5
),
o AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY c1 ORDER BY c2 DESC) n
FROM test
)
,
g AS (
SELECT c1,
count(*) c
FROM test
GROUP BY c1
HAVING count(*) < 3
)
SELECT o.id,o.c1,o.c2,o.c3,o.c4
FROM o
JOIN g
ON g.c1 = o.c1
WHERE o.n=1
AND o.c4<>5
AND ( datediff(minute,o.c2,getdate())>20.0
OR o.c2 IS NULL
)

         id c1   c2                      c3                               c4
----------- ---- ----------------------- ----------------------- -----------
2 A 2012-12-01 11:26:21.227 2014-09-01 11:26:21.227 2
8 E NULL 2014-09-01 11:26:21.227 4
专注or全面 2014-09-01
  • 打赏
  • 举报
回复

按你的要求写的
悲剧,你要的四AE,我查询出来的BCD

select * from
(
select 
c1,c2,c3,c4,
COUNT(*)over(partition by c1) as ct,
ROW_NUMBER()over(partition by c1 order by c3 desc) as rm
from test
where 
c4!='5'
and 
c2 >dateadd(MI,-20,GETDATE())
) t where rm=1 and ct<3


c1         c2                      c3                      c4          ct          rm
---------- ----------------------- ----------------------- ----------- ----------- --------------------
B          2014-09-01 11:11:27.237 2014-09-01 11:11:27.237 3           1           1
C          2014-09-01 11:11:27.237 2014-09-01 11:11:27.237 3           2           1
D          2014-09-01 11:11:27.237 2014-09-01 11:11:27.237 4           1           1

(3 行受影响)


qq_20039471 2014-09-01
  • 打赏
  • 举报
回复
引用 1 楼 ap0405140 的回复:
请问希望结果是什么? 请举例说明结果的格式及数据.
希望结果是 2 A 2012-12-01 10:32:11.943 2014-09-01 10:32:11.943 2 8 E NULL 2014-09-01 10:32:11.943 4 上面的3和7要排除掉,理由上面已写
qq_20039471 2014-09-01
  • 打赏
  • 举报
回复
引用 1 楼 ap0405140 的回复:
请问希望结果是什么? 请举例说明结果的格式及数据.

create table test(id int,c1 varchar(10),c2 datetime,c3 datetime,c4 int)
go
insert into test
select 1,'A',dateadd(mm,-40,getdate()),getdate(),1 union all
select 2,'A',dateadd(mm,-21,getdate()),getdate(),2 union all
select 3,'B',dateadd(mm,0,getdate()),getdate(),3 union all
select 4,'C',dateadd(mm,-60,getdate()),getdate(),3 union all
select 5,'C',dateadd(mm,-40,getdate()),getdate(),4 union all
select 6,'C',dateadd(mm,-10,getdate()),getdate(),3 union all
select 7,'D',dateadd(mm,0,getdate()),getdate(),4 union all
select 8,'E',null,getdate(),4  union all
select 9,'F',null,getdate(),5 


2 A	2012-12-01 10:32:11.943	2014-09-01 10:32:11.943	2
3 B	2014-09-01 10:32:11.943	2014-09-01 10:32:11.943	3
7 D	2014-09-01 10:32:11.943	2014-09-01 10:32:11.943	4
8 E	NULL	2014-09-01 10:32:11.943	4
为什么是这几条呢。 ID 2的是比1的c1时间晚,且c2距离当前时间超过20分钟 ID 8是c4<>5,且只有1条,平且C1时间为null,标识未处理 ID3和7是因为C1时间不为空,且C1时间未超过20分钟
唐诗三百首 2014-09-01
  • 打赏
  • 举报
回复
请问希望结果是什么? 请举例说明结果的格式及数据.
qq_20039471 2014-09-01
  • 打赏
  • 举报
回复
引用 8 楼 Tiger_Zhao 的回复:
[quote=引用 6 楼 qq_20039471 的回复:]如果加个规则呢 比方说之前的数据再加上2条 select 11,'G',dateadd(mm,-22,getdate()),getdate(),3 union all select 12,'G',dateadd(mm,0,getdate()),getdate(),5 union all 这2个数据也要排除掉,第一条是符合条件的,但是由于第二条的c4=5,说明已经把c1为g的处理完成了,只不过是第二次才处理成功。
原查询完全符合要求。
引用 7 楼 qq_20039471 的回复:
顺便问下o.n=1是什么意思
你看一下 o 的内容就明白了。[/quote] 好的。ths
Tiger_Zhao 2014-09-01
  • 打赏
  • 举报
回复
引用 6 楼 qq_20039471 的回复:
如果加个规则呢 比方说之前的数据再加上2条 select 11,'G',dateadd(mm,-22,getdate()),getdate(),3 union all select 12,'G',dateadd(mm,0,getdate()),getdate(),5 union all 这2个数据也要排除掉,第一条是符合条件的,但是由于第二条的c4=5,说明已经把c1为g的处理完成了,只不过是第二次才处理成功。
原查询完全符合要求。
引用 7 楼 qq_20039471 的回复:
顺便问下o.n=1是什么意思
你看一下 o 的内容就明白了。
qq_20039471 2014-09-01
  • 打赏
  • 举报
回复
顺便问下o.n=1是什么意思
qq_20039471 2014-09-01
  • 打赏
  • 举报
回复
引用 5 楼 Tiger_Zhao 的回复:
WITH test (id,c1,c2,c3,c4) AS(
select 1,'A',dateadd(mm,-40,getdate()),getdate(),1 union all
select 2,'A',dateadd(mm,-21,getdate()),getdate(),2 union all
select 3,'B',dateadd(mm,0,getdate()),getdate(),3 union all
select 4,'C',dateadd(mm,-60,getdate()),getdate(),3 union all
select 5,'C',dateadd(mm,-40,getdate()),getdate(),4 union all
select 6,'C',dateadd(mm,-10,getdate()),getdate(),3 union all
select 7,'D',dateadd(mm,0,getdate()),getdate(),4 union all
select 8,'E',null,getdate(),4  union all
select 9,'F',null,getdate(),5 
),
o AS (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY c1 ORDER BY c2 DESC) n
      FROM test
)
,
g AS (
    SELECT c1,
           count(*) c
      FROM test
  GROUP BY c1
    HAVING count(*) < 3
)
SELECT o.id,o.c1,o.c2,o.c3,o.c4
  FROM o
  JOIN g
    ON g.c1 = o.c1
 WHERE o.n=1
   AND o.c4<>5
   AND (   datediff(minute,o.c2,getdate())>20.0
        OR o.c2 IS NULL
       )
         id c1   c2                      c3                               c4
----------- ---- ----------------------- ----------------------- -----------
          2 A    2012-12-01 11:26:21.227 2014-09-01 11:26:21.227           2
          8 E    NULL                    2014-09-01 11:26:21.227           4
谢谢,安全符合我的要求。 如果加个规则呢 比方说之前的数据再加上2条 select 11,'G',dateadd(mm,-22,getdate()),getdate(),3 union all select 12,'G',dateadd(mm,0,getdate()),getdate(),5 union all 这2个数据也要排除掉,第一条是符合条件的,但是由于第二条的c4=5,说明已经把c1为g的处理完成了,只不过是第二次才处理成功。

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧