根据特定条件分组

时间:2018-06-29 03:13:41

标签: sql-server

我努力按照要求构建数据,但遇到了一些问题。

我想按照下面的图片显示在groupId列中的值。

目前我的数据集中有3组,分别是红色,绿色和蓝色,这些组实际上是我在TRXCODE 8202列中包含的10%TRAMT值,已经完成。

例如按图像-

  • 第2行的TRAMT列是TRAMT列的第3行的10%
  • 第5行的TRAMT列是TRAMT列的第4行的10%
  • 第7行的TRAMT列是TRAMT列的第6行的10%

所以我要根据我的图像在groupId中提供信息。

enter image description here

这是记录集的脚本

CREATE TABLE #TempRecords 
(CODE VARCHAR(20), TRDATE DATE, TRBATCH INT, TRREF VARCHAR(20), TRXCODE INT, TRAMT decimal(8,2)
);

GO

INSERT INTO #TempRecords
 VALUES ('ABMSC31', '2003-01-21', 6957, 'SD21010304', 1753, 31.20),
        ('ABMSC31', '2003-01-21', 6957, 'SD21010304', 8202, 3.12),
        ('ABMSC31', '2003-01-21', 6957, 'SD21010304', 8104, 589.68),
        ('ABMSC31', '2003-01-30', 7024, '0000056246', 8104, -31.20),
        ('ABMSC31', '2003-01-30', 7024, '0000056246', 1753, -589.68),
        ('ABMSC31', '2003-01-30', 7024, '0000056246', 8202, -3.12),
        ('ABMSC31', '2003-01-30', 7024, '0000056246', 8104, 554.60),
        ('ABMSC31', '2003-01-30', 7024, '0000056246', 1753, 32.18),
        ('ABMSC31', '2003-01-30', 7024, '0000056246', 8202, 3.22),
        ('ABMSC31', '2003-03-07', 7301, '0000057577', 1001, 1250.00),
        ('ABMSC31', '2003-03-07', 7301, '0000057577', 1001, 1250.00),
        ('ABMSC31', '2003-03-07', 7301, '0000057577', 1001, 1250.00),
        ('ABMSC31', '2003-09-25', 8737, '0000060259', 1701, 1104.00)

GO

SELECT * FROM #TempRecords

我根据以下代码制作并显示在图像中的Recordset

  SELECT *,DENSE_RANK () OVER(ORDER BY TRAMT1) as groupId
      FROM(
           SELECT CODE,
                  TRDATE,
                  TRBATCH,
                  TRREF,
                  TRXCODE,
                  TRAMT,
                  CAST(IIF(TRXCODE = 8202, TRAMT, TRAMT * 0.10 ) AS DECIMAL(8,2))  AS TRAMT1
             FROM #TempRecords 
          ) AS SubRecSet

谢谢

2 个答案:

答案 0 :(得分:1)

这也是可行的

