sql >> Base de Datos >  >> RDS >> Sqlserver

¿Cómo selecciono una fila variable en SQL Server?

DEMO

use unpivot y luego únase a su tabla de artículos.

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]

SALIDA