我正在尝试为我们的ERP系统中的一些校准工具创建报告。 我对SSRS相当陌生,不确定如何组织它。
我们希望有6列:
PART_ID, Description, Calibration_Required, Calibrated_Date, Next_Calibration, Employee
此信息位于表的4列中,并由字段ID定义:
Part_ID
Description
Date_Val
String_Val
Field_ID
00004 = Calibrated Date From Date_Val
00005 = Next Calibration From Date_Val
00006 = Employee From String_Val
00007 = Calibration Required From String_Val
我希望每一行都在其自己的列中,并且只将零件列出一次,而不是每个Field_ID列出4次。
答案 0 :(得分:0)
我使用了common table expression CTE到pivot记录集。为了模拟数据,我使用了mockaroo。
WITH
field_descriptions
AS
(
SELECT tbl.* FROM (VALUES
( '00004', 'Calibrated Date')
, ( '00005', 'Next Calibration')
, ( '00006', 'Employee')
, ( '00007', 'Calibration Required')
) tbl ([Field_ID], [Field_Description])
)
,
part_data
AS
(
SELECT tbl.* FROM (VALUES
( 10001, '00004', 'NSX', '26-Jan-2019', NULL)
, ( 10001, '00005', 'NSX', '23-Aug-2018', NULL)
, ( 10001, '00006', 'NSX', NULL, 'aludlom1')
, ( 10001, '00007', 'NSX', NULL, 'tweek0')
, ( 10002, '00004', 'S2000', '19-Aug-2018', NULL)
, ( 10002, '00005', 'S2000', '31-Aug-2018', NULL)
, ( 10002, '00006', 'S2000', NULL, 'mmiskelly7')
, ( 10002, '00007', 'S2000', NULL, 'btwitty2')
, ( 10003, '00004', 'LHS', '15-Jun-2018', NULL)
, ( 10003, '00005', 'LHS', '16-Sep-2018', NULL)
, ( 10003, '00006', 'LHS', NULL, 'ceronief')
, ( 10003, '00007', 'LHS', NULL, 'rmccaughran5')
, ( 10004, '00004', 'Corvette', '07-Jul-2018', NULL)
, ( 10004, '00005', 'Corvette', '24-Oct-2018', NULL)
, ( 10004, '00006', 'Corvette', NULL, 'jgodonh')
, ( 10004, '00007', 'Corvette', NULL, 'rbrayley8')
, ( 10005, '00004', 'Savana', '14-Oct-2018', NULL)
, ( 10005, '00005', 'Savana', '08-Feb-2018', NULL)
, ( 10005, '00006', 'Savana', NULL, 'dwilsoni')
, ( 10005, '00007', 'Savana', NULL, 'sdunnan9')
, ( 10006, '00004', 'Z4 M', '01-Jan-2019', NULL)
, ( 10006, '00005', 'Z4 M', '07-Dec-2018', NULL)
, ( 10006, '00006', 'Z4 M', NULL, 'ncopestickt')
, ( 10006, '00007', 'Z4 M', NULL, 'tskahilla')
, ( 10007, '00004', 'M-Class', '11-Aug-2018', NULL)
, ( 10007, '00005', 'M-Class', '02-May-2018', NULL)
, ( 10007, '00006', 'M-Class', NULL, 'bbamlingx')
, ( 10007, '00007', 'M-Class', NULL, 'ddunkb')
, ( 10008, '00004', 'Neon', '31-Oct-2018', NULL)
, ( 10008, '00005', 'Neon', '16-Jul-2018', NULL)
, ( 10008, '00006', 'Neon', NULL, 'vtocknell11')
, ( 10008, '00007', 'Neon', NULL, 'thagstonk')
, ( 10009, '00004', 'Celica', '05-Sep-2018', NULL)
, ( 10009, '00005', 'Celica', '16-Mar-2018', NULL)
, ( 10009, '00006', 'Celica', NULL, 'fwudeland1a')
, ( 10009, '00007', 'Celica', NULL, 'wjennionsp')
, ( 10010, '00004', 'Savana', '09-Oct-2018', NULL)
, ( 10010, '00005', 'Savana', '05-Jul-2018', NULL)
, ( 10010, '00006', 'Savana', NULL, 'dtrevena1c')
, ( 10010, '00007', 'Savana', NULL, 'dcaseror')
) tbl ([Part_ID], [Field_ID], [Description], [Date_Val], [String_Val])
)
,
part_data_udf
AS
(
SELECT
pd.[Part_ID]
, pd.[Field_ID]
, fd.[Field_Description]
, pd.[Description]
, pd.[Date_Val]
, pd.[String_Val]
, [udf_field] = COALESCE(REPLACE(CONVERT(VARCHAR(11), pd.[Date_Val], 113), ' ', '-'), pd.[String_Val])
FROM
part_data AS pd
INNER JOIN field_descriptions AS fd ON pd.[Field_ID] = fd.[Field_ID]
)
SELECT
[Part_ID]
, [Description]
, [Calibrated Date]
, [Next Calibration]
, [Employee]
, [Calibration Required]
FROM
(
SELECT
pd.[Part_ID]
, pd.[Field_Description]
, pd.[Description]
, pd.[udf_field]
FROM
part_data_udf pd
) pL
PIVOT
(
MAX([udf_field])
FOR [Field_Description]
IN
(
[Calibrated Date]
, [Next Calibration]
, [Employee]
, [Calibration Required]
)
) AS pvt
ORDER BY [Part_ID]