Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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 August 21st, 2003, 01:46 PM
black_lotus black_lotus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 241 black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 12 h 22 m 8 sec
Reputation Power: 19
SQL help please

Hi,

I'm a little stuck with this query I'm working with. I'm trying to return products that have a value in one or both of two fields. If there is a value in both fields, return this product twice. Here is a lame example:

If the product is a fruit, return its number.
or
If its a vegetable, return its number.
or
If its both, then return its number twice.

Can this be done in one query? This is probably something simple but I cant seem to figure it out.
Thanks, for the help!

Reply With Quote
  #2  
Old August 21st, 2003, 07:06 PM
unatratnag unatratnag is offline
Average Intelligence
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Location: Ohio/Chicago
Posts: 678 unatratnag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 22 sec
Reputation Power: 6
Send a message via AIM to unatratnag
return number twice? as in same row?

Code:
select case
	when (p.veg= '1'
               and p.fru = '1')
         then  p.id + ',' + p.id
        else
            p.id
          end
from products as p

Reply With Quote
  #3  
Old August 21st, 2003, 07:56 PM
black_lotus black_lotus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 241 black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level)black_lotus User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 12 h 22 m 8 sec
Reputation Power: 19
ya, the row would return twice

Reply With Quote
  #4  
Old August 21st, 2003, 09:03 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,913 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 24 m 34 sec
Reputation Power: 1018
select productnumber from products
where fruit='yes'
UNION ALL
select productnumber from products
where vegetable='yes'

UNION ALL returns all rows from both selects, where UNION would have eliminated the dupe

rudy
http://r937.com/

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL help please


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