我有一个常见的复杂场景,即在运行时在sql server中的多个列上创建组

时间:2017-01-03 09:52:48

标签: sql-server sql-server-2012

模式就像在col1中只更改了master,但col2中的所有值都相同。喜欢

[

输入表

id  Col1    Col2    ColC
1   A1      A2      1
2   A1      A3      1
3   A1      A1      1
4   A2      A2      2
5   A2      A1      2
6   A2      A3      2
7   A3      A1      3
8   A3      A2      3
9   A3      A3      3
10  B1      B1      4
11  B1      B2      4
12  B1      B3      4
13  B2      B1      5
14  B2      B2      5
15  B2      B3      5
16  B3      B1      6
17  B3      B2      6
18  B3      B3      6

输出结果应该是这样的

id  Col1    Col2    ColC    Group
1   A1      A2      1       G1
2   A1      A3      1       G1
3   A1      A1      1       G1
4   A2      A2      2       G1
5   A2      A1      2       G1
6   A2      A3      2       G1
7   A3      A1      3       G1
8   A3      A2      3       G1
9   A3      A3      3       G1
10  B1      B1      4       G2
11  B1      B2      4       G2
12  B1      B3      4       G2
13  B2      B1      5       G2
14  B2      B2      5       G2
15  B2      B3      5       G2
16  B3      B1      6       G2
17  B3      B2      6       G2
18  B3      B3      6       G2

我在循环中使用Update语句来解决问题但是下面的表格的插入和更新语句不够好。

Create Table #attachment (id int identity,effective_date int, Col1 int, Aid int, SCID int, Col3 int,
 termination_date int, Col2 int,Groups int)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42005',100073,71,3,21885,'42190',100073)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42005',100073,71,3,21885,'42190',100539)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42005',100073,71,3,21885,'42190',100561)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42005',100073,71,3,21885,'42190',103532)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42005',100073,71,3,21885,'42190',105078)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42219',103532,71,3,41483,'42262',100073)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42219',103532,71,3,41483,'42262',100539)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42219',103532,71,3,41483,'42262',100561)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42219',103532,71,3,41483,'42262',103532)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42219',103532,71,3,41483,'42262',105078)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42280',100561,71,3,41484,'42311',100073)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42280',100561,71,3,41484,'42311',100539)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42280',100561,71,3,41484,'42311',100561)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42280',100561,71,3,41484,'42311',103532)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42280',100561,71,3,41484,'42311',105078)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42341',103532,71,3,41485,'42581',100073)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42341',103532,71,3,41485,'42581',100539)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42341',103532,71,3,41485,'42581',100561)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42341',103532,71,3,41485,'42581',103532)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('42341',103532,71,3,41485,'42581',105078)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('423',103,71,3,41485,'42581',105)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('423',103,71,3,41485,'42581',103)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('423',105,71,3,41485,'42581',105)
    Insert into #attachment (effective_date, Col1, Aid, SCID, Col3, termination_date, Col2)values('423',105,71,3,41485,'42581',103)

这是更新声明

Declare @id int = 1, @Col1 int, @Col3 int,@maxid int = 24,@Groups int,@maxgropid int 
while @id <= @maxid
begin

    select @maxgropid = MAx(Groups) from  #attachment 

    select @Col1 = Col1 ,@Col3 =Col3,@Groups = Groups  from #attachment where id = @id 
    select @id,@maxgropid,@Col1
    IF @Groups is null 
    Begin 
    update #attachment set Groups = isnull(@maxgropid,0)+1
    where @Col1 In (
    select Distinct Col1 from #attachment where Col2 In (select Col2 from 
    #attachment where Col1 = @Col1)) and Col2 in (select Col2 from 
    #attachment where Col1 = @Col1)
    --and id = @id 
    end 
    set @id = @id+1


end 

如何在select语句中获取group column结果而不是更新one。

3 个答案:

答案 0 :(得分:1)

你想要这个吗?

    SELECT *,'G'+LTRIM(DENSE_RANK()OVER(ORDER BY LEFT(t.Col1,1))) 
    FROM (values
       (1, 'A1', 'A2', 1),
        (2, 'A1', 'A3', 1),
        (3, 'A1', 'A1', 1),
        (4, 'A2', 'A2', 2),
        (5, 'A2', 'A1', 2),
        (6, 'A2', 'A3', 2),
        (7, 'A3', 'A1', 3),
        (8, 'A3', 'A2', 3),
        (9, 'A3', 'A3', 3),
        (10, 'B1', 'B1', 4),
        (11, 'B1', 'B2', 4),
        (12, 'B1', 'B3', 4),
        (13, 'B2', 'B1', 5),
        (14, 'B2', 'B2', 5),
        (15, 'B2', 'B3', 5),
        (16, 'B3', 'B1', 6),
        (17, 'B3', 'B2', 6),
        (18, 'B3', 'B3', 6)
        ) t(ID,Col1,Col2,Colc)
ID          Col1 Col2 Colc        
----------- ---- ---- ----------- -------------------------
1           A1   A2   1           G1
2           A1   A3   1           G1
3           A1   A1   1           G1
4           A2   A2   2           G1
5           A2   A1   2           G1
6           A2   A3   2           G1
7           A3   A1   3           G1
8           A3   A2   3           G1
9           A3   A3   3           G1
10          B1   B1   4           G2
11          B1   B2   4           G2
12          B1   B3   4           G2
13          B2   B1   5           G2
14          B2   B2   5           G2
15          B2   B3   5           G2
16          B3   B1   6           G2
17          B3   B2   6           G2
18          B3   B3   6           G2

答案 1 :(得分:0)

CREATE TABLE #Table1
    ([id] int, [Col1] varchar(2), [Col2] varchar(2), [ColC] int)
;

INSERT INTO #Table1
    ([id], [Col1], [Col2], [ColC])
VALUES
    (1, 'A1', 'A2', 1),
    (2, 'A1', 'A3', 1),
    (3, 'A1', 'A1', 1),
    (4, 'A2', 'A2', 2),
    (5, 'A2', 'A1', 2),
    (6, 'A2', 'A3', 2),
    (7, 'A3', 'A1', 3),
    (8, 'A3', 'A2', 3),
    (9, 'A3', 'A3', 3),
    (10, 'B1', 'B1', 4),
    (11, 'B1', 'B2', 4),
    (12, 'B1', 'B3', 4),
    (13, 'B2', 'B1', 5),
    (14, 'B2', 'B2', 5),
    (15, 'B2', 'B3', 5),
    (16, 'B3', 'B1', 6),
    (17, 'B3', 'B2', 6),
    (18, 'B3', 'B3', 6)
;

;with cte as 
(
select  * ,left(col1,1) new_column from #Table1
)
select id, [Col1], [Col2], [ColC], case when new_column= 'A' then 'G1' else 'G2' end as n from cte


id  Col1    Col2    ColC    n
1   A1  A2  1   G1
2   A1  A3  1   G1
3   A1  A1  1   G1
4   A2  A2  2   G1
5   A2  A1  2   G1
6   A2  A3  2   G1
7   A3  A1  3   G1
8   A3  A2  3   G1
9   A3  A3  3   G1
10  B1  B1  4   G2
11  B1  B2  4   G2
12  B1  B3  4   G2
13  B2  B1  5   G2
14  B2  B2  5   G2
15  B2  B3  5   G2
16  B3  B1  6   G2
17  B3  B2  6   G2
18  B3  B3  6   G2

  SELECT *,'G'+LTRIM(DENSE_RANK()OVER(ORDER BY LEFT(Col1,1)))  from #table1

答案 2 :(得分:0)

使用更新语句。

Declare @id int = 1, @Col1 int, @Col3 int,@maxid int = 24,@Groups int,@maxgropid int 
    while @id <= @maxid
    begin

        select @maxgropid = MAx(Groups) from  #attachment 

        select @Col1 = Col1 ,@Col3 =Col3,@Groups = Groups  from #attachment where id = @id 
        select @id,@maxgropid,@Col1
        IF @Groups is null 
        Begin 
        update #attachment set Groups = isnull(@maxgropid,0)+1
        where @Col1 In (
        select Distinct Col1 from #attachment where Col2 In (select Col2 from 
        #attachment where Col1 = @Col1)) and Col2 in (select Col2 from 
        #attachment where Col1 = @Col1)
        --and id = @id 
        end 
        set @id = @id+1


    end