MS SQL Group基于开始和结束的行

时间:2017-04-09 21:05:26

标签: sql sql-server loops group-by

我有一个看起来像这样的表,重复的行为3和3 ...

Column1      | Column2
CustomerID   | 22
CustomerName | ”ABC”
Responsible  | ”Allan”
CustomerID   | 23
CustomerName | ”DEF”
Responsible  | ”Jessica”
CustomerID   | 24
CustomerName | ”GHI”
Responsible  | ”Paul”

以下脚本可用于创建表格并使用示例数据填充...

CREATE TABLE Responsible
    ( [ RowType ] VARCHAR(12),
      [ Value ]   VARCHAR(9) )
;

INSERT INTO Responsible
    ( [RowType],
      [ Value ] )
VALUES
    ( 'CustomerID',
      '22' ),
    ( 'CustomerName',
      'ABC'),
    ( 'Responsible',
      'Allan' ),
    ( 'CustomerID',
      '23' ),
    ( 'CustomerName',
      'DEF' ),
    ( 'Responsible',
      'Jessica' ),
    ( 'CustomerID',
      '24' ),
    ( 'CustomerName',
      'GHI' ),
    ( 'Responsible',
      'Paul' );

我想让它像一张看起来像这样的表:

CustomerID | CustomerName | Responsible  
22         | ABC          | Allan  
23         | DEF          | Jessica  
24         | GHI          | Paul      

前进的最佳方式是什么?

2 个答案:

答案 0 :(得分:0)

编辑,SQL服务器版本:

查询10

select CustomerID, CustomerName, Responsible
from (
select row_number() over(order by k)  as id , v as CustomerID
from t1
where k ='CustomerID') tt1
inner join (
select row_number() over(order by k)  as id , v as CustomerName
from t1
where k ='CustomerName') tt2
on tt1.id = tt2.id
inner join (
select row_number() over(order by k)  as id , v as Responsible
from t1
where k ='Responsible') tt3
on tt1.id = tt3.id

<强> Results

| CustomerID | CustomerName | Responsible |
|------------|--------------|-------------|
|         22 |          ABC |       Allan |
|         23 |          DEF |     Jessica |
|         24 |          GHI |        Paul |

这是你想要的吗?

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE t1
    (`k` varchar(12), `v` varchar(9))
;

INSERT INTO t1
    (`k`, `v`)
VALUES
    ('CustomerID', '22'),
    ('CustomerName', 'ABC'),
    ('Responsible', 'Allan'),
    ('CustomerID', '23'),
    ('CustomerName', 'DEF'),
    ('Responsible', 'Jessica'),
    ('CustomerID', '24'),
    ('CustomerName', 'GHI'),
    ('Responsible', 'Paul')
;

查询1

set @v1 = 0, @v2 = 0, @v3 = 0

查询2

select CustomerID, CustomerName, Responsible
from (
    select @v1:= @v1+1 as id , v as CustomerID
    from t1
    where k ='CustomerID'
) tt1
inner join (
    select @v2:= @v2+1 as id , v as CustomerName
    from t1
    where k ='CustomerName'
) tt2
    on tt1.id = tt2.id
inner join (
    select @v3:= @v3+1 as id , v as Responsible
    from t1
    where k ='Responsible'
) tt3
    on tt1.id = tt3.id;

<强> Results

| CustomerID | CustomerName | Responsible |
|------------|--------------|-------------|
|         22 |          ABC |       Allan |
|         23 |          DEF |     Jessica |
|         24 |          GHI |        Paul |

答案 1 :(得分:0)

我让它在SQL Server中像这样工作。我没有看到任何其他选项,但是使用游标一次下降一行。 以下脚本仅适用于您的独特情况。

创建新表格

 USE [YOURDATABASE NAME GOES HERE]
 GO

 SET ANSI_NULLS ON
 GO

 SET QUOTED_IDENTIFIER ON
 GO

 CREATE TABLE [dbo].[Table_2](
  [CustomerID] [int] NULL,
  [CustomerName] [varchar](50) NULL,
  [Responsible] [varchar](50) NULL
 ) ON [PRIMARY]

 GO

将数据插入表格

 DECLARE @CustomerId INT
 DECLARE @CustomerName VARCHAR(50)
 DECLARE @Responsible VARCHAR(50)
 DECLARE myCursor CURSOR
 FOR SELECT Column2 FROM Table_1;
 OPEN myCursor;
 FETCH NEXT FROM myCursor
 INTO @CustomerId;

 FETCH NEXT FROM myCursor
 INTO @CustomerName;

 FETCH NEXT FROM myCursor
 INTO @Responsible;

 WHILE @@FETCH_STATUS = 0
 BEGIN
        INSERT INTO [dbo].[Table_2](CustomerID,CustomerName,Responsible)
        VALUES (@CustomerId,@CustomerName,@Responsible)

        FETCH NEXT FROM myCursor
        INTO @CustomerId;

        FETCH NEXT FROM myCursor
        INTO @CustomerName;

        FETCH NEXT FROM myCursor
        INTO @Responsible;
END
CLOSE myCursor;
DEALLOCATE myCursor;
GO