按列将值递增到行中-SQL Server

时间:2018-11-14 19:14:11

标签: sql sql-server

我有以下类型的数据

Input

CREATE TABLE #tmp
(
    Room [NVARCHAR](50) NOT NULL,
    iking INT,
    iqueen INT,
    isingle INT,
    idouble INT
)

INSERT INTO #tmp 
VALUES ('Marriot', 0, 1, 2, 1),
       ('Hilton', 1, 2, 0, 1)

我尝试了Cross Applycase语句

我将数据添加到临时表中,并为每列编写了4个交叉应用函数

  1. 国王

    SELECT tk.Room, tk.iking, Type = CONCAT('BED', t.n)
    INTO #tempking1
    FROM #tmp tk
    CROSS APPLY
        (SELECT TOP (tk.iking)
            n = ROW_NUMBER() OVER (ORDER BY o.object_id)
         FROM sys.objects o) t
    ORDER BY tk.Room;
    --select * from #tempking1
    
  2. 女王

    SELECT 
        tq.Room, tq.iQueen,   
        Type = CASE WHEN ROOM in (SELECT  Distinct ROOM FROM #tempking1) 
                       THEN CONCAT('BED', t.n + 1)
                       ELSE CONCAT('BED', t.n) 
               END          
    INTO #tempQueen1        
    FROM #tmp tq
    CROSS APPLY 
        (SELECT TOP (tq.iQueen) 
             n = ROW_NUMBER() OVER (ORDER BY o.object_id)
         FROM sys.objects o) t 
     ORDER BY tq.Room;
     --select * from #tempqueen1
    
  3. SELECT 
        tq.Room, tq.isingle,   
        Type = CASE WHEN ROOM IN (SELECT Distinct ROOM FROM #tempking1) 
                       THEN CONCAT('BED', t.n + 1)
                    WHEN ROOM IN (SELECT Distinct ROOM FROM #tempqueen1) 
                       THEN CONCAT('BED', t.n + 1)
                    ElSE CONCAT('BED', t.n) 
               END
    INTO #tempsingle1
    FROM #tmp tq
    CROSS APPLY 
        (SELECT TOP (tq.isingle)
             n = ROW_NUMBER() OVER (ORDER BY o.object_id)
         FROM sys.objects o) t
    ORDER BY tq.Room;
    --select * from #tempsingle1
    
  4. Double

    SELECT 
        tq.Room, tq.isingle,   
        Type = CASE WHEN ROOM IN (SELECT Distinct ROOM FROM #tempking1)  
                       THEN CONCAT('BED', t.n + 1)
                    WHEN ROOM IN (SELECT Distinct ROOM FROM #tempqueen1) 
                       THEN CONCAT('BED', t.n + 1)
                    WHEN ROOM IN (SELECT Distinct ROOM FROM #tempsingle1) 
                       THEN CONCAT('BED', t.n + 1)
                    ELSE CONCAT('BED', t.n) 
                END
        INTO #tempdouble1
        FROM #tmp tq
        CROSS APPLY 
            (SELECT TOP (tq.isingle)
                 n = ROW_NUMBER() OVER (ORDER BY o.object_id)
             FROM sys.objects o) t
        ORDER BY tq.Room;
        --select * from #tempDouble1
    
    SELECT Room, Type, 'King' AS Descp FROM #tempKing1
    UNION ALL
    SELECT Room, Type, 'Queeen' AS Descp FROM #tempQueen1   
    UNION ALL
    SELECT Room, Type, 'Single' AS Descp FROM #tempsingle1
    UNION ALL
    SELECT Room, Type, 'Double' AS Descp FROM #tempDouble1
    

但是我知道了

Output

我的例外输出是

Excepted Output

你能帮我吗

1 个答案:

答案 0 :(得分:0)

您可以使用UNPIVOT运算符来解决您的解决方案。试试这个脚本:

drop TABLE #tmp
go
drop TABLE #Hotel
go

CREATE TABLE #tmp
( 
     Room       nvarchar(30) NOT NULL
    ,iking      int 
    ,iqueen     int
    ,isingle    int
    ,idouble    int 
)

CREATE TABLE #Hotel
(
     Room       NVARCHAR(30)
    ,RoomType   NVARCHAR(30)
    ,Total      INT
)

Insert into #tmp Values ('Marriot', 0,1,2,1),('Hilton', 1,2,0,1)

INSERT INTO #Hotel
SELECT Room, RoomType, Total  
FROM   
   (SELECT Room,iking,iqueen,isingle,idouble
   FROM #tmp) p  
UNPIVOT  
   (Total FOR RoomType IN   
      (iking,iqueen,isingle,idouble)  
)AS unpvt

SELECT   Room
        ,RoomType
        --,'Bed'+CAST(Number AS VARCHAR) AS [Desc]
        ,'Bed'+CAST(ROW_NUMBER() OVER(PARTITION BY Room ORDER BY RoomType,Number) AS VARCHAR) AS [Desc]
        ,'Bed'+CAST(Total AS varchar) [Desc2]
FROM #Hotel
INNER JOIN master.dbo.spt_values N ON  Total>=N.Number AND N.type='P' AND number<>0