MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2004, 09:34 AM
el1179 el1179 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 el1179 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 5 sec
Reputation Power: 0
selecting columns into other conditionally

I have a table that contains people with addresses, we will call it 'person'. Some of these people also have mailing addresses. Those who do have mailing addresses are marked with a flag field. The mailing addresses are found in a seperate table, that I will call 'mailadd'.

I am trying to select a group of people using a select statement and joining the 'mailadd' table address when somebody from the 'person' table is marked with the mailing address flag.

I can get the result set fine, but what I want to do is sort the results by address, using the mailadd address for a person if it exists and otherwise using the address found in the 'person' table.

I really want to treat the result set as if there was one address for each record and sort by it. Is there a way to select the mailing address into the regular (without altering the phyisical tables), or to do a conditional sort?

Reply With Quote
  #2  
Old July 2nd, 2004, 10:35 AM
lucasalexander lucasalexander is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Atlanta
Posts: 39 lucasalexander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via AIM to lucasalexander Send a message via MSN to lucasalexander
I would use a stored procedure to select the mailing addresses and person names (and other non-address data) into a temp table or table variable. Then I would insert into that table/variable all the addresses and other data for people without mailing addresses. Finally, you can select from the table/variable with whatever sort order you need.

Lucas Alexander
http://www.alexanderdevelopment.net

Reply With Quote
  #3  
Old July 2nd, 2004, 10:57 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
Code:
select person.name, person.adress,mailadd.adress
from person left join mailadd
on person.primarykeyColumn = mailadd.foreignkeyColumn
order by coalesce(mailadd.adress,person.adress)


Change names as apropriate.

Reply With Quote
  #4  
Old July 2nd, 2004, 11:52 AM
el1179 el1179 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 el1179 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 5 sec
Reputation Power: 0
Awsome, thanks a lot... I was not familiar with the coalesce Expression

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > selecting columns into other conditionally


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 1 hosted by Hostway
Stay green...Green IT