I have been tasked by the powers-that-be to sort through >49000 labor transactions for CY2009 and summarize their hours and dollars information by workcenter. I've already got a rudimentary cross-tab query hashed out (thanks to searching SQLTEAM.COM) but there are a couple tricks involved...First, each 'superjob' has potentially 9999 'subjobs' - they want the subjob data displayed in aggregate with the superjob data. No sweat, that, already done. BUT...There are 38 distinct workcenters, and not every job uses the same subset of workcenters. I don't want to display 76 columns (One for each hour and dollar sum) especially if 64 of them will always be empty. Even moreso than not wanting to display that much empty garbage, I don't want to hand-code for each!This is the query I have drawn up so far:SELECT DISTINCT(left(ladetail.fjobno, 5)) as JONO, SUM(CASE WHEN ladetail.fpro_id = 'DESIGN ' THEN (CAST(DATEDIFF(s, ladetail.fsdatetime, ladetail.fedatetime) as numeric) / 3600) ELSE 0 END) as DESIGN_HR, SUM(CASE WHEN ladetail.fpro_id = 'DESIGN ' THEN ladetail.ftotpcost ELSE 0 END) AS DESIGN$, SUM(CASE WHEN ladetail.fpro_id = 'CONTROL' THEN (CAST(DATEDIFF(s, ladetail.fsdatetime, ladetail.fedatetime) as numeric) / 3600) ELSE 0 END) AS CONTROL_HR, SUM(CASE WHEN ladetail.fpro_id = 'CONTROL' THEN ladetail.ftotpcost ELSE 0 END) AS CONTROL$ from ladetailwhere ladetail.fdate between ('2009-01-01') and ('2009-12-31') and LEFT(ladetail.fjobno, 5) in ('20386')GROUP BY LEFT(ladetail.fjobno, 5)which yieldsquote:
JONO DESIGNHR DESIGN$ CONTROLHR CONTROL$------- -------------- ------------ ---------------- --------------20386 181.50 3001.10 2824.00 57599.29