Archives of the TeradataForum
Message Posted: Fri, 05 Jan 2007 @ 16:13:50 GMT
Subj: | | Re: Counting a column multiple times based on CASE |
|
From: | | Dieter Noeth |
Anomy.Anom wrote:
> 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
COUNT counts non-NULL rows and the result of your CASE is 1 or 0, i.e. non-NULL.
So replace "ELSE 0" with "ELSE NULL" or simply remove it.
Even better, use SUM instead of COUNT.
Dieter
|