Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help for building this query

Author  Topic 

nicky_river
Yak Posting Veteran

55 Posts

Posted - 2010-07-18 : 09:36:27
Hi Everyone,

I am facing a peculiar problem on how to go about building this query. The query is as given below. I would be grateful is you could provide me with the resultant query.

The O/P is as given below :-

O/P
---------------------------------------
Date Client_code salary Cumulative_sal
----------------------------------------
1/4 A 4000 4000
2/4 A 0 4000
3/4 A 0 4000
4/4 A 6000 10000
. . .
. . .
. . .
1/4 B 6000 6000
2/4 B 6000 12000
3/4 B 0 12000
4/4 B 0 12000
. . .
. . .
. . .

Original table
----------------------------------------
date Client_code salary
----------------------------------------
1/4 A 4000
1/4 B 6000
2/4 B 6000
4/4 A 6000
6/4 B 8000
6/4 A 2000

Basically, I need to generate the above o/p for each employee and for all dates in april. If there is no employee from the original table, associated with that date then both the employees salary must be 0 for that date and cumulative salary gets added with the below salary.

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-07-18 : 10:52:11
I created a sample data table for your case.
What you need to have is a date table with all the dates for your salary export (this is just a case). To populate a date table is very easy and fast.

Otherwise solution is very straightforward. It created derived / temp table with all the dates and clients, later updated all the existing salaries and calculates cumulatives.


--populate sample TABLE
create table sal
(date smalldatetime
,client_code char(1)
,salary int)

insert into sal
select '2010/4/1','A',4000 union all
select '2010/4/1','B',6000 union all
select '2010/4/2','B',6000 union all
select '2010/4/4','A',6000 union all
select '2010/4/6','B',8000 union all
select '2010/4/6','A',2000
--(6 row(s) affected)


--POPULATE DATE TABLE --WILL BE NEEDED IN FURTHER STEPS
create table tbl_date
(date smalldatetime)
insert into tbl_date
select '2010/4/1' union all
select '2010/4/2' union all
select '2010/4/3' union all
select '2010/4/4' union all
select '2010/4/5' union all
select '2010/4/6'
--(6 row(s) affected)



select
d1.date
,x.client_code
,0 as salary
into #temp
from tbl_date as d1
cross join (select distinct client_code from sal) as x
--(12 row(s) affected)

update t
set t.salary = s.salary
from #temp as t
join sal as s
on s.client_code = t.client_code and s.date = t.date
--(6 row(s) affected)


-- CUMULATIVE RESULTS WITH NULL VALUES ROWS
select
s1.date
,s1.client_code
,s1.salary
,(select sum(salary) from #temp as s2
where s2.date <= s1.date
and s1.client_code = s2.client_code) as sal_cum
from #temp as s1

order by s1.client_code, s1.date


Go to Top of Page

nicky_river
Yak Posting Veteran

55 Posts

Posted - 2010-07-19 : 04:59:33
Hi slimt_slimt,

Thanks for your quick reply. But the resultant o/p is not showing all the dates of April its just showing 1st to 6st for A and B. I require 1st to 30th dates for A and 1st to 30th dates for B, along with the salary and cumulative salary.

Awaiting your response.
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2010-07-19 : 05:20:03
"What you need to have is a date table with all the dates for your salary export (this is just a case). To populate a date table is very easy and fast."

only 6 dates in the SAMPLE extract....because slimt only filled in 6 sample dates. he left it up to you to pad out the rest.
Go to Top of Page
   

- Advertisement -