t sql horizontal query

Ali Yılmaz 81 Reputation points
2022-07-21T12:05:09.413+00:00

Hi,

I have a sql table like this. I need to make this table horizontally. Their properties will be written side by side. Descriptions will be written below. How can I do that. I would be glad if you help.

223137-sql-live.png

select top 100 ASP.Articel AS [Product Name], ASPD.English AS Ozellik, ASP.English  
  
from ArticelSpec ASP  
  
inner join ArticelSpecDefinition ASPD on ASP.ArticelSpecDefinition = ASPD.Oid  
  
where Articel='176E0AC3-1874-4801-BEB8-16E9B2F7D1FD'   
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-07-21T13:12:49.867+00:00

    Do you want just these 5 columns with these pre-defined titles or you want it to be dynamic?

    For simple static PIVOT it would be

    ;with cte as (select top 100 ASP.Articel AS [Product Name], ASPD.English AS Ozellik, ASP.English  
          
     from ArticelSpec ASP  
          
     inner join ArticelSpecDefinition ASPD on ASP.ArticelSpecDefinition = ASPD.Oid  
          
     where Articel='176E0AC3-1874-4801-BEB8-16E9B2F7D1FD'  order by ASP.Articel)  
      
    select * from cte PIVOT (max(English) FOR Ozelik IN ([Number of Pockets], [Sleeve Detail], ...)) pvt  
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-07-21T12:35:41.857+00:00

    What is your desired output? I believe you're looking at dynamic PIVOT for your problem. Search for 'dynamic pivot' for solution implementation. This assumes that Ozelik colum values are less than 128 characters.

    0 comments No comments

  2. Ali Yılmaz 81 Reputation points
    2022-07-21T12:57:49.65+00:00

    Hi,

    This is the output I wanted. Can you make an example by looking at the table and the code?

    223166-image.png

    0 comments No comments

  3. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2022-07-21T19:30:53.853+00:00
    select ASP.Articel AS [Product Name],   
    max(Case when ASPD.English ='NUMBER OF POCKETS' then  ASP.English  end ) as [NUMBER OF POCKETS],   
    max(Case when ASPD.English ='SLEEVE DETAIL' then  ASP.English  end ) as [SLEEVE DETAIL],   
    max(Case when ASPD.English ='PRODUCT WEIGHT' then  ASP.English  end ) [PRODUCT WEIGHT],   
    max(Case when ASPD.English ='WARRANTY' then  ASP.English  end ) [Number of Pockets],   
    max(Case when ASPD.English ='EXTRA PRODUCT DETAIL 1' then  ASP.English  end ) [EXTRA PRODUCT DETAIL 1]    
    --.....  
    from ArticelSpec ASP          
    inner join ArticelSpecDefinition ASPD on ASP.ArticelSpecDefinition = ASPD.Oid  
    where Articel='176E0AC3-1874-4801-BEB8-16E9B2F7D1FD'  
    Group by ASP.Articel  
    
    0 comments No comments

  4. LiHong-MSFT 10,061 Reputation points
    2022-07-22T01:54:55.21+00:00

    Hi @Ali Yılmaz
    For static PIVOT query, please refer to Naomi and JingyangLi's answers which you need to type column names manually.
    While in this issue, you could try dynamic PIVOT if there are too many columns you want to display.
    Check this:

    DECLARE @sql_str VARCHAR(MAX)  
    DECLARE @spread_elements VARCHAR(MAX)  
    DECLARE @Article VARCHAR(MAX)  
    SET @Article = '176E0AC3-1874-4801-BEB8-16E9B2F7D1FD'   
      
    SELECT @spread_elements = ISNULL(@spread_elements + ',','') + QUOTENAME(ASPD.English)   
    FROM ArticelSpec ASP inner join ArticelSpecDefinition ASPD ON ASP.ArticelSpecDefinition = ASPD.Oid  
    WHERE Articel=@Article  
    GROUP BY ASPD.English  
    --PRINT @spread_elements  
      
    SET @sql_str = '  
    SELECT * FROM   
    (  
     select top 100 ASP.Articel AS [Product Name], ASPD.English AS Ozellik, ASP.English  
     from ArticelSpec ASP inner join ArticelSpecDefinition ASPD on ASP.ArticelSpecDefinition = ASPD.Oid  
     where Articel='+@Article+'  
    )S   
    PIVOT (MAX(English) FOR Ozellik IN ( '+ @spread_elements +') ) AS P   
    ORDER BY [Product Name]  
    '  
    --PRINT (@sql_str)  
    EXEC (@sql_str)  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.