SQL - 按组计数记录

时间:2017-12-08 21:09:10

标签: sql sql-server sql-server-2008

我希望我的结果只包含每个债务数超过标记日期的记录。

SELECT DISTINCT
    tag.tag_shrt_nm AS [Tag]
    ,cnsmr.cnsmr_idntfr_agncy_id AS [Borrower Number]
    ,CAST(cnsmr_pymnt_tag.cnsmr_pymnt_tag_assgn_dt AS Date) AS [Tag Date]
    ,cnsmr_accnt_idntfr_agncy_id [Debt Number]
FROM         
    cnsmr INNER JOIN
    cnsmr_pymnt_jrnl ON cnsmr.cnsmr_id = cnsmr_pymnt_jrnl.cnsmr_id INNER JOIN
    cnsmr_pymnt_tag ON cnsmr_pymnt_jrnl.cnsmr_pymnt_jrnl_id =  cnsmr_pymnt_tag.cnsmr_pymnt_jrnl_id INNER JOIN
    tag ON cnsmr_pymnt_tag.tag_id = tag.tag_id INNER JOIN
    cnsmr_accnt ON cnsmr.cnsmr_id = cnsmr_accnt.cnsmr_id
WHERE     
    tag.tag_shrt_nm IN ('tRehab')
ORDER BY 
    cnsmr_accnt_idntfr_agncy_id

这里每个债务号码的日期超过1个,所以这是我想要的输出。

Tag         Borrower Number     Tag Date        Debt Number
tRehab      1000020234          2012-02-20      1089484
tRehab      1000020234          2012-04-07      1089484
tRehab      1000020234          2012-04-10      1089484
tRehab      1000020234          2012-02-20      1089487
tRehab      1000020234          2012-04-07      1089487
tRehab      1000020234          2012-04-10      1089487

此处每个标签日期只有1个债务编号,所以这不是我正在寻找的,不应该在结果中。

Tag         Borrower Number     Tag Date        Debt Number
tRehab      1000020234          2012-02-19      1089484
tRehab      1000020234          2012-04-07      1089485
tRehab      1000020234          2012-04-10      1089486
tRehab      1000020234          2012-02-20      1089487
tRehab      1000020234          2012-04-07      1089488
tRehab      1000020234          2012-04-10      1089489

3 个答案:

答案 0 :(得分:0)

您可以使用EXISTS查找具有多个日期的债务数字。请参阅下面的简化示例。

CREATE TABLE #Test(TagDate date, DebtNumber int)

INSERT INTO #Test(TagDate, DebtNumber) VALUES
('01/01/2017', 1089484),
('01/02/2017', 1089484),
('01/01/2017', 1089485),
('01/01/2017', 1089486),
('01/01/2017', 1089487),
('01/01/2017', 1089488)

SELECT *
FROM #Test AS O
WHERE EXISTS (SELECT 1 FROM #Test WHERE DebtNumber = O.DebtNumber HAVING COUNT(TagDate) > 1)

DROP TABLE #Test

答案 1 :(得分:0)

你可以试试这个,因为它只需要对你已有的东西进行最小的修改:

SELECT *
FROM (SELECT DISTINCT
         tag.tag_shrt_nm AS [Tag]
         ,cnsmr.cnsmr_idntfr_agncy_id AS [Borrower Number]
         ,CAST(cnsmr_pymnt_tag.cnsmr_pymnt_tag_assgn_dt AS Date) AS [Tag Date]
         ,cnsmr_accnt_idntfr_agncy_id [Debt Number]
         ,COUNT(cnsmr_accnt_idntfr_agncy_id) OVER(PARTITION BY cnsmr_accnt_idntfr_agncy_id ORDER BY cnsmr_accnt_idntfr_agncy_id) AS [Debt Number Count]
      FROM         
         cnsmr INNER JOIN
         cnsmr_pymnt_jrnl ON cnsmr.cnsmr_id = cnsmr_pymnt_jrnl.cnsmr_id INNER JOIN
         cnsmr_pymnt_tag ON cnsmr_pymnt_jrnl.cnsmr_pymnt_jrnl_id =  
         cnsmr_pymnt_tag.cnsmr_pymnt_jrnl_id INNER JOIN
         tag ON cnsmr_pymnt_tag.tag_id = tag.tag_id INNER JOIN
         cnsmr_accnt ON cnsmr.cnsmr_id = cnsmr_accnt.cnsmr_id
      WHERE     
         tag.tag_shrt_nm IN ('tRehab')
      ORDER BY 
         cnsmr_accnt_idntfr_agncy_id) tA
WHERE tA.[Debt Number Count] > 1

我想我唯一的问题是,是否存在同一债务数量同一日期不止一次出现的情况?例如,可能有两个不同的借款人号码?

答案 2 :(得分:0)

这应该这样做。

DECLARE @TabA TABLE (id INT IDENTITY(1,1), Tag VARCHAR(25), Borrower VARCHAR(25), TagDate DATE, DebtNumber VARCHAR(25))
INSERT INTO @TabA
        ( Tag, Borrower, TagDate, DebtNumber )

  select 'tRehab','1000020234','2012-02-20','1089484'
union select 'tRehab','1000020234','2012-04-07','1089484'
union select 'tRehab','1000020234','2012-04-10','1089484'
union select 'tRehab','1000020234','2012-02-20','1089487'
union select 'tRehab','1000020234','2012-04-07','1089487'
union select 'tRehab','1000020234','2012-04-10','1089487'

DECLARE @TabB TABLE (id INT IDENTITY(1,1), Tag VARCHAR(25), Borrower VARCHAR(25), TagDate DATE, DebtNumber VARCHAR(25))
INSERT INTO @TabB
        ( Tag, Borrower, TagDate, DebtNumber )

  select 'tRehab','1000020234','2012-02-19','1089484'
union select 'tRehab','1000020234','2012-04-07','1089485'
union select 'tRehab','1000020234','2012-04-10','1089486'
union select 'tRehab','1000020234','2012-02-20','1089487'
union select 'tRehab','1000020234','2012-04-07','1089488'
union select 'tRehab','1000020234','2012-04-10','1089489'

SELECT * FROM @TabA TA 
WHERE EXISTS (SELECT 1 FROM @TabA TA2 WHERE TA2.DebtNumber = TA.DebtNumber AND TA2.TagDate <> TA.TagDate AND TA.Id <> TA2.ID)


SELECT * FROM @TabB TA 
WHERE EXISTS (SELECT 1 FROM @TabB TA2 WHERE TA2.DebtNumber = TA.DebtNumber AND TA2.TagDate <> TA.TagDate AND TA.Id <> TA2.ID)