I figured this might be worth posting, as I assume I’m not the only person in the world who struggles with SQL now and again:
I needed to report some basic Work Order data from our maintenance database, and show all involved assets, technicians/time applied, and parts used for each work order. In the past, using something like Access as a front end, I could have easily dropped the asset, technician and parts data into sub-reports. There is no sub-report analogue in the Ignition Report Designer, so to prevent me from going skydiving without a parachute, one of my guys decided the best solution was to just treat them as data types.
I had already built a [color=#FF8000]SQL View[/color] for each sub-report category so we were already starting with only useful data. In an aggregating View, he used [color=#BF4080]UNION[/color] statements to bring the data that would normally be contained in a sub-report into a single [color=#FF00BF]results table[/color], adding a “[color=#4000FF]type[/color]” column to identify which sub-report the data row is part of. The sub-report data is returned with the [color=#408080]type[/color], a [color=#408080]parameter column[/color], and a [color=#408080]value[/color] column.
------- Start of horrible SQL stuff (Jimmy’s Code of Doom)
SELECT wo_num, date_completed, WO_Description, date_added, requested_by, CASE WHEN Asset_ID IS NOT NULL THEN Asset_ID WHEN Resource_Description IS NOT NULL
THEN Resource_Description WHEN KP_Part_Number IS NOT NULL THEN KP_Part_Number END AS [color=#408080]Col1[/color], CASE WHEN Asset_Description IS NOT NULL
THEN Asset_Description WHEN Resource_Actual_Hours IS NOT NULL THEN CAST(CAST(Resource_Actual_Hours AS DECIMAL(9, 1)) AS nvarchar(255)) + ’ hours’ WHEN Part_Description IS NOT NULL
THEN Part_Description END AS [color=#408080]Col2[/color], [color=#4000FF]WO_Type[/color]
FROM (SELECT wo_num, date_completed, WO_Description, date_added, requested_by, Work_Order_Type, Asset_ID, Asset_Description, NULL AS Resource_Actual_Hours, NULL AS Resource_Description, NULL
AS KP_Part_Number, NULL AS Part_Description, ‘Asset’ AS WO_Type
FROM [color=#FF8000]dbo.ignition_report_work_order_assets[/color]
[color=#BF4080]UNION[/color]
SELECT wo_num, date_completed, WO_Description, date_added, requested_by, Work_Order_Type, NULL AS Asset_ID, NULL AS Asset_Description, Resource_Actual_Hours, Resource_Description, NULL
AS KP_Part_Number, NULL AS Part_Description, ‘Resources’ AS WO_Type
FROM [color=#FF8000]dbo.ignition_report_work_order_resources[/color]
[color=#BF4080]UNION[/color]
SELECT wo_num, date_completed, WO_Description, date_added, requested_by, Work_Order_Type, NULL AS Asset_ID, NULL AS Asset_Description, NULL AS Resource_Actual_Hours, NULL
AS Resource_Description, KP_Part_Number, Part_Description, ‘Parts’ AS WO_Type
FROM [color=#FF8000]dbo.ignition_report_work_order_parts[/color]) AS [color=#FF00BF]tbl[/color]
------- End of horrible SQL stuff
The following columns are common to each work order, so they always contain data:
wo_num, date_completed, WO_Description, date_added, requested_by
The sub-report data is returned in the three new columns like this:
col1_____________col2______________WO_Type
13-0144__________EMS Light__________Asset
13-0145__________EMS Light__________Asset
001-0000_________NUT, C, TO WEXXAR__Parts
121-0005_________COIL, SOLENOID_____Parts
Brown, Standard____0.5 hours__________Resources
Once the data was in this form, building the Ignition report was a lot easier.
This has saved me a lot of frustration, so I hope it is helpful to someone else out there. It’s also why I only hire people smarter than me.