Q:

Case Count When Teradata SQL

I am trying to write a query that will bring back an order number unless there are multiple order numbers with the transaction, which will need to return the word Multiple. I am getting a group by cannot contain aggregate,  but if I take it out it says non-aggregate values must be part of the associated group. Any ideas? The below code is what I currently have
Select
s.Loc_id as Loc_ID
,lh.loc_nbr as Store_Nbr
,s.sltrn_dt as Sales_date
,s.rgstr_sls_post_dt as Sales_Post_Date
,s.sltrn_id As Trans_ID
,s.sls_tm as Sales_Time
,zeroifnull(HDISS_TAX_EXMPT_ID) as Exempt_ID
,sum(net_ce_sls_amt)
,sum(mkdn_amt)
,sum(tax_amt)
,sum(grs_sls_amt)
,sr.cust_ord_nbr

FROM PR_US_SALES_UNSEC_VIEWS.SLTRN s

join
PR_US_SALES_UNSEC_VIEWS.SLTRN_TAX_EXMPT ste
on (s.loc_id = ste.loc_id and s.sltrn_dt = ste.sltrn_dt and s.pos_rgstr_id = ste.pos_rgstr_id and s.sltrn_id = ste.sltrn_id)

join 
pr_shrd_views.loc_hier lh
on(s.loc_id = lh.loc_id)

join pr_us_sales_unsec_views.sltrn_rsm sr
on (s.loc_id = sr.loc_id and s.sltrn_dt = sr.sltrn_dt and s.pos_rgstr_id = sr.pos_rgstr_id and s.sltrn_id = sr.sltrn_id)

where s.sltrn_dt between '2012-07-06' and current_date
--and lh.loc_nbr in (1501, 1523, 1528, 1551)
and trans_typ_cd in (1,3)
and trans_stat_cd = 1
and ipv_flg= 'N'

--HAVING COUNT( cust_ord_nbr ) > 1

Group by 1,2,3,4,5,6,7,12

A:

When you uncomment the HAVING (which should be after GROUP BY) you need to remove cust_ord_nbr from GROUP BY and apply an aggregate function within the Select list, e.g. MIN(cust_ord_nbr).
Based on your narration you want something like this:
select
...
   ,sum(grs_sls_amt)
   ,case
       when COUNT(DISTINCT cust_ord_nbr ) > 1 
       then 'multiple'
       else trim(min(sr.cust_ord_nbr)
    end  
FROM PR_US_SALES_UNSEC_VIEWS.SLTRN s
...
Group by 1,2,3,4,5,6,7

