Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Compbined into one select?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-17-03, 11:05
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Question Combined into one select?

Hi all,

I have the following:

Code:
select count([Account Number]) AS UNDER25_12_SINGLE from OH_UNDER25_12MONTHS where AccountCount = 1 select count([Account Number]) AS UNDER25_12_MULTIPLE from OH_UNDER25_12MONTHS where AccountCount > 1 select count([Account Number]) AS UNDER25_36_SINGLE from OH_UNDER25_36MONTHS where AccountCount = 1 select count([Account Number]) AS UNDER25_36_MULTIPLE from OH_UNDER25_36MONTHS where AccountCount > 1 select count([Account Number]) AS UNDER25_60_SINGLE from OH_UNDER25_60MONTHS where AccountCount = 1 select count([Account Number]) AS UNDER25_60_MULTIPLE from OH_UNDER25_60MONTHS where AccountCount > 1

Is there anyway to combined them into one query? So I get one result?

Thanks,

Ken

Last edited by GA_KEN : 03-17-03 at 11:26.
Reply With Quote
  #2 (permalink)  
Old 03-17-03, 11:48
osy45 osy45 is offline
Registered User
 
Join Date: Nov 2002
Posts: 833
try the case construct

Syntax case
when search_condition then expression
[when search_condition then expression]...
[else expression]
end
case and values syntax:
case expression
when expression then expression
[when expression then expression]...
[else expression]
end
Parameters case
begins the case expression.
when
precedes the search condition or the expression to be compared.
Reply With Quote
  #3 (permalink)  
Old 03-17-03, 11:50
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Can you provide a small example to make it a little clearer?

Thanks for the reply!

Ken
Reply With Quote
  #4 (permalink)  
Old 03-17-03, 12:34
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Quote:
Originally posted by GA_KEN
Can you provide a small example to make it a little clearer?

Thanks for the reply!

Ken

I'd have prefered to start with all the data in one table, but this will do it (if I haven't made any mistakes):

select sum(UNDER25_12_SINGLE) AS UNDER25_12_SINGLE
, sum(UNDER25_12_MLTIPLE) AS UNDER25_12_MULTIPLE
, sum(UNDER25_36_SINGLE) AS UNDER25_36_SINGLE
, sum(UNDER25_36_MLTIPLE) AS UNDER25_36_MULTIPLE
, sum(UNDER25_60_SINGLE) AS UNDER25_60_SINGLE
, sum(UNDER25_60_MLTIPLE) AS UNDER25_60_MULTIPLE
from
(
select sum( case when AccountCount = 1 then 1 else 0 end) AS UNDER25_12_SINGLE
, sum( case when AccountCount > 1 then 1 else 0 end) AS UNDER25_12_MULTIPLE
, 0 AS UNDER25_36_SINGLE
, 0 AS UNDER25_36_MULTIPLE
, 0 AS UNDER25_60_SINGLE
, 0 AS UNDER25_60_MULTIPLE
from OH_UNDER25_12MONTHS
UNION ALL
select 0 AS UNDER25_12_SINGLE
, 0 AS UNDER25_12_MULTIPLE
, sum( case when AccountCount = 1 then 1 else 0 end) AS UNDER25_36_SINGLE
, sum( case when AccountCount = 1 then 1 else 0 end) AS UNDER25_36_MULTIPLE
, 0 AS UNDER25_60_SINGLE
, 0 AS UNDER25_60_MULTIPLE
from OH_UNDER25_36MONTHS
UNION ALL
select 0 AS UNDER25_12_SINGLE
, 0 AS UNDER25_12_MULTIPLE
, 0 AS UNDER25_36_SINGLE
, 0 AS UNDER25_36_MULTIPLE
, sum( case when AccountCount = 1 then 1 else 0 end) AS UNDER25_60_SINGLE
, sum( case when AccountCount = 1 then 1 else 0 end) AS UNDER25_60_MULTIPLE
from OH_UNDER25_36MONTHS
);
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On