I got a pile of records I need to get so I can throw them in a pivot table in Excel. I'm not sure the best way to do it.
I have a view that gives me Ctrl_Job_number, JobNumber, Post_date, Cost, Billed, and PM. When I put it into a pivot table, I use group by to sum up the cost and billed but still get a lot of records due to all the post dates.
I import that into Excel and use Excel's group by to set up by Year, Quarter and month. The results are like so, with indentations being aggregates. PM has ctrlJobs, ctrlJobs have jobs, all roll up to the PM:
Jan Feb
PM Cost Billed Cost Billed
Bob 500 1000 200 400
CtrJobNum1 100 200 100 200
Jobnum1a 50 100 100 200
JobNum1b 50 100 0 0
CtrlJobNum2 400 800 100 200
JobNum2a 400 800 100 200
Jim
CtrlJobNum3
etc.. etc...
That all works fine. Excel can group my dates nicely so even though I get rawer data, it looks fine. My issue is with getting a count of the subjobs. The raw data could have several lines of the subjobs.
Take the above example, you may have 10 entries for JobNum2a at 40 bucks apiece that get rolled up to the 400 seen in the excel. But a count will give me 15 when I only have one. If I remove the postdate, then I rollup everything just fine in the initial query but I lose the ability to group by month, quarter, and year.
So, How can I group and aggregate these multiple rows and get an accurate count of the unique jobnumbers?
Here's a basic query I'm running:
I removed the PM to simplify it a bit (lotta joins). The heiarchy is Job_code then JCDT_JOB_CODE. JCDT_JOB_CODE is the one I'm trying to get an accurate count of. Count distinct doesn't work since there are many entries and I still end up with dups that mess up the count.
Thanks in advance!
I have a view that gives me Ctrl_Job_number, JobNumber, Post_date, Cost, Billed, and PM. When I put it into a pivot table, I use group by to sum up the cost and billed but still get a lot of records due to all the post dates.
I import that into Excel and use Excel's group by to set up by Year, Quarter and month. The results are like so, with indentations being aggregates. PM has ctrlJobs, ctrlJobs have jobs, all roll up to the PM:
Jan Feb
PM Cost Billed Cost Billed
Bob 500 1000 200 400
CtrJobNum1 100 200 100 200
Jobnum1a 50 100 100 200
JobNum1b 50 100 0 0
CtrlJobNum2 400 800 100 200
JobNum2a 400 800 100 200
Jim
CtrlJobNum3
etc.. etc...
That all works fine. Excel can group my dates nicely so even though I get rawer data, it looks fine. My issue is with getting a count of the subjobs. The raw data could have several lines of the subjobs.
Take the above example, you may have 10 entries for JobNum2a at 40 bucks apiece that get rolled up to the 400 seen in the excel. But a count will give me 15 when I only have one. If I remove the postdate, then I rollup everything just fine in the initial query but I lose the ability to group by month, quarter, and year.
So, How can I group and aggregate these multiple rows and get an accurate count of the unique jobnumbers?
Here's a basic query I'm running:
Code:
SELECT Job_Code, JCDT_POST_DATE, SUM(CASE vw_CMIC_Ctrl_Job_Rollup.JCDT_TYPE_CODE WHEN 'C' THEN vw_CMIC_Ctrl_Job_Rollup.JCDT_AMT ELSE 0 END)
AS Costs, SUM(CASE vw_CMIC_Ctrl_Job_Rollup.JCDT_TYPE_CODE WHEN 'B' THEN vw_CMIC_Ctrl_Job_Rollup.JCDT_AMT ELSE 0 END) AS Billed,
JCDT_JOB_CODE
FROM dbo.vw_CMIC_Ctrl_Job_Rollup
WHERE (JCDT_COMP_CODE = 'WN')
GROUP BY Job_Code, JCDT_POST_DATE, JCDT_JOB_CODE
I removed the PM to simplify it a bit (lotta joins). The heiarchy is Job_code then JCDT_JOB_CODE. JCDT_JOB_CODE is the one I'm trying to get an accurate count of. Count distinct doesn't work since there are many entries and I still end up with dups that mess up the count.
Thanks in advance!