I'm working with a query that deals with imported data. As it is imported, it can go through a number of processes along the way. I'm trying to build a query that will show the progress of each of the imported records and the status of the processes. There could be one process, there could be two, or three... Naturally all of the data is scattered across multiple tables... I can get the results in row-fashion, but now I'm trying to flatten it out into columns, so that for one imported record, I get one row, with the process statuses going across...
This illustrates the problem I'm having:
Code:
create table #RECORDSTATUS (
RECORDID uniqueidentifier,
IMPORTPROCESS nvarchar(50),
IMPORTDESCRIPTION nvarchar(200),
[FILENAME] nvarchar(50),
PROCESSNUMBER nvarchar(10),
PROCESSDESCRIPTION nvarchar(200),
PROCESSSTATUSCODE int,
PROCESSSTATUS nvarchar(50),
WORKFLOWSTATE nvarchar(200),
PROCESSCREATEDATE datetime
)
insert into #RECORDSTATUS
select
'DF918365-1026-4B81-987E-FFA4D00C1F92', 'Contact processing', '', 'GBIImportSample005.csv', '25', 'Created by the Contact processing.', 1, 'Committed', 'Open', '2015-12-30'
union all select
'5BCC958C-F153-4D6D-8287-7CF3D7CE19F8', 'Contact processing', '', 'GBIImportSample005.csv', '25', 'Created by the Contact processing.', 1, 'Committed', 'Open', '2015-12-30'
union all select
'8CAE521B-6FD1-48D6-BF3F-34D768158A25', 'Contact processing', '', 'GBIImportSample005.csv', '25', 'Created by the Contact processing.', 1, 'Committed', 'Open', '2015-12-30'
union all select
'5D494BBC-2E03-4CDF-9445-05C4B7DA20E8', 'Contact processing', '', 'GBIImportSample005.csv', '25', 'Created by the Contact processing.', 1, 'Committed', 'Open', '2015-12-30'
union all select
'73D20D31-6BC8-40FB-8657-5EB0FE79E90C', 'Contact processing', '', 'GBIImportSample005.csv', '25', 'Created by the Contact processing.', 1, 'Committed', 'Open', '2015-12-30'
union all select
'DF918365-1026-4B81-987E-FFA4D00C1F92', 'Invoice processing', '', 'GBIImportSample005.csv', '47', 'Created by Invoice processing.', 0, 'Uncommitted', 'Open', '2015-12-30'
union all select
'5BCC958C-F153-4D6D-8287-7CF3D7CE19F8', 'Invoice processing', '', 'GBIImportSample005.csv', '47', 'Created by Invoice processing.', 0, 'Uncommitted', 'Open', '2015-12-30'
union all select
'8CAE521B-6FD1-48D6-BF3F-34D768158A25', 'Invoice processing', '', 'GBIImportSample005.csv', '47', 'Created by Invoice processing.', 0, 'Uncommitted', 'Open', '2015-12-30'
union all select
'5D494BBC-2E03-4CDF-9445-05C4B7DA20E8', 'Invoice processing', '', 'GBIImportSample005.csv', '47', 'Created by Invoice processing.', 0, 'Uncommitted', 'Open', '2015-12-30'
union all select
'73D20D31-6BC8-40FB-8657-5EB0FE79E90C', 'Invoice processing', '', 'GBIImportSample005.csv', '47', 'Created by Invoice processing.', 0, 'Uncommitted', 'Open', '2015-12-30'
; with IMPORTSTATUS as (
select
row_number() over (partition by RS.RECORDID order by RS.RECORDID, RS.PROCESSCREATEDATE) as ImportPivot,
row_number() over (partition by RS.RECORDID order by RS.RECORDID, RS.PROCESSCREATEDATE) as ProcessPivot,
row_number() over (partition by RS.RECORDID order by RS.RECORDID, RS.PROCESSCREATEDATE) as StatusPivot,
RS.RECORDID,
RS.IMPORTPROCESS,
--RS.IMPORTDESCRIPTION,
--RS.[FILENAME],
RS.PROCESSNUMBER,
--RS.PROCESSDESCRPTION,
RS.PROCESSSTATUS
--RS.PROCESSWORKFLOWSTATE
from #RECORDSTATUS RS
)
select RECORDID,
IMPORT1 = ([1]),
PROCESSNUMBER1 = ([2]),
PROCESSSTATUS1 = ([3]),
IMPORT2 = ([4]),
PROCESSNUMBER2 = ([5]),
PROCESSSTATUS2 = ([6]) --,
--IMPORT3 = max([7]),
--PROCESSNUMBER3 = max([8]),
--PROCESSSTATUS3 = max([9]),
--IMPORT4 = max([10]),
--PROCESSNUMBER4 = max([11]),
--PROCESSSTATUS4 = max([12]),
--IMPORT5 = max([13]),
--PROCESSNUMBER5 = max([14]),
--PROCESSSTATUS5 = max([15])
from IMPORTSTATUS I
pivot (max(IMPORTPROCESS) for ImportPivot in ([1], [4])) as pvt1 --, [7], [10], [13])) as pvt1
pivot (max(PROCESSNUMBER) for ProcessPivot in ([2], [5])) as pvt2 -- , [8], [11], [14])) as pvt2
pivot (max(PROCESSSTATUS) for StatusPivot in ([3], [6])) as pvt3 -- , [9], [12], [15])) as pvt3
drop table #RECORDSTATUS
Right now that gives me the following results:
RECORDID IMPORT1 PROCESSNUMBER1 PROCESSSTATUS1 IMPORT2 PROCESSNUMBER2 PROCESSSTATUS2
5D494BBC-2E03-4CDF-9445-05C4B7DA20E8 NULL 47 NULL NULL NULL NULL
5D494BBC-2E03-4CDF-9445-05C4B7DA20E8 Contact processing NULL NULL NULL NULL NULL
8CAE521B-6FD1-48D6-BF3F-34D768158A25 NULL 47 NULL NULL NULL NULL
8CAE521B-6FD1-48D6-BF3F-34D768158A25 Contact processing NULL NULL NULL NULL NULL
73D20D31-6BC8-40FB-8657-5EB0FE79E90C NULL 25 NULL NULL NULL NULL
73D20D31-6BC8-40FB-8657-5EB0FE79E90C Invoice processing NULL NULL NULL NULL NULL
5BCC958C-F153-4D6D-8287-7CF3D7CE19F8 NULL 47 NULL NULL NULL NULL
5BCC958C-F153-4D6D-8287-7CF3D7CE19F8 Contact processing NULL NULL NULL NULL NULL
DF918365-1026-4B81-987E-FFA4D00C1F92 NULL 47 NULL NULL NULL NULL
DF918365-1026-4B81-987E-FFA4D00C1F92 Contact processing NULL NULL NULL NULL NULL
(ungh - I tried to get the output to display somewhat nicely but the forum doesn't want to play.)
There's two issues right out of the gate here... Import 2 isn't filling in with the correct process - it should have "Invoice processing" in it... and the status doesn't come over at all....
What I'm looking for is to have all of the data flattened out into one row... so that the *1 fields show the import, process, and status for "Contact processing" and the *2 fields show the import, process, & status for "Invoice processing" ... eventually I'd like to be able to show up to 5 groupings
The temp table in the above example is actually a CTE ... I'm using the temp table here for simplicity of the example ... but the fields and types of the #table are exactly what the CTE produces.
Import1 should have "Contact processing", ProcessNumber1 should be "47" and ProcessStatus1 should be "Committed"
Import2 should have "Invoice processing", ProcessNumber2 should be "25" and ProcessStatus1 should be "Uncommitted"
I know that I'll need to do another aggregate on the fields and group by the RecordID... but before I can even do that, I need to get the pivot working correctly.
What.
Am.
I.
Missing?
-tg