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 2005 Forums
 Transact-SQL (2005)
 Help to query

Author  Topic 

Erik.doe
Starting Member

6 Posts

Posted - 2007-11-15 : 14:51:32
I need to build a query that compares the date field from two tables.
The result only need to be table1.id.
In the result must the table1.id only be once

Thanks in advance
Best regards
Erik Doe

Table1
id Date
1 2-11-2007
2 4-11-2007
3 6-11-2007
4 8-11-2007
5 10-11-2007
6 15-11-2007

Table2
id table1_id Date
1 1 3-11-2007
2 1 7-11-2007
3 1 9-11-2007
4 3 7-11-2007
5 3 12-11-2007
6 5 11-11-2007

Result
table1.id
6 from table1.date = 15
3 from table2.date = 12
5 from table2.date = 11
1 from table2.date = 9
4 from table1.date = 8
2 from table1.date = 4

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-11-15 : 15:10:04
quote:
Originally posted by Erik.doe

I need to build a query that compares the date field from two tables.
The result only need to be table1.id.
In the result must the table1.id only be once



Compares and then what? You want the latest? or oldest date from table2?


Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-11-15 : 15:18:29
check if this works:

SET dateformat dmy
go

DECLARE @Table1 TABLE (id INT, Date DATETIME)
INSERT INTO @Table1
SELECT 1, '2-11-2007' UNION ALL
SELECT 2, '4-11-2007' UNION ALL
SELECT 3, '6-11-2007' UNION ALL
SELECT 4, '8-11-2007' UNION ALL
SELECT 5, '10-11-2007' UNION ALL
SELECT 6, '15-11-2007'

DECLARE @Table2 TABLE (id INT, table1_id INT, Date DATETIME)
INSERT INTO @Table2
SELECT 1, 1, '3-11-2007' UNION ALL
SELECT 2, 1, '7-11-2007' UNION ALL
SELECT 3, 1, '9-11-2007' UNION ALL
SELECT 4, 3, '7-11-2007' UNION ALL
SELECT 5, 3, '12-11-2007' UNION ALL
SELECT 6, 5, '11-11-2007'

SELECT T1.Id, Coalesce(T2.T2Date, T1.date)
FROM @Table1 T1
LEFT JOIN ( SELECT table1_id, Max(Date) T2Date
FROM @Table2
GROUP BY table1_id
) T2 ON T1.Id = T2.table1_id





Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-11-15 : 16:13:46
Maybe this, using Dinakar's data?
SELECT 
T1.Id
FROM
@Table1 AS T1
LEFT OUTER JOIN
(
SELECT table1_id, MAX(Date) AS Date
FROM @Table2
GROUP BY table1_id
) AS T2
ON T1.ID = T2.table1_ID
ORDER BY
CASE
WHEN T2.Date IS NULL THEN T1.Date
WHEN T1.Date > T2.Date THEN T1.Date
ELSE T2.Date
END DESC
Go to Top of Page

Erik.doe
Starting Member

6 Posts

Posted - 2007-11-15 : 16:33:20
Hi dinakar

This query is exactly what I was looking for.
Thank You very much.

/Erik Doe

SELECT T1.Id, Coalesce(T2.T2Date, T1.date) AS MyDate
FROM Table1 T1
LEFT JOIN ( SELECT table1_id, Max(Date) T2Date
FROM Table2
GROUP BY table1_id
) T2 ON T1.Id = T2.table1_id
ORDER BY MyDate DESC;
Go to Top of Page
   

- Advertisement -