infinitizon
MIS
Hi all,
I am using SQL Server 2008 and I have this issue.
I have a table that has a job field, date field and a cost field. The date field holds dates that work was done on a particular job while the cost field holds the amount spent on that date (Work may be done on several days for a single job)
I want to create a select statement such that I get the sum of cost spent between 0-7days and 8-30days of opening a job in different columns.
I tried this:
It doesn't seem to work.
The idea is this: The job table contains transactions each job makes on differents days. For instance. Job 001 may have transactions on it for 20days and each day has a different cost implication. I want to know the total spent on job 001 between 1-7 days, between 8-30 days etc.
Currently, I get Zeros in the [0 - 7 days], [8 - 30 days] and [> 30 days] columns
Any help will be appreciated.
I am using SQL Server 2008 and I have this issue.
I have a table that has a job field, date field and a cost field. The date field holds dates that work was done on a particular job while the cost field holds the amount spent on that date (Work may be done on several days for a single job)
I want to create a select statement such that I get the sum of cost spent between 0-7days and 8-30days of opening a job in different columns.
I tried this:
Code:
select
[Job No_],
DATEDIFF(day, MIN([Posting Date]), MAX([Posting Date])) as [WIP Age],
SUM([Total Cost])[Total WIP cost],
[0 - 7 days]=case
when (DATEDIFF(day, MIN([Posting Date]), MAX([Posting Date])) between 1 and 7) then
SUM([Total Cost])
else 0 end,
[8 - 30 days]=case
when (DATEDIFF(day, MIN([Posting Date]), MAX([Posting Date])) between 8 and 30) then
SUM([Total Cost])
else 0 end,
[> 30 days]=case
when (DATEDIFF(day, MIN([Posting Date]), MAX([Posting Date]))> 30) then
SUM([Total Cost])
else 0 end
from [Job]
group by [Job No_]
The idea is this: The job table contains transactions each job makes on differents days. For instance. Job 001 may have transactions on it for 20days and each day has a different cost implication. I want to know the total spent on job 001 between 1-7 days, between 8-30 days etc.
Currently, I get Zeros in the [0 - 7 days], [8 - 30 days] and [> 30 days] columns
Any help will be appreciated.