1

I have the following type of data

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)

I tried Cross Apply and case statements

I add data into temp table and wrote 4 cross apply functions for each column

  1. King

    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. Queen

    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. Single

    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
    

but I got

Output

My excepted output is

Excepted Output

Could you please help me

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JP Jack
  • 699
  • 1
  • 7
  • 13
  • 7
    Hi and welcome to SO. Not many people are just going to write this for you. And posting images of data is not helpful, [why](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557)? You need to post some details like [this article](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/). – Sean Lange Nov 14 '18 at 19:16
  • As commented, you will need to improve your question in order to get help. A good start would be to add the statements that you have tried. It also looks like you could potentially solve the problem with a [SELECT DISTINCT statement](https://www.w3schools.com/sql/sql_distinct.asp) – mtr.web Nov 14 '18 at 19:19
  • You say you tried using `CROSS APPLY` and a `CASE` expression (it's not a statement). Could you include the code you tried when you update your post as per Sean's guide lines too please? Seeing your attempts often helps us understand your goal better, and sometimes it's a very small change that gets you what you're after; making the answer far easier for others to provide. – Thom A Nov 14 '18 at 19:25
  • Please don't put your code in the comments. It is part of the question...not to mention the formatting is just horrible in comments. – Sean Lange Nov 14 '18 at 19:56
  • To clarify Sean's comment, EDIT your question and put this info into it. Prefix each line with 4 spaces to render it as a code block. Please ensure your sql is nicely formatted/pretty printed and not just one big block of unindented/unformatted mess – Caius Jard Nov 14 '18 at 20:01
  • Thank you Sean and Caius Do i need to provide more information – JP Jack Nov 14 '18 at 20:29

1 Answers1

0

You can use UNPIVOT operator to fix your solution. Try this script:

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
Zeki Gumus
  • 1,484
  • 7
  • 14
  • Thank you for your response We can use pivot, But if iqueen = 2 we need 2 rows and type must be bed1 and bed2. In your script it getting only one row iqueen and bed1 – JP Jack Nov 14 '18 at 20:53
  • I believe my script already cover it(with Numbers table). Did you execute it? – Zeki Gumus Nov 14 '18 at 20:56
  • I am sorry, I just noticed the desc detail. Still the script cover what you want.I have added [Desc2] to my final select script. Can you check it? – Zeki Gumus Nov 14 '18 at 21:00
  • Thank You Ayzek. Its working – JP Jack Nov 14 '18 at 22:00
  • Ayzek, The code you provided is one-time use. If I re-run the code the bed+ value is incrementing and changing the values. Could you please explain me the "INNER JOIN master.dbo.spt_values N ON Total>=N.Number AND N.type='P' AND number<>0" – JP Jack Nov 15 '18 at 19:24
  • @VenkataJagadishPippalla Probably you got an error message because of the Hotel table. I have added an extra script to create Hotel first and insert into the table. the "master.dbo.spt_values" table contains numbers sequentially increasing from 0 to 2047. You wanted to multiply the beds if it more than one so I used this table to multiply it. You can find here:https://stackoverflow.com/questions/4273723/what-is-the-purpose-of-system-table-master-spt-values-and-what-are-the-meanings more information about the spt_values table. – Zeki Gumus Nov 15 '18 at 19:52
  • Gmus Thank you for your response. Now I changed the Row Number() to Dense_Rank() and added few more columns – JP Jack Nov 16 '18 at 16:57