Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 1st, 2005, 03:05 AM
sssm sssm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 sssm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Select 1 Record From Multiple Records, Distinct

I have just joined this thread and hope that I will get the solution

I have two tables provider and provider location. I am joining these two tables on the provider_id. In the provider_locn table I have more then locations against any single provider id. I am giving the sample of the tables

PROVIDER Table

Provider_ID PROVIDER_NAME .....
1 ABC
2 XYZ
....
.....
PROVIDER_LOCN table

Provider_ID Provider_Locn Provider_Addr_line1 Provider_Addr_line2
1 1 abc st apt 1
1 2 xyz suite 1
1 3 123 1234
2 1 124 12
2 2 abc 1

I want to select only 1 provider_location against any provider_id

I was trying to use the distinct and group by clause, it works only if I am just selecting the provider_id and location_id, but if I try to get the location address then it selects the multiple location id's and addresses. but I want only 1 location address. I will really appriciate your help.

Reply With Quote
  #2  
Old January 2nd, 2005, 02:44 AM
popeye137 popeye137 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Queen City of the South--> Cebu Philippines
Posts: 11 popeye137 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 22 sec
Reputation Power: 0
Select 1 Record From Multiple Records, Distinct

can you post your query please...

if you want your result to be more specific supply more statements in the where clause..



Reply With Quote
  #3  
Old January 3rd, 2005, 03:09 PM
sssm sssm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 sssm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by popeye137
can you post your query please...

if you want your result to be more specific supply more statements in the where clause..




I am using the command like this, after selecting one location_id I will join this table with the provider table on the basis of the provider_id. I am using the max because I want to select only one location_id against any provider_id, no matter it is max or min.

select prvdr_id,max(locn_id)
from provider_locn
group by prvdr_id

It works on these two columns, but when I try to put locn_addr_line1 and locn_addr_line2 and put these two columns in the group by clause then it again selects more then 1 location_id against any single provider_id

select prvdr_id,max(locn_id),locn_addr_line1,locn_addr_li ne2
from es_provider_locn
group by prvdr_id,locn_addr_line1,locn_addr_line2

I dont know where I am doing wrong or how should I select these columns in the query, so your help will be appriciated please. thx

Reply With Quote
  #4  
Old January 6th, 2005, 04:32 AM
popeye137 popeye137 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Queen City of the South--> Cebu Philippines
Posts: 11 popeye137 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 22 sec
Reputation Power: 0
YES, it will return more than 1 location_id against any single provide_id because your provider_id contains multiple values of locn_addr_line1 & locn_addr_line2. MS SQL does not know what locn_addr_ljne1 he will pick up so MS SQL will return all the locn_addr_line1...

workout the query below to obtain single values for locn_addr_line1 & locn_addr_line2...

select prvdr_id,
max(locn_id),
locn_addr_line1 = (select... from.... where...),
locn_addr_li ne2 = (select... from.... where...)
from es_provider_locn
group by prvdr_id

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Select 1 Record From Multiple Records, Distinct


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT