How do I select a variable row in SQL Server?

后端 未结 3 1487
时光说笑
时光说笑 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:26

    This can be done with standard SQL, albeit a bit ugly:

    select i.item, 
           i.year, 
           i.model,
           case i.model 
               when 'MODELA' then p.MODELA 
               when 'MODELB' then p.MODELB 
               when 'MODELC' then p.MODELC 
               when 'MODELD' then p.MODELD 
           end as price
    from Items i INNER JOIN Pricing p ON p.year = i.year
    

    Rextexter demo

提交回复
热议问题