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 |
|
bartman_z
Starting Member
11 Posts |
Posted - 2005-06-22 : 10:45:41
|
| Guys,I have 2 tables..Table1CallID (int, PK)Other fields...Table2CallDetailID (int, PK)CallID (int, FK)DateRaised (datetime)Description (varchar(500))I need to create a query based on table1 that is something likeSELECT * FROM Table1 WHERE CallID=3What I also want in the same query is the latest DESCRIPTION from Table2 that relates to Table1.i.e.SELECT Table1.* (and the latest related description from Table2) FROM Table1 WHERE CallID=3Please helpMany thanksBartwww.meridiantd.co.uk |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-06-22 : 12:44:00
|
Here is one way. If this doesn't work, post working DDL/DML and expected results.(DDL/DML is: create table statements and insert statements)--this is assuming the Latest description is based on RaisedDate--and that there could be duplicate RaisedDate valuesselect a.CallID ,a.<OtherColumns> --Use explicit column list instead of * ,b.Descriptionfrom Table1 ajoin ( select top 1 CallID ,Description from Table2 a where CallID = 3 and DateRaised in ( select max(DateRaised) DateRaised from Table2 where CallID = a.CallID ) order by callDetailID desc ) b on a.CallID = b.CallID Be One with the OptimizerTG |
 |
|
|
bartman_z
Starting Member
11 Posts |
Posted - 2005-06-22 : 13:12:02
|
| TG,Thanks, nearly worked, but I think I fooled you on specifiying criteria for the CaseIDDDL isif exists (select * from sysobjects where id = object_id(N'[table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [table1]GOif exists (select * from sysobjects where id = object_id(N'[table2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [table2]GOcreate table table1( CaseID int, CaseNo varchar(10))create table table2( CaseDetailID int, CaseID int, Description varchar(200))INSERT INTO table1 (CaseID, CaseNo) VALUES (1, 'ABC123')INSERT INTO table1 (CaseID, CaseNo) VALUES (2, 'DEF123')INSERT INTO table1 (CaseID, CaseNo) VALUES (3, 'GHI123')INSERT INTO table2 (CaseDetailID, CaseID, Description ) VALUES (1, 1, '1 First')INSERT INTO table2 (CaseDetailID, CaseID, Description ) VALUES (2, 1, '2 Second')INSERT INTO table2 (CaseDetailID, CaseID, Description ) VALUES (3, 2, '2 First')INSERT INTO table2 (CaseDetailID, CaseID, Description ) VALUES (4, 2, '2 Second')INSERT INTO table2 (CaseDetailID, CaseID, Description ) VALUES (5, 3, '3 First')INSERT INTO table2 (CaseDetailID, CaseID, Description ) VALUES (6, 3, '3 Second')--SELECT CaseID, CaseNo FROM table1SELECT a.CaseID, a.CaseNo, b.Description FROM Table1 a INNER JOIN (SELECT TOP 1 CaseID, Description FROM Table2 a WHERE CaseID = a.caseid AND CaseDetailID IN (SELECT MAX(CaseDetailID) CaseDetailID FROM Table2 WHERE CaseID = CaseID) ORDER BY CaseDetailID DESC) b ON a.CaseID = a.CaseIDdrop table [table1]drop table [table2]What I get as results from this are;1 ABC123 3 Second2 DEF123 3 Second3 GHI123 3 SecondWhen what I need are1 ABC123 1 Second2 DEF123 2 Second3 GHI123 3 Secondi.e. the latest CallDetailID for each row from Table1Thanks in advanced for further helpBartmanwww.meridiantd.co.uk |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-06-22 : 13:26:09
|
Ah, switched things up on me, huh? Try this:by the way, I think the description for max CaseDetailID for CaseID=1 is "2 Second"(unless I'm missing something)select a.CaseID ,a.CaseNo ,b.Descriptionfrom Table1 ajoin ( select CaseID ,Description from Table2 a where CaseDetailID in ( select max(CaseDetailID) CaseDetailID from Table2 where CaseID = a.CaseID ) ) b on a.CaseID = b.CaseID Be One with the OptimizerTG |
 |
|
|
bartman_z
Starting Member
11 Posts |
Posted - 2005-06-22 : 13:30:30
|
| Perfect, and you are quite right about the CaseID=1 is "2 Second", my rubbish typing skills.What a genius, thanks very muchBartwww.meridiantd.co.uk |
 |
|
|
|
|
|
|
|