Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2009, 05:31 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select Distinct Query

Hi There

I have the following query:

SELECTdistinct
cast(RT_VALUATION.RT_VAL_ID1 as varchar) RT_VAL_ID1,
cast(RT_VALUATION.RT_VAL_ID2 as varchar) RT_VAL_ID2 ,
cast(RT_VALUATION.RT_VAL_ID3 as varchar) RT_VAL_ID3,
cast(RT_VALUATION.RT_VAL_ID4 as varchar) RT_VAL_ID4,
cast(RT_VALUATION.RT_VAL_LOCATION as varchar) RT_VAL_LOCATION,
cast(RT_VALUATION__RT_VAL_OWNERS.RT_VAL_OWNERS as varchar) RT_VAL_OWNERS,
cast(RT_VALUATION__RT_VAL_OCCUPIERS.RT_VAL_OCCUPIERS as varchar)as RT_VAL_OCCUPIERS
FROM RT_VALUATION INNERJOIN
RT_VALUATION__RT_VAL_OWNERS ON RT_VALUATION.RT_VALUATION__ID = RT_VALUATION__RT_VAL_OWNERS.RT_VALUATION__ID INNERJOIN
RT_VALUATION__RT_VAL_OCCUPIERS ON RT_VALUATION.RT_VALUATION__ID = RT_VALUATION__RT_VAL_OCCUPIERS.RT_VALUATION__ID
WHERE RT_VALUATION__RT_VAL_OWNERS.RT_VAL_OWNERS notlike RT_VALUATION__RT_VAL_OCCUPIERS.RT_VAL_OCCUPIERS
ORDERBY RT_VAL_ID1,RT_VAL_ID2,RT_VAL_ID3,RT_VAL_ID4

and it gives me the results as this:

03710 001 00 NULL 168 BOILER GULLY RD 127715 129805
03710 001 00 NULL 168 BOILER GULLY RD 129805 127715
03710 002 00 NULL 140 WATTLE BAY RD 171438 710
03710 002 00 NULL 140 WATTLE BAY RD 710 171438
03710 002 02 NULL 196 WATTLE BAY RD 104992 152463
03710 002 02 NULL 196 WATTLE BAY RD 152463 104992

you will notice that it gives me two lines where I only need one eg:

I would only like to have returned one row: eg:
03710 001 00 NULL 168 BOILER GULLY RD 127715 129805
Any help woul be much appreciated

Thanks

Greg
 
Old May 19th, 2009, 05:49 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Could it possibly be a data problem? You've got the same people as owners and occupiers at 168 Boiler Bully Rd. In once case they are the owner and the other occupier. Wouldn't that row read:

03710 001 00 NULL 168 Boiler Gully Rd. 127715 127715

or

03710 001 00 NULL 168 Boiler Gully Rd. 129805 129805

This would have them as owner and occupier.
 
Old May 19th, 2009, 05:56 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, This is correct. I am trying to extract where the owner and the occupier are different and enup with more than one row and I only want to end up with one line - it could be a data problem - not sure
 
Old May 20th, 2009, 12:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

DISTINCT results in unique rows, so the SELECT list should be taken care accordingly.
__________________
- Vijay G
 
Old May 20th, 2009, 01:45 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

You are pulling unique rows in your result set. It's just that you have two owners for the same property. It seems that there could only be one owner of a property at a time.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Distinct kirkmc Access 13 July 13th, 2006 01:26 PM
Select Distinct kirkmc Excel VBA 3 May 5th, 2006 07:55 PM
Select Distinct? [email protected] SQL Language 5 November 5th, 2005 09:58 AM
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM
select distinct bmains ADO.NET 0 April 8th, 2004 02:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.