| Author |
Topic |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-04-17 : 01:37:51
|
| create table T1( empID int, leave int)create table T2( empid int, workdate datetime, leave char, paid char )insert into T1select 1, 3 union allselect 2, 4insert into T2 (empid, workdate, leave)select 1, '20060101','Y' union allselect 1, '20060102','Y' union allselect 1, '20060103','Y' union allselect 1, '20060104','N' union allselect 1, '20060105','N' union allselect 1, '20060106','N' union allselect 1, '20060107','N' union allselect 1, '20060108','N' union allselect 1, '20060109','Y' union allselect 1, '20060110','Y' union allselect 1, '20060111','Y' union allselect 1, '20060112','N' union allselect 1, '20060113','N' union all--------------------------------------select 2, '20060101','Y' union allselect 2, '20060102','Y' union allselect 2, '20060103','N' union allselect 2, '20060104','Y' union allselect 2, '20060105','Y' union allselect 2, '20060106','N' union allselect 2, '20060107','Y' union allselect 2, '20060108','N'Hi all, i would like to know about the solution for my problem.Using the tables defined above, i actually want to put information whether it is paid leave or unpaid leave.required output : 1, '20060101','Y', 'paid' -- T1.leave -= 1 -> 2 1, '20060102','Y', 'paid' -- T1.leave -= 1 -> 1 1, '20060103','Y', 'paid' -- T1.leave -= 1 -> 0 1, '20060104','N', 1, '20060105','N', 1, '20060106','N', 1, '20060107','N', 1, '20060108','N', 1, '20060109','Y', 'unpaid' 1, '20060110','Y', 'unpaid' 1, '20060111','Y', 'unpaid' 1, '20060112','N', 1, '20060113','N', -------------------------------------- 2, '20060101','Y', 'paid' -- T1.leave -= 1 -> 3 2, '20060102','Y', 'paid' -- T1.leave -= 1 -> 2 2, '20060103','N', 2, '20060104','Y', 'paid' -- T1.leave -= 1 -> 1 2, '20060105','Y', 'paid' -- T1.leave -= 1 -> 0 2, '20060106','N', 2, '20060107','Y', 'unpaid' 2, '20060108','N'anyone? thx for ur kind attention.... sql is fun... |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-04-18 : 05:55:04
|
Hi erwine,Here's one way which seems to give what you're asking...  --dataif object_id('tempdb.dbo.#t1') is not null drop table #T1create table #T1(empID int,leave int)if object_id('tempdb.dbo.#t2') is not null drop table #T2create table #T2(empid int,workdate datetime,leave char,paid char(6))insert into #T1select 1, 3 union allselect 2, 4insert into #T2 (empid, workdate, leave)select 1, '20060101','Y' union allselect 1, '20060102','Y' union allselect 1, '20060103','Y' union allselect 1, '20060104','N' union allselect 1, '20060105','N' union allselect 1, '20060106','N' union allselect 1, '20060107','N' union allselect 1, '20060108','N' union allselect 1, '20060109','Y' union allselect 1, '20060110','Y' union allselect 1, '20060111','Y' union allselect 1, '20060112','N' union allselect 1, '20060113','N' union all--------------------------------------select 2, '20060101','Y' union allselect 2, '20060102','Y' union allselect 2, '20060103','N' union allselect 2, '20060104','Y' union allselect 2, '20060105','Y' union allselect 2, '20060106','N' union allselect 2, '20060107','Y' union allselect 2, '20060108','N'--calculationupdate t2 set paid = 'paid'from #t1 t1 inner join #t2 t2 on t1.empid = t2.empid and t2.leave = 'Y'where (select count(*) from #t2 where empid = t2.empid and leave = 'Y' and workdate < t2.workdate) < t1.leaveupdate #t2 set paid = 'unpaid' where leave = 'Y' and paid is null--resultsselect * from #t2Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|