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

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 October 22nd, 2005, 06:32 AM
erakana erakana is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 erakana User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 31 sec
Reputation Power: 0
SQl statements with condition

I need some help in the SQL select statements with the condition. The select should contain something like this where:-

One cust_id have many listing_id. Each listing carry status such as O and R. There are thousand's of cust_id with the status of 'O' and 'R'. If the cust_id have all 'O' status and no 'R' status, the another table will be updated.. If have 'O' and 'R' in that cust_id it will be skipped.

What I need to do is to select cust_id that have all listing_id with the status of 'O' only and update in the another table.
Do anyone have suggestions. Pls let me know...
Thank You...

Reply With Quote
  #2  
Old October 22nd, 2005, 02:27 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
Please post the structure of your tables.
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Reply With Quote
  #3  
Old October 23rd, 2005, 12:12 AM
erakana erakana is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 erakana User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 31 sec
Reputation Power: 0
Table Listing
Listing_Id Status Cust_Id
10001 O 00001
10002 O 00001
10003 R 00002
10004 O 00002

Table Customer
Cust_Id ServiceType
00001 Active
00002 Active

Only cust_id with the status of 'O' in all the listing will be picked up and update the customer table abd change the active to 'OB' status....Here cust_id '00001' will be changed to 'OB'

Thanks....

Reply With Quote
  #4  
Old October 23rd, 2005, 12:50 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
If you don't need to check servicetype, you can just use

Code:
SELECT
 cust_id
,SUM(
  CASE 
   WHEN status != 'O' THEN 1 ELSE 0
  END) AS nonO
FROM
 listing 
GROUP BY
 cust_id
HAVING 
 nonO = 0


to get your 'O'-value cust_id's. If you need to check servicetype, you can use a join:


Code:
SELECT
 l.cust_id
,SUM(
  CASE 
   WHEN l.status != 'O' THEN 1 ELSE 0
  END) AS nonO
FROM
 listing AS l
INNER JOIN
 customer AS c
ON
 l.cust_id = c.cust_id
WHERE
 c.servicetype = 'Active'
GROUP BY
 l.cust_id
HAVING 
 nonO = 0

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > SQl statements with condition


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