How do I select a variable row in SQL Server?

后端 未结 3 1492
时光说笑
时光说笑 2021-01-23 08:56

Frustratingly, I\'m working with a table where the row name should be a value. How can I select that row in SQL Server based on a value? I\'m aiming at something that looks like

3条回答
  •  爱一瞬间的悲伤
    2021-01-23 09:39

    DEMO

    use unpivot and then join to your item table.

    WITH cte as (
        SELECT [Year], [Model], [Price]
        FROM (SELECT [Year], [MODELA], [MODELB], [MODELC], [MODELD]
              FROM pricing) p
        UNPIVOT 
            ([Price] FOR [Model] IN 
                         ([MODELA], [MODELB], [MODELC], [MODELD])
            ) as unpvt
    ) 
    SELECT i.*, c.price
    FROM cte c
    JOIN items i 
      ON c.[Year] = i.[Year]
     AND c.[Model] = i.[model]
    

    OUTPUT

提交回复
热议问题