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 |
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-25 : 04:04:47
|
| I have some set of table.TablesSiteSiteID---------Name1----------test2----------test2SiteAddressSiteAddressID------SiteID--------AddressID---------EffectiveDate------TerminateDate1---------------1----------------1----------------------------1/1/2004--------------1/1/20041---------------1----------------2----------------------------1/1/2004--------------1/1/20041---------------2----------------1---------------------------1/1/2004 --------------1/1/20041---------------2----------------2----------------------------1/1/2004--------------1/1/2004AddressAddressID----Address1--------Address21------ABC---------------XYZ2--------PQR--------------ZZZAddressTypeAddressTypeID--------Description1-----------Office2-----------HomeAddressAddressTypeAddressAddressTypeID---------AddressID-----------AddressTypeID1------------------1--------------------------12------------------1--------------------------23------------------2--------------------------2Scenario is like this one site having more then one addresses and one address can have more then one typeI want to retrieve a data like theseLocation-----------HomeAddress--------------OfficeAddress1--------------------PQR,ZZZ-------------------ABC,XYZIf the Site having more then one home/Work address i have to check effective date from SiteAddress Table and Show only latest address from the list of Site AddressI got this result by using Subquery but it take 4 sec to executeI want to improve performance.How can I write a query without subquery ?? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-25 : 04:07:42
|
| post your current code----------------------------------'KH'Happy Chinese New Year |
 |
|
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-25 : 04:16:46
|
| SELECT (SELECT dbo.tbl_Site.SiteID + ' ' + dbo.tbl_Address.AddressLine1 + ' ' + dbo.tbl_Address.AddressLine2 + ' ' + dbo.tbl_Address.AddressLine3FROM dbo.tbl_Address INNER JOINdbo.tbl_SiteAddress ON dbo.tbl_Address.pk_AddressID = dbo.tbl_SiteAddress.fk_AddressID INNER JOINdbo.tbl_AddressAddressType ON dbo.tbl_Address.pk_AddressID = dbo.tbl_AddressAddressType.fk_AddressID INNER JOINdbo.tbl_AddressType ON dbo.tbl_AddressAddressType.fk_AddressTypeID = dbo.tbl_AddressType.pk_AddressTypeID RIGHT OUTER JOINdbo.tbl_Site ON dbo.tbl_SiteAddress.fk_SiteID = dbo.tbl_Site.pk_SiteIDWHERE (dbo.tbl_SiteAddress.TerminationDate IS NULL ORdbo.tbl_SiteAddress.TerminationDate > GETDATE()) AND (dbo.tbl_AddressType.Description = 'Work')AND tbl_SiteAddress.fk_SiteID = S.pk_SiteID ) ,(SELECT dbo.tbl_Site.SiteID + ' ' + dbo.tbl_Address.AddressLine1 + ' ' + dbo.tbl_Address.AddressLine2 + ' ' + dbo.tbl_Address.AddressLine3FROM dbo.tbl_Address INNER JOINdbo.tbl_SiteAddress ON dbo.tbl_Address.pk_AddressID = dbo.tbl_SiteAddress.fk_AddressID INNER JOINdbo.tbl_AddressAddressType ON dbo.tbl_Address.pk_AddressID = dbo.tbl_AddressAddressType.fk_AddressID INNER JOINdbo.tbl_AddressType ON dbo.tbl_AddressAddressType.fk_AddressTypeID = dbo.tbl_AddressType.pk_AddressTypeID RIGHT OUTER JOINdbo.tbl_Site ON dbo.tbl_SiteAddress.fk_SiteID = dbo.tbl_Site.pk_SiteIDWHERE (dbo.tbl_SiteAddress.TerminationDate IS NULL ORdbo.tbl_SiteAddress.TerminationDate > GETDATE()) AND (dbo.tbl_AddressType.Description = 'Home')AND tbl_SiteAddress.fk_SiteID = S.pk_SiteID )FROM tbl_Site S |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-25 : 05:00:47
|
| can you post your tables DDL ? Is this a working query ? It looks like the subquery may return more than one records----------------------------------'KH'Happy Chinese New Year |
 |
|
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-25 : 05:10:16
|
| Sorry i forgotI used top 1 and order by Effective Date DESCBut i am not very sure its right or wrong becoz my db doesn't contain perfect data but its enough for write a query |
 |
|
|
sanjay_jadam
Yak Posting Veteran
53 Posts |
Posted - 2006-01-27 : 00:41:18
|
| Is it any other way to write this query |
 |
|
|
|
|
|