SELECT 
    t1.* 
    ,t2.Grouped
    ,DENSE_RANK () OVER(ORDER BY TRAMT1 + case when Grouped = 'G' then 0 else rn * .00001 end) as groupId

    FROM
    (Select *, Row_Number() Over (order by  TRAMT1 ) as rn From
        (Select *   , CAST(case when TRXCODE = 8202 then TRAMT else TRAMT * 0.10 end  AS DECIMAL(8,2))  AS TRAMT1
        From #TempRecords) as t0 
    ) as  t1

LEFT JOIN
    (Select distinct
        CAST(case when TRXCODE = 8202 then TRAMT else TRAMT * 0.10 end  AS DECIMAL(8,2))  AS TRAMT2
        ,case when TRXCODE = 8202 then 'G' else 'n' end as Grouped
        FROM #TempRecords  where case when TRXCODE = 8202 then 'G' else 'n' end = 'G'
    ) as t2
    on t1.TRAMT1 = t2.tramt2

    order by t1.tramt1

答案 1 :(得分:0)

尝试以下操作:

    CREATE TABLE #TempRecords 
    (CODE VARCHAR(20), TRDATE DATE, TRBATCH INT, TRREF VARCHAR(20), TRXCODE INT, TRAMT decimal(8,2)
    );

    GO

    INSERT INTO #TempRecords
     VALUES ('ABMSC31', '2003-01-21', 6957, 'SD21010304', 1753, 31.20),
            ('ABMSC31', '2003-01-21', 6957, 'SD21010304', 8202, 3.12),
            ('ABMSC31', '2003-01-21', 6957, 'SD21010304', 8104, 589.68),
            ('ABMSC31', '2003-01-30', 7024, '0000056246', 8104, -31.20),
            ('ABMSC31', '2003-01-30', 7024, '0000056246', 1753, -589.68),
            ('ABMSC31', '2003-01-30', 7024, '0000056246', 8202, -3.12),
            ('ABMSC31', '2003-01-30', 7024, '0000056246', 8104, 554.60),
            ('ABMSC31', '2003-01-30', 7024, '0000056246', 1753, 32.18),
            ('ABMSC31', '2003-01-30', 7024, '0000056246', 8202, 3.22),
            ('ABMSC31', '2003-03-07', 7301, '0000057577', 1001, 1250.00),
            ('ABMSC31', '2003-03-07', 7301, '0000057577', 1001, 1250.00),
            ('ABMSC31', '2003-03-07', 7301, '0000057577', 1001, 1250.00),
            ('ABMSC31', '2003-09-25', 8737, '0000060259', 1701, 1104.00)

    GO

declare @max_id int
;with cte as
(
select distinct t1.CODE, t1.TRDATE, t1.TRBATCH, t1.TRREF, t1.TRXCODE, t1.TRAMT, CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) TRAMT1, DENSE_RANK () OVER(ORDER BY CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2))) as grp_id
from #TempRecords t1
join #TempRecords t2 on t1.TRAMT = CAST(IIF(t2.TRXCODE = 8202, t2.TRAMT, t2.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
or CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) = CAST(IIF(t2.TRXCODE = 8202, t2.TRAMT, t2.TRAMT * 0.10 ) AS DECIMAL(8,2))
where CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
in (select CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
from #TempRecords t1 where t1.TRXCODE = 8202) or t1.TRAMT = CAST(IIF(t2.TRXCODE = 8202, t2.TRAMT, t2.TRAMT * 0.10 ) AS DECIMAL(8,2))
)
select max(grp_id) grp_id into #t from cte
set @max_id = (select grp_id from #t)
drop table if exists #t
select t1.CODE, t1.TRDATE, t1.TRBATCH, t1.TRREF, t1.TRXCODE, t1.TRAMT, CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) TRAMT1, @max_id + row_number () OVER(ORDER BY CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2))) as grp_id
from #TempRecords t1
where t1.TRXCODE <> 8202
and CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
not in (select CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) from #TempRecords t1 where t1.TRXCODE = 8202)
UNION
select distinct t1.CODE, t1.TRDATE, t1.TRBATCH, t1.TRREF, t1.TRXCODE, t1.TRAMT, CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) TRAMT1, DENSE_RANK () OVER(ORDER BY CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2))) as grp_id
from #TempRecords t1
join #TempRecords t2 on t1.TRAMT = CAST(IIF(t2.TRXCODE = 8202, t2.TRAMT, t2.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
or CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) = CAST(IIF(t2.TRXCODE = 8202, t2.TRAMT, t2.TRAMT * 0.10 ) AS DECIMAL(8,2))
where CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
in (select CAST(IIF(t1.TRXCODE = 8202, t1.TRAMT, t1.TRAMT * 0.10 ) AS DECIMAL(8,2)) 
from #TempRecords t1 where t1.TRXCODE = 8202) or t1.TRAMT = CAST(IIF(t2.TRXCODE = 8202, t2.TRAMT, t2.TRAMT * 0.10 ) AS DECIMAL(8,2))
order by grp_id