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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 another query

Author  Topic 

ch9862
Yak Posting Veteran

76 Posts

Posted - 2007-04-12 : 11:52:35
Here are my tables:
declare @st table (id int, id2 int, dte datetime)
insert into @st
select 1, 102, '03/03/2006' union all
select 2, 109, '10/03/2006' union all
select 3, 122, '05/01/2005' union all
select 4, 134, '02/23/2005'

declare @dy table (id2 int, de_id int, dte2 datetime)
insert into @dy
select 102, 33, '04/10/2007' union all
select 102, 34, '03/01/2006' union all
select 102, 34, '03/01/2006' union all
select 102, 35, '02/10/2002' union all
select 102, 36, '11/28/1998' union all
select 109, 39, '01/12/2007' union all
select 109, 40, '02/03/2007' union all
select 122, 30, '05/18/1998' union all
select 122, 32, '02/03/1994' union all
select 122, 33, '11/27/1992' union all
select 134, 15, '01/08/2005' union all
select 134, 16, '09/05/2005'



I need to select id from @st, plus de_id from @dy, where @st.id2=@dy.id2 and @dy.dte2 = max(dte2) for this particular id, while being lower than @st.dte.
There might be multiple records in @dy with the same id2, de_id and dte2, I only need one in the results.

In other words I'm expecting:
id   de_id  dte2
-----------------------
1 34 '03/01/2006' <- this is the highest dte2 for 109, lower than dte for it
2 NULL NULL <- no record in @dy with dte2 lower than dte
3 30 '05/18/1998'
4 15 '01/08/2005'


How do I write this query?

Edit: I had incorrect de_id for id=3

ch9862
Yak Posting Veteran

76 Posts

Posted - 2007-04-12 : 12:04:55
OK. I think I have the id and dte2:
select st.id, dy.dte2
from @st as st
left join (
select x.id, max(x.dte2) as dte2 from (
select distinct st1.id, d1.dte2
from @st as st1
left join @dy as d1
on st1.id2=d1.id2
where d1.dte2 < st1.dte
) as x
group by x.id
) as dy
on st.id = dy.id

How do I get de_id from that?
Go to Top of Page

ch9862
Yak Posting Veteran

76 Posts

Posted - 2007-04-12 : 13:08:08
OK. I have something that seems to work, although is pretty ugly. With emphasis on ugly, not pretty . Is it correct, and can it be simplified?
select b.id, c.de_id, b.dte2
from (
select st.id, st.id2, dyX.dte2
from @st as st
left join (
select x.id, max(x.dte2) as dte2 from (
select distinct st1.id, d1.dte2
from @st as st1
left join @dy as d1
on st1.id2=d1.id2
where d1.dte2 < st1.dte
) as x
group by x.id
) as dyX
on dyX.id=st.id
) as b
left join (
select distinct d.id2, d.dte2, e.de_id
from (
select id2, dte2
from @dy
) as d
left join @dy as e
on d.id2=e.id2 and d.dte2=e.dte2
) as c
on c.id2=b.id2 and c.dte2=b.dte2
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-12 : 15:11:12
[code]SELECT DISTINCT st.ID,
dy.de_id,
dy.dte2
FROM @st AS st
LEFT JOIN @dy AS dy ON dy.id2 = st.id2 AND dy.dte2 = (SELECT MAX(b.dte2) FROM @dy AS b WHERE b.id2 = dy.id2 AND b.dte2 <= st.dte)
ORDER BY st.ID[/code]
Peter Larsson
Helsingborg, Sweden
Go to Top of Page

ch9862
Yak Posting Veteran

76 Posts

Posted - 2007-04-12 : 16:26:26
Hey - that's embarassingly short . Thanks!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-12 : 16:34:13
You're welcome.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -