使用日期作为列的SQL数据透视表

时间:2017-04-27 20:36:38

标签: sql sql-server-2008 tsql

我如何获得此表格: enter image description here 看起来像这样:enter image description here 我已尝试过枢轴,但到目前为止无法使用,猜测因为ItemID列是varchar列。 以下是我到目前为止的样本数据:

    IF OBJECT_ID ('tempdb..#TempSupplierDemands') IS NOT NULL
  DROP TABLE #TempSupplierDemands

  DECLARE @VEN varchar(10)
  SET @VEN = '10161';

  DECLARE @VenRecordID as VARCHAR(MAX)
  SET @VenRecordID = 
  (SELECT VEN_RecordID
  FROM VEN
  WHERE VEN_VENDORID = @VEN)

  CREATE TABLE #TempSupplierDemands(
  VendorID NVARCHAR(100),
  ItemID VARCHAR(MAX),
  ItemDescription VARCHAR(MAX),
  MondayWeekStart DATETIME,
  SDM_DemandDate DATETIME,
  SDM_DemandWeek INT)

  INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000940','CONN ROD/BRG ASSY, 21 CFM','2017-03-20 00:00:00.000','2017-03-24 00:00:00.000','12');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000508','PLATE, OIL PUMP ASSY W/OPS','2017-03-27 00:00:00.000','2017-03-29 00:00:00.000','13');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000198','CRANKCASE, ASSY, 3 CYL REED','2017-03-27 00:00:00.000','2017-03-29 00:00:00.000','13');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000374','CONNECTING ROD MACH, 24 CFM 116 LG','2017-03-27 00:00:00.000','2017-03-29 00:00:00.000','13');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','06DA507674','TERM PLATE, 5 PIN','2017-03-27 00:00:00.000','2017-03-29 00:00:00.000','13');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','06DA405234','CONNECTING ROD ASSY (4,6 CYL)','2017-04-10 00:00:00.000','2017-04-10 00:00:00.000','15');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','06DA405234','CONNECTING ROD ASSY (4,6 CYL)','2017-04-10 00:00:00.000','2017-04-11 00:00:00.000','15');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1TU0502','MOTOR CASE - MACHINED','2017-04-10 00:00:00.000','2017-04-13 00:00:00.000','15');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1TA0915D','CASING, OUTLET(4.0/2.8)','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB002222','MOTOR CASE MACHINING, FRAME 2','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003224','OUTLET CASE MACHINING','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003226','IRA BEARING COVER MACHINING','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003228','IRA DISCHARGE COVER MACHINING','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003205','ROTOR CASE MACHINING','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003126','IRA BEARING COVER MACHINING','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1TA0915D','CASING, OUTLET(4.0/2.8)','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003128','IRA DISCHARGE COVER MACHINING','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000508','PLATE, OIL PUMP ASSY W/OPS','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000198','CRANKCASE, ASSY, 3 CYL REED','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','0AMB000374','CONNECTING ROD MACH, 24 CFM 116 LG','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003105','ROTOR CASE MACHINING','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');
INSERT INTO #TempSupplierDemands (VendorID,ItemID,ItemDescription,MondayWeekStart,SDM_DemandDate,SDM_DemandWeek) VALUES ('10161','1BZB003122','MOTOR COVER MACHINING','2017-04-17 00:00:00.000','2017-04-19 00:00:00.000','16');

declare @cols varchar(max)


