If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > need help with a query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-04, 03:11
chhabradc2 chhabradc2 is offline
Registered User
 
Join Date: Mar 2004
Posts: 4
Question need help with a query

In the below query ,if there is no row with ORIG_TERM_IND as 'T' the whole query fails ,even though there are rows with value as 'O'.

What I want is that CALLING should display the sum of those rows which have ORIG_TERM_IND as 'O' and CALLED should display 0

SELECT SUM(E1.DUR) CALLING,
SUM(E2.DUR) CALLED
FROM
Table a E1,
Table a E2
WHERE
E2.LOGIN=E1.LOGIN
AND E1.ORIG_TERM_IND = 'O'
AND E2.ORIG_TERM_IND ='T'
GROUP BY
E1.LOGIN

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 03-31-04, 03:18
wuadko wuadko is offline
Registered User
 
Join Date: Oct 2003
Location: Slovakia
Posts: 21
I am not sure whether I understand your porblem but try this code...it may help you...

SELECT SUM(E1.DUR) CALLING,
SUM(E2.DUR) CALLED
FROM
Table a E1,
Table a E2
WHERE
E2.LOGIN=E1.LOGIN
AND
(E1.ORIG_TERM_IND = 'O' OR E2.ORIG_TERM_IND ='T')
GROUP BY
E1.LOGIN
__________________
wuadko
Reply With Quote
  #3 (permalink)  
Old 03-31-04, 03:25
chhabradc2 chhabradc2 is offline
Registered User
 
Join Date: Mar 2004
Posts: 4
Sorry ,but that wont help becuase by putting OR both CALLING and CALLED would display the same values .
I want CALLING to display sum of those rows whihc have value as 'O' and CALLED to display sum of those rows which have value as 'T'.


Quote:
Originally posted by wuadko
I am not sure whether I understand your porblem but try this code...it may help you...

SELECT SUM(E1.DUR) CALLING,
SUM(E2.DUR) CALLED
FROM
Table a E1,
Table a E2
WHERE
E2.LOGIN=E1.LOGIN
AND
(E1.ORIG_TERM_IND = 'O' OR E2.ORIG_TERM_IND ='T')
GROUP BY
E1.LOGIN
Reply With Quote
  #4 (permalink)  
Old 03-31-04, 03:28
zeus77 zeus77 is offline
Registered User
 
Join Date: Mar 2004
Location: Venice,Italy
Posts: 20
Re: need help with a query

I'm new of here, so I take this to present myself: I'm from Italy and I worked mainly on oracle,mssql and mysql

a better solution that query the table once can be this (I assume you use oracle,but it can be translate easily)

select
sum(decode(ORIG_TERM_IND,'O',DUR,0)) CALLING,
sum(decode(ORIG_TERM_IND,'T',DUR,0)) CALLED
from table
group by login
Reply With Quote
  #5 (permalink)  
Old 03-31-04, 07:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
zeus77's approach written in standard sql --
PHP Code:
select login
     
sum( case when ORIG_TERM_IND 'O'
                 
then DUR
                 
else 0   end) as CALLING
     
sum( case when ORIG_TERM_IND 'T'
                 
then DUR
                 
else 0   end) as CALLED
  from table
group 
    by login 
another approach:
PHP Code:
select login
     
'Calling' as type
     
sum(DUR) as sumtype
  from table
 where ORIG_TERM_IND 
'O'  
group 
    by login
union all
select login
     
'Called' 
     
sum(DUR
  
from table
 where ORIG_TERM_IND 
'T'  
group 
    by login 
yet another approach:
PHP Code:
select login
     
ORIG_TERM_IND 
     
sum(DUR) as sumtype
  from table
group 
    by login
     
ORIG_TERM_IND 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-31-04, 08:12
zeus77 zeus77 is offline
Registered User
 
Join Date: Mar 2004
Location: Venice,Italy
Posts: 20
I didn't know 'case when then else' is in standard SQL.... good to know, thanks
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On