Home Page for the TeradataForum
 

Archives of the TeradataForum

Message Posted: Fri, 05 Jan 2007 @ 15:35:06 GMT


     
  <Prev Next>  
<<First
<Prev
Next> Last>>  


Subj:   Counting a column multiple times based on CASE
 
From:   Anomy Anom

<-- Anonymously Posted: Friday, January 05, 2007 10:04 -->

Hi all,

I have the following data

     CREATE VOLATILE TABLE temp3 ,NO FALLBACK ,
          CHECKSUM = DEFAULT,
          LOG
          (
           ID DECIMAL(12,0),
           TRXN_ID DECIMAL(18,0),
           frst_prch_days INTEGER,
           interacts INTEGER)
     PRIMARY INDEX ( ID,TRXN_ID,frst_prch_days )
     ON COMMIT PRESERVE ROWS;

     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values(300000000000,333333333,89,6);
     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values(300000000000,333333334,364,1);
     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values(300000000000,333333335,547,2);
     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values(300000000000,333333336,550,5);
     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values(400000000000,222222222,89,3);
     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values (500000000000,111111111,300,3);
     insert into temp3 (id,trxn_id,frst_prch_days,interacts)
        values(500000000000,111111112,300,3);

I need to count the trxn_id 's by ID based on a set of conditions for frst_prch_days.

Here is my SQL;

     select id
       ,count(Case When  frst_prch_days  between 0 and  90
                            Then trxn_id
                            Else    0
                    end )As txns_1
       ,count(case when frst_prch_days  between 91 and 365
                          then trxn_id
                          else 0
          end) as txns_2
       ,count(case when frst_prch_days  between 366 and 548
                          then trxn_id
                          else 0
         end) as txns_3
        ,count(case when frst_prch_days  ge 549
                 then trxn_id
                           else 0
         end) as txns_4
     from temp3
     group by 1

I am expecting the following result

     ID              txns_1  txns_2  txns_3  txns_4

     ----------------------------------------------
     300000000000    1       1       1       1
     500000000000    0       2       0       0
     400000000000    1       0       0       0

But I am getting the result as

     ID              txns_1  txns_2  txns_3  txns_4

     ----------------------------------------------
     300000000000    4       4       4       4
     500000000000    2       2       2       2
     400000000000    1       1       1       1

Any suggestions on how to modify my SQL


Thanks in advance



     
  <Prev Next>  
<<First
<Prev
Next> Last>>  
 
 
 
 
 
 
 
 
  
  Top Home Privacy Feedback  
 
 
Copyright for the TeradataForum (TDATA-L), Manta BlueSky    
Copyright 2016 - All Rights Reserved    
Last Modified: 27 Dec 2016