查找记录比特定类型记录更新的所有分组记录

时间:2017-07-05 09:46:11

标签: sql sql-server

这将是我需要的中间输出:

Id1, Id2, Type,   Date
1,   2,    test,  2010-10-10
1,   3,    stage, 2010-05-05
1,   2,    stage, 2010-12-12
1,   3,    test,  2010-04-04
1,   3,    prod,  2010-12-31

从那时起,我想知道有多少分组(Id1,Id2)记录确实存在 记录后的Type ='test'。

对于上面我会得到这个最终输出:

Id1, Id2, Count
1,   2,   1
1,   3,   2

那将是什么SQL查询?

用例是找到所有那些从未进入过后期的记录而不是type =“test”

5 个答案:

答案 0 :(得分:0)

你必须得到日期的值,以及#34; test"输入相应的ID1和ID2,然后计算记录。像这样:

drop table if exists dbo.LaterThan;

create table dbo.LaterThan (
    Id1 int
    , Id2 int
    , Type varchar(100)
    , Date date
);

insert into dbo.LaterThan (Id1, Id2, Type, Date)
values (1, 2, 'test', '20101010')
    , (1, 3, 'stage', '20100505')
    , (1, 2, 'stage', '20101212')
    , (1, 3, 'test', '20100404')
    , (1, 3, 'prod', '20101231')

select
    lt.Id1
    , lt.Id2
    , count(case when lt.Date >= test.Date and lt.Type <> 'test' then 1 else null end) as Cnt
from dbo.LaterThan lt
    outer apply (
            select
                top (1)
                tlt.Date
            from dbo.LaterThan tlt
            where lt.ID1 = tlt.Id1
                and lt.ID2 = tlt.Id2
                and tlt.Type = 'test'
    ) test
group by lt.Id1, lt.Id2 

答案 1 :(得分:0)

SELECT x.Id1, x.Id2, x.[Date],
       CountAfter = (
           SELECT COUNT(*) FROM dbo.Table1 AS y 
           WHERE x.Id1=y.Id1 AND x.Id2=y.Id2 
           AND y.[Date]>x.[Date]) -- After
FROM dbo.Table1 AS x
WHERE x.[Type]='test'

注意:我假设每个{Id1, Id2}对值只有一行Type='test'

修改1:如果您想使用CountAfter = 0过滤所有行,那么

SELECT x.Id1, x.Id2, x.[Date]
FROM dbo.Table1 AS x
WHERE x.[Type]='test'
AND NOT EXISTS (
     SELECT * FROM dbo.Table1 AS y 
     WHERE x.Id1=y.Id1 AND x.Id2=y.Id2 
     AND y.[Date]>x.[Date])

或(但它不是最佳解决方案)

SELECT ...
FROM (
... insert main query ...
) AS mq
WHERE mq.CountAfter = 0

答案 2 :(得分:0)

我认为查询将是,

SELECT Id1, Id2, count(Id1) from <table_name> where Type = 'test' group by Id1, Id2;

答案 3 :(得分:0)

你可以这样做:

SELECT
    t.Id1,
    t.Id2,
    COUNT(*) AS Count
FROM (SELECT Id1, Id2, Date FROM Table WHERE TYPE = 'test') t
LEFT JOIN Table tt
    ON tt.Id1 = t.Id1 AND tt.Id2 = t.Id2 AND t.Date <= tt.Date
    AND tt.Type <> 'Test'
GROUP BY t.Id1, t.Id2

示例:

DECLARE @table TABLE (Id1 int, Id2 int, [Type] varchar(10),[Date] date)
INSERT INTO @table VALUES (1,   2,    'test',  '2010-10-10'),
(1,   3,    'stage', '2010-05-05'),
(1,   2,    'stage', '2010-12-12'),
(1,   3,    'test',  '2010-04-04'),
(1,   3,    'prod',  '2010-12-31')

SELECT
    t.Id1,
    t.Id2,
    COUNT(*) AS Count
FROM (SELECT Id1, Id2, Date FROM @Table WHERE TYPE = 'test') t
LEFT JOIN @Table tt
    ON tt.Id1 = t.Id1 AND tt.Id2 = t.Id2 AND t.Date <= tt.Date
    AND tt.Type <> 'Test'
GROUP BY t.Id1, t.Id2

答案 4 :(得分:0)

类似(Id1,Id2)记录可能有各种日期。 因此,如果您需要的是具有此(Id1,Id2)对的记录计数,而不考虑日期字段,那么这将适合您。

SELECT T.Id1,T.Id2,COUNT(*) AS Count 
FROM
   (SELECT Id1, Id2, Date 
    FROM TABLENAME 
    WHERE TYPE = 'test') AS T
GROUP BY
T.Id1, T.Id2

否则,您需要在GROUP BY子句中添加Date字段。