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
 General SQL Server Forums
 New to SQL Server Programming
 Distinct

Author  Topic 

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2009-10-01 : 10:25:00
I need to select the top 3 distinct from the below stored procedure rather than the top 3, how do I do this?

SELECT top 3 tb_specialoffer.*, dbo.tb_properties.*, tb_counties.*, tb_regions.*, tb_country.*, tb_town.*, tb_photos.* FROM tb_specialoffer INNER JOIN
dbo.tb_properties ON dbo.TB_specialoffer.so_bnbid = dbo.tb_properties.PropertyID INNER JOIN
dbo.tb_counties ON dbo.tb_properties.Prop_County = dbo.tb_counties.county_id INNER JOIN
dbo.tb_regions ON dbo.tb_counties.region_id = dbo.tb_regions.RegionID INNER JOIN
dbo.tb_country ON dbo.tb_regions.Region_Country = dbo.tb_country.CountryID INNER JOIN
dbo.tb_photos ON dbo.tb_properties.PropertyID = dbo.tb_photos.propertyid INNER JOIN
tb_town ON tb_town.town_id = prop_town
WHERE dbo.tb_photos.main_photo = 1 AND town_approved = 1 and tb_properties.prop_visible = 1 and tb_specialoffer.so_start <current_timestamp and tb_specialoffer.so_end > current_timestamp

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-10-01 : 10:56:11
SELECT TOP 3 FROM (SELECT DISTINCT ...FROM...WHERE...) A ORDER BY ...
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-01 : 14:44:26
DISTINCT what?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-01 : 14:54:58
unambiguously distinct *,*,*,*,*,*,*


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 14:55:35
you mean distinct of entire value combination or distinct of any 1 field?
Go to Top of Page

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2009-10-02 : 04:35:12
Sorry I should have been more clearer.

I need to find a distinct value of tb_properties.propertyid. All methods I tried I end up with unambiguously distinct
Go to Top of Page

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2009-10-02 : 04:35:52
Posted - 10/02/2009 : 04:35:12
--------------------------------------------------------------------------------

Sorry I should have been more clearer.

I need to find the top 3 distinct value of tb_properties.propertyid. All methods I tried I end up with unambiguously distinct
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 06:21:09
quote:
Originally posted by godspeedba

Sorry I should have been more clearer.

I need to find a distinct value of tb_properties.propertyid. All methods I tried I end up with unambiguously distinct


then you should decide what values you need to bring along with this distinct propertyid from other fields. you need to apply some aggregation like min,max,sum,... for others
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-02 : 11:23:04
SELECT DISTINCT tb_properties.propertyid

FROM tb_specialoffer INNER JOIN
dbo.tb_properties ON dbo.TB_specialoffer.so_bnbid = dbo.tb_properties.PropertyID INNER JOIN
dbo.tb_counties ON dbo.tb_properties.Prop_County = dbo.tb_counties.county_id INNER JOIN
dbo.tb_regions ON dbo.tb_counties.region_id = dbo.tb_regions.RegionID INNER JOIN
dbo.tb_country ON dbo.tb_regions.Region_Country = dbo.tb_country.CountryID INNER JOIN
dbo.tb_photos ON dbo.tb_properties.PropertyID = dbo.tb_photos.propertyid INNER JOIN
tb_town ON tb_town.town_id = prop_town
WHERE dbo.tb_photos.main_photo = 1 AND town_approved = 1 and tb_properties.prop_visible = 1 and tb_specialoffer.so_start <current_timestamp and tb_specialoffer.so_end > current_timestamp



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -