首页 > 代码库 > 两张表行转列案例

两张表行转列案例

  B表

 

  A表

 

  SELECT PV.*  FROM (          SELECT                    b.DeliverLineType,                    b.DeliverLineTypeName,                    convert(varchar,CoverageRangeStart)+-+convert(varchar,CoverageRangeEnd) AS CoverageRange,                    b.SpecialRegionFactor,                    a.Charging,                    convert(varchar,WeightRangeStart)+-+convert(varchar,WeightRangeEnd) AS WeightRange,                    a.Inputer,                    a.StartTime,                    a.EndTime          FROM [dbo].[cfg_DeliverLineTypePrice] a                    INNER JOIN [cfg_DeliverLineType] b ON a.DeliverLineType=b.DeliverLineType        ) TBL            PIVOT (MAX(TBL.Charging) FOR WeightRange IN ([0-1000], [1001-2000],[2001-3000],[3001-5000],[5001-100000])) PV 


最终查询效果图,将A表中的数据根据DeliverLineType行转列.