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.
| Author |
Topic |
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-29 : 05:58:52
|
| Hi everyone. Hi KH. I have a complicated problem on query. I tried to solve it but i didn't get the answer.DECLARE @date1 smalldatetime, @date2 smalldatetimeSET @date1 = '1/1/2007'SET @date2 = '5/1/2007'table1 - hrEmpPaySummempdcno otpay period11 100 1/1/20071 200 2/1/20071 300 2/16/20072 400 3/1/20073 500 4/1/20073 600 5/1/2007table2 - hrEmpadjustmentsempdcno trantype amount1 A 10001 A 20002 B 30002 C 40002 C 5000Expected result: if @date1 = '1/1/2007' and @date2 = '5/1/2007'empno ot_adj amount1 otpay 1001 otpay 2001 otpay 3001 A 10001 A 20002 otpay 4002 B 30002 C 40002 C 50003 otpay 5003 otpay 600-Thanks-Ron-"A logician trying to explain logic to a programmer is like a cat trying to explain to a fish what it's like to get wet." |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-29 : 06:23:00
|
| Hi RonObviously I haven't seen what you have tried. Did you try a union? Two queries each getting part of the result set you want and then union them together. |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-29 : 06:28:06
|
| DECLARE @date1 smalldatetime, @date2 smalldatetimeSET @date1 = '1/1/2007'SET @date2 = '5/1/2007'declare @t table(empdcno int, otpay int, period1 datetime)insert @tselect 1, 100, '1/1/2007' union allselect 1, 200, '2/1/2007' union allselect 1, 300, '2/16/2007' union allselect 2, 400, '3/1/2007' union allselect 3, 500, '4/1/2007' union allselect 3, 600, '5/1/2007'declare @y table(empdcno int, trantype char(1), amount int)insert @yselect 1, 'A', 1000 union allselect 1, 'A', 2000 union allselect 2, 'B', 3000 union allselect 2, 'C', 4000 union allselect 2, 'C', 5000Select empdcno, 'Otpay' as ot_name, otpay from @t where period1 between @date1 and @date2union allSelect empdcno, trantype, amount from @yorder by 1--------------------------------------------------S.Ahamed |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-06-29 : 07:43:06
|
| Impossible to tell from the limited sample data but I suspect the second query in the uniom is meant to join to hrEmpPaySumm to filter for relevent records. |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-29 : 21:39:07
|
| thanks pbguy. i tried to use union in other way... your query is simple.But i have problem on joining the result to other tables.Where should i insert the join clause ? try to look at this.DECLARE @pDateRangeFrom smalldatetime, @pDateRangeTo smalldatetimeSET @pDateRangeFrom = '1/1/2007'SET @pDateRangeTo = '5/1/2007'Select empdcno, 'Otpay' as ot_name, otpayfrom hrEmpPaySumm awhere (period1 between @pDateRangeFrom and @pDateRangeTo) ANDotpay > 0union allSelect empdcno, trantype, amountfrom hrEmpAdjustmentswhere trandate between @pDateRangeFrom and @pDateRangeToorder by 1inner join psdatacenter d on d.dcno = a.empdcnoinner join ...inner join ...what i wanted is the result should be joined to psdatacenter, etc... without using a union. should be a pure join.showing d.fullname-Ron-"A logician trying to explain logic to a programmer is like a cat trying to explain to a fish what it's like to get wet." |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-06-30 : 01:02:10
|
| ok i have solved it...-Thanks-Ron-"A logician trying to explain logic to a programmer is like a cat trying to explain to a fish what it's like to get wet." |
 |
|
|
|
|
|
|
|