SELECT @cols = COALESCE (@cols + ',[' + CONVERT(NVARCHAR, [MondayWeekStart], 106) + ']', 
               '[' + CONVERT(NVARCHAR, [MondayWeekStart], 106) + ']')
               FROM    (SELECT DISTINCT [MondayWeekStart] FROM #TempSupplierDemands) PV  
               ORDER BY [MondayWeekStart]

declare @query varchar(max) 

select @query = 'select *
from 
(
  select VendorID,ItemID, ItemDescription ,MondayWeekStart
  from #TempSupplierDemands
) src
pivot
(
  COUNT(ItemID)
  for MondayWeekStart in (' + @cols + ')
) piv;'


execute(@query)

1 个答案:

答案 0 :(得分:1)

我使用了图片中的输入,我使用动态sql解决了它。希望你能清楚。

CREATE TABLE #T (ItemID VARCHAR(255), ItemDesc VARCHAR(255), MondayWeekStart DATE, SDM_DemandDate DATETIME, SDM_DemandWeek INT)
INSERT INTO #T VALUES
('0AMB000940','CONN ROD/BRG ASSY, 21 CFM','2017-03-27 00:00:00.000','2017-03-31 00:00:00.000','12'),
('0AMB000508','PLATE, OIL PUMP ASSY W/OPS','2017-04-03 00:00:00.000','2017-04-05 00:00:00.000','13'),
('0AMB000198','CRANKCASE, ASSY, 3 CYL REED','2017-04-03 00:00:00.000','2017-04-05 00:00:00.000','13'),
('0AMB000374','CONNECTING ROD MACH, 24 CFM 116 LG','2017-04-03 00:00:00.000','2017-04-05 00:00:00.000','13'),
('06DA507674','TERM PLATE, 5 PIN','2017-04-03 00:00:00.000','2017-04-05 00:00:00.000','13'),
('06DA405234','CONNECTING ROD ASSY (4,6 CYL)','2017-04-17 00:00:00.000','2017-04-17 00:00:00.000','15'),
('06DA405234','CONNECTING ROD ASSY (4,6 CYL)','2017-04-17 00:00:00.000','2017-04-18 00:00:00.000','15'),
('1TU0502','MOTOR CASE - MACHINED','2017-04-17 00:00:00.000','2017-04-20 00:00:00.000','15'),
('1TA0915D','CASING, OUTLET(4.0/2.8)','2017-04-24 00:00:00.000','2017-04-25 00:00:00.000','16'),
('1BZB002222','MOTOR CASE MACHINING, FRAME 2','2017-04-24 00:00:00.000','2017-04-25 00:00:00.000','16'),
('1BZB003224','OUTLET CASE MACHINING','2017-04-24 00:00:00.000','2017-04-25 00:00:00.000','16'),
('1BZB003226','IRA BEARING COVER MACHINING','2017-04-24 00:00:00.000','2017-04-25 00:00:00.000','16'),
('1BZB003228','IRA DISCHARGE COVER MACHINING','2017-04-24 00:00:00.000','2017-04-25 00:00:00.000','16'),
('1BZB003205','ROTOR CASE MACHINING','2017-04-24 00:00:00.000','2017-04-25 00:00:00.000','16'),
('1BZB003126','IRA BEARING COVER MACHINING','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('1TA0915D','CASING, OUTLET(4.0/2.8)','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('1BZB003128','IRA DISCHARGE COVER MACHINING','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('0AMB000508','PLATE, OIL PUMP ASSY W/OPS','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('0AMB000198','CRANKCASE, ASSY, 3 CYL REED','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('0AMB000374','CONNECTING ROD MACH, 24 CFM 116 LG','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('1BZB003105','ROTOR CASE MACHINING','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16'),
('1BZB003122','MOTOR COVER MACHINING','2017-04-24 00:00:00.000','2017-04-26 00:00:00.000','16')

CREATE TABLE #W  (WeekStart DATE)
INSERT INTO #W VALUES
('20170313'),('20170320'),('20170327'),('20170403'),('20170410'),('20170417'),('20170424'),('20170501')

DECLARE @StartDate DATE, @EndDate DATE
SET @StartDate = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) --Monday of current week
SET @EndDate = (SELECT MAX(WeekStart) FROM #W)

DECLARE @Sql NVARCHAR(MAX), @PvtColumns VARCHAR(MAX), @IsNullPvtColumns AS VARCHAR(MAX)

SET @PvtColumns = substring(
                    (
                        Select ',['+ CAST(w.WeekStart AS VARCHAR(255)) + ']' AS [text()]
                        From #W AS w
                        Where w.WeekStart >= @StartDate AND w.WeekStart <= @EndDate
                        For XML PATH ('')
                    ), 2, 4000)

SET @IsNullPvtColumns = substring(
                    (
                        Select ',ISNULL(['+ CAST(w.WeekStart AS VARCHAR(255)) + '], 0) AS [' + CAST(w.WeekStart AS VARCHAR(255)) + '] ' AS [text()]
                        From #W AS w
                        Where w.WeekStart >= @StartDate AND w.WeekStart <= @EndDate
                        For XML PATH ('')
                    ), 2, 4000)

SET @Sql = 'SELECT pvt.ItemId, pvt.ItemDesc, ISNULL(bkl.Backlog, 0) AS Backlog, ' + @IsNullPvtColumns + ' FROM ( '
SET @Sql = @Sql + 'SELECT ItemId, ItemDesc, w.WeekStart, SDM_DemandWeek FROM ' 
SET @Sql = @Sql + '(SELECT WeekStart FROM #W WHERE WeekStart >= '''+ CAST(@StartDate AS VARCHAR(255)) + ''' AND WeekStart <= '''+ CAST(@EndDate AS VARCHAR(255))+''') AS w  LEFT OUTER JOIN #T AS t ON w.WeekStart = t.MondayWeekStart ) AS r '
SET @Sql = @Sql + 'PIVOT ' 
SET @Sql = @Sql + '(SUM(SDM_DemandWeek) FOR r.WeekStart IN ('+ @PvtColumns +')'
SET @Sql = @Sql + ') AS pvt '
SET @Sql = @Sql + 'LEFT OUTER JOIN (SELECT ItemId, SUM(SDM_DemandWeek) AS Backlog FROM #T WHERE SDM_DemandDate < '''+ CAST(@StartDate AS VARCHAR(255)) + ''' GROUP BY ItemID, ItemDesc) AS bkl '
SET @Sql = @Sql + 'ON bkl.ItemId = pvt.ItemId '
SET @Sql = @Sql + 'WHERE pvt.ItemId IS NOT NULL '
SET @Sql = @Sql + 'ORDER BY pvt.ItemID '

EXEC sp_executesql @sql

结果

ItemId      ItemDesc                            Backlog  2017-04-24  2017-05-01
0AMB000198  CRANKCASE, ASSY, 3 CYL REED         13       16          0
0AMB000374  CONNECTING ROD MACH, 24 CFM 116 LG  13       16          0
0AMB000508  PLATE, OIL PUMP ASSY W/OPS          13       16          0
1BZB002222  MOTOR CASE MACHINING, FRAME 2       0        16          0
1BZB003105  ROTOR CASE MACHINING                0        16          0
1BZB003122  MOTOR COVER MACHINING               0        16          0
1BZB003126  IRA BEARING COVER MACHINING         0        16          0
1BZB003128  IRA DISCHARGE COVER MACHINING       0        16          0
1BZB003205  ROTOR CASE MACHINING                0        16          0
1BZB003224  OUTLET CASE MACHINING               0        16          0
1BZB003226  IRA BEARING COVER MACHINING         0        16          0
1BZB003228  IRA DISCHARGE COVER MACHINING       0        16          0
1TA0915D    CASING, OUTLET(4.0/2.8)             0        32          0