I am having some difficulties with this query and was wondering if anyone know what was wrong. The data I am working with can be setup 2 ways -
Master job only - all data on one line, perfect!
Sub Jobs and master job -
Data can be in the MJ line and the SJ line. I need to roll up all data into a single line.
First off, I added the SUM in the Where clause knowing it would fail - I'd like the query to only ignore jobs whos total contract amount is less then 300k and not skip a sub job that is smaller then 300k.
If that SUM is removed, the code sorta works - The GAP, billed_jtd and Contract sums will total just fine. The problem is that when I try and use a formula the result is close, but not quite right. Is there a way to correct this, or do some sort of subquery to get these totals and then preform the computations on the result? This will be placed into a stored procedure if that helps.
SUM(a.billed_jtd)/ SUM(a.contract_amount) * 100 'percent' -- problem line.
Thanks!
select distinct
a.company_code,
a.master_job,
b.job_description,
b.Project_Manager,
sum(a.contract_Amount) 'Contract_Amount',
cast(sum(a.billed_jtd)/ sum(a.contract_amount) * 100 as int) 'percent',
sum(a.GAP) 'GAP'
FROM Metadata a
INNER JOIN job_info b ON a.master_job = b.master_job AND a.master_job = b.job_Number
WHERE
a.Master_Job >= 2000 AND SUM(a.Contract_Amount) > 300000
AND a.Company_Code <>'004'
GROUP BY a.company_code, a.master_job, b.job_description, b.Project_Manager
Master job only - all data on one line, perfect!
Sub Jobs and master job -
Data can be in the MJ line and the SJ line. I need to roll up all data into a single line.
First off, I added the SUM in the Where clause knowing it would fail - I'd like the query to only ignore jobs whos total contract amount is less then 300k and not skip a sub job that is smaller then 300k.
If that SUM is removed, the code sorta works - The GAP, billed_jtd and Contract sums will total just fine. The problem is that when I try and use a formula the result is close, but not quite right. Is there a way to correct this, or do some sort of subquery to get these totals and then preform the computations on the result? This will be placed into a stored procedure if that helps.
SUM(a.billed_jtd)/ SUM(a.contract_amount) * 100 'percent' -- problem line.
Thanks!
select distinct
a.company_code,
a.master_job,
b.job_description,
b.Project_Manager,
sum(a.contract_Amount) 'Contract_Amount',
cast(sum(a.billed_jtd)/ sum(a.contract_amount) * 100 as int) 'percent',
sum(a.GAP) 'GAP'
FROM Metadata a
INNER JOIN job_info b ON a.master_job = b.master_job AND a.master_job = b.job_Number
WHERE
a.Master_Job >= 2000 AND SUM(a.Contract_Amount) > 300000
AND a.Company_Code <>'004'
GROUP BY a.company_code, a.master_job, b.job_description, b.Project_Manager