SELECT Failed. 3504: Selected non-aggregate values must be part of the associated group in v14.10.05.09 and not in v14.00.07.02 - teradata

There are totally 9 fields in the select clause of the query where 5 are direct fields. 2 of them are result of select query in the select clause and 2 more are from using the result from previous 2 fields used in case when statements. The SUM aggregate is applied on the last two columns.
All the first 5 columns are listed in the group by clause.
Query :
SELECT
sub.a,sub.b,sub.c,Table1.d,Table1.e,
(select x from table t1 where y=current_date) as f,
(select z from table t1 where y=w) as g,
sum(case when f=g then 1 else 0)) as h,
sum(case when f+1=g then 1 else 0)) as i
FROM Table1
LEFT JOIN
(SELECT a,b,c,l,m,n,o FROM Table2 INNER JOIN Table3 ON Table2.p=Table3.t)sub
ON Table1.e = sub.l
GROUP BY sub.a,sub.b,sub.c,Table1.d,Table1.e
The above query which works perfectly fine in Teradata v14.10.05.09 but fails with
"SELECT Failed. 3504: Selected non-aggregate values must be part of
the associated group"
in Teradata v14.00.07.02.
Is this because of something introduced in the newer version that is causing the query to fail but run fine in older version?
Or am I missing something?
Thanks in Advance.
This is the actual query that is giving the problem: SELECT
sub.key,sub.div,sub.reg_nm,tb_cal.calendar_yr,tb_cal.calendar_mth,
(SEL fiscal_yr FROM db1.tb_cal tb_cal WHERE calendar_dt=CURRENT_DATE) current_fy,
(SEL fiscal_yr FROM db1.tb_cal tb_cal WHERE calendar_dt=clse_dt) clse_dt_fy,
SUM(CASE WHEN current_fy=clse_dt_fy THEN sub.amt ELSE 0 END) cnt
FROM db1.tb_cal tb_cal
LEFT JOIN( SELECT
tbl_a.key,tbl_a.stage,COALESCE(tbl_u.div, 'Un-assigned') div,COALESCE(tbl_u.reg_nm,'Un-assigned') reg_nm,tbl_a.clse_dt,tbl_a.amt
FROM db_a.tbl_a tbl_a
INNER JOIN db1.tbl_u tbl_u
ON tbl_u.unit_key=tbl_a.unit_key
WHERE tbl_a.ctg IN ('G','O','C','F','I') AND tbl_a.stage NOT IN ('R', 'D', 'N','A') AND tbl_a.ind = 0
)sub
ON sub.clse_dt = tb_cal.calendar_dt
GROUP BY sub.key,sub.div,sub.reg_nm,tb_cal.calendar_yr,tb_cal.calendar_mth

SELECT
sub.key,
sub.div,
sub.reg_nm,
tb_cal.calendar_yr,
tb_cal.calendar_mth,
(SEL fiscal_yr FROM db1.tb_cal tb_cal WHERE calendar_dt=CURRENT_DATE) current_fy,
(SEL fiscal_yr FROM db1.tb_cal tb_cal WHERE calendar_dt=clse_dt) clse_dt_fy,
SUM(CASE WHEN current_fy=clse_dt_fy THEN sub.amt ELSE 0 END) cnt
FROM db1.tb_cal tb_cal
LEFT JOIN( SELECT
tbl_a.key,tbl_a.stage,COALESCE(tbl_u.div, 'Un-assigned') div,COALESCE(tbl_u.reg_nm,'Un-assigned') reg_nm,tbl_a.clse_dt,tbl_a.amt
FROM db_a.tbl_a tbl_a
INNER JOIN db1.tbl_u tbl_u
ON tbl_u.unit_key=tbl_a.unit_key
WHERE tbl_a.ctg IN ('G','O','C','F','I') AND tbl_a.stage NOT IN ('R', 'D', 'N','A') AND tbl_a.ind = 0
) sub
ON sub.clse_dt = tb_cal.calendar_dt
GROUP BY sub.key,sub.div,sub.reg_nm,tb_cal.calendar_yr,tb_cal.calendar_mth,current_fy,clse_dt_fy;
This should work fine. But as per your comments, you said adding the last to columns in group by would alter your required answer. If that is the case, you can post what is your desired result set. We can help you out in that. Although looking at your query it doesn't seem that adding current_fy and clse_dt_fy will change your current result set.

Related

Case statement duplicating rows - teradata

i have the following query and the problem is in the Case statement. It s being used in a Join condition. for some reason is bringing up both alternatives from the case
here s the code
SELECT C.GREGORIAN_MONTH_ID
,C.BUSINESS_PARTY_ID
,C.TOTAL_MONTO
,C.Capitas_Puntuales
,C.CUIT
,nb_apellido
,nb_oficial_actual
,nb_cne_obe
,nb_nodo
,nb_territorio
,cd_area_negocio
FROM (SELECT gregorian_month_id
,business_party_id
,Case when BUSINESS_PARTY_ID in (88888,200) then '9999999999999' else business_party_ident_num end CUIT
,SUM(amt_accum) Total_Monto
,count(*) Capitas_Puntuales
FROM dbsreg.A127932_PARTY_MAIN_PAYROLL_HISTORY
where gregorian_month_id= 201806
group by 1,2,3
) C
LEFT JOIN
(select NU_CUIT, nb_apellido,nb_oficial_actual, nb_cne_obe, nb_nodo, nb_territorio,cd_area_negocio
,(fh_corte/100 + 190000) gregorian_month_id
from dbsreg.a119527_base_info_gerencial
) G
on C.CUIT = G.NU_CUIT
and (case
when C.gregorian_month_id in (G.gregorian_month_id) then C.gregorian_month_id
else C.gregorian_month_id -1
END) = G.gregorian_month_id
ORDER BY C.BUSINESS_PARTY_ID
anyone can tell what s going on there?
You final query returns C.BUSINESS_PARTY_ID which is not transformed by Case. Your case statement also draws on BUSINESS_PARTY_ID. Did you mean to have C.CUIT instead of C.BUSINESS_PARTY_ID in your outer select statement?
long story short, i moved the case withing to a where statement from table C and added a userdefined value for the gregorian:
SELECT C.GREGORIAN_MONTH_ID
,C.BUSINESS_PARTY_ID
,C.TOTAL_MONTO
,C.Capitas_Puntuales
,C.CUIT
,nb_apellido
,nb_oficial_actual
,nb_cne_obe
,nb_nodo
,nb_territorio
,cd_area_negocio
FROM (SELECT gregorian_month_id
,business_party_id
,Case when BUSINESS_PARTY_ID in (88888,200) then '9999999999999' else business_party_ident_num end CUIT
,SUM(amt_accum) Total_Monto
,count(*) Capitas_Puntuales
FROM dbsreg.A127932_PARTY_MAIN_PAYROLL_HISTORY
where gregorian_month_id= ?FECHA_BASE
group by 1,2,3
) C
LEFT JOIN
(select NU_CUIT, nb_apellido,nb_oficial_actual, nb_cne_obe, nb_nodo, nb_territorio,cd_area_negocio
,(fh_corte/100 + 190000) gregorian_month_id
from dbsreg.a119527_base_info_gerencial
where gregorian_month_id = (Case when ?FECHA_BASE = (gregorian_month_id) then gregorian_month_id else gregorian_month_id-1 end )
) G
on C.CUIT = G.NU_CUIT
ORDER BY C.BUSINESS_PARTY_ID

Choosing Single Row from multiple rows - Teradata

I have a requirement below to choose latest status.
Table 1:
Table2:
Results Expected:
Below is the logic we need.
SELECT
Table1.ID,
,CASE WHEN (Table1.hub=Table2.hub) THEN Table2.Status ELSE NULL END AS Original_Status
,CASE WHEN (Table1.hub<>Table2.hub AND Table2.Status like 'Found%' ) THEN Table2.hub ELSE NULL END AS Derived_Hub
,CASE WHEN (Table1.hub<>Table2.hub AND Table2.Status like 'Found%' ) THEN Table2.Status ELSE NULL END AS Derived_Status
from
Table1
Join Table2
ON (Table1.ID=Table2.ID)
With this code, I am getting 3 rows. If I put max in the above case statements, I am getting Hub3 instead of Hub2.
Could you please let me know how I can merge everything into single row. Please note that I want to show latest found Status from Table 2 even if there are 2 hubs with found status.
Let me know if you need any further information.
I think your best bet is to join into Table 2 twice. The first time, much like you are doing here, but using a LEFT OUTER JOIN on both id and hub. Then the second time on a derived version of table2 where you only select the top 1 records when sorted by timestamp in descending order:
SELECT
t1.id,
t1.hub as "Original Hub"
t2.status as "Original Hub Status",
t3.hub as "Found Hub",
t3.status as "Found Hub Status"
FROM
Table1 t1
LEFT OUTER JOIN Table2 t2 ON
t1.id = t2.id AND
t1.hub = t2.hub
LEFT OUTER JOIN
(
--Select a hub with the same id, that doesn't share the same hub number
--Only choose the top record when sorted by timestamp in descending order
SELECT TOP 1 id, hub, status
FROM table2
WHERE t1.hub <> table2.hub
ORDER BY TimeStamp Desc
) t3 ON
t1.id = t3.id
That subquery that has alias t3 is using a correlated subquery by referencing the t1.hub value in it's WHERE statement.
In TD14.10 you can utilize LAST_VALUE to access data from the "last" row. The CASEs are based on your query:
SELECT
t2.ID,
CASE WHEN t1.Hub = t2.Hub THEN t2.Hub END AS Original_Hub,
CASE WHEN t1.Hub = t2.Hub THEN t2.Status END AS Original_Status,
-- get the last Hub
LAST_VALUE(CASE WHEN t1.Hub <> t2.Hub AND Table2.Status like 'Found%'
THEN t2.Hub
END)
OVER (PARTITION BY t1.ID
ORDER BY CASE WHEN t1.Hub = t2.Hub THEN 0 ELSE 1 END, t2.TS
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Derived_Hub,
-- get the last Status
LAST_VALUE(CASE WHEN t1.Hub <> t2.Hub AND Table2.Status like 'Found%'
THEN t2.Status
END)
OVER (PARTITION BY t1.ID
ORDER BY CASE WHEN t1.Hub = t2.Hub THEN 0 ELSE 1 END, t2.TS
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Derived_Status
FROM
Table1 AS t1
JOIN Table2 AS t2
ON (t1.ID=t2.ID)
QUALIFY
ROW_NUMBER () -- return the 1st row only
OVER (PARTITION BY t1.ID
ORDER BY CASE WHEN t1.Hub = t2.Hub THEN 0 ELSE 1 END, t2.TS) = 1
EXPLAIN should combine all OVER into a single STATS-step as they use the same PARTITION BY and ORDER BY.

Teradata case statement

Hi i am using the below query where i need to print value 'Yes' if a particular column has a value .in the below example for particular case id there are two values (one with proper date and one with NULL) so the result should be 'Yes' but i am getting two results 'Yes' and 'No'.Please find below the case statement.
select
distinct CC.CASE_ID,
case when CC.L2CALL_BK_SCEHDULED_PST_DT is not NULL
then 'Yes'
else 'No'
end as L2_OUTB_CAL_SCHD_FL
FROM EDW_KATAMARI_T.CNTCT_CASE CC
INNER JOIN EDW_KATAMARI_T.CNTCT_CASE_EXTN CCE
ON CC.CNTCT_CASE_APND_KEY = CCE.CNTCT_CASE_APND_KEY
INNER JOIN EDW_STAGE_COMN_SRC.STG_CNTCT_CASE_DELTA DELTA
on CC.CASE_ID = DELTA.CASE_ID
where
CC.CASE_ID='22922029'
group by 1,2
Moving from a comment by #dnoeth:
You need to do a MAX(CASE... and GROUP BY 1

Inner Join & Count, Microsoft SQL

I am trying to do a count inside a nested statement with inner join
select a.app_id, a.first_name, a.last_name, d.svd_id
from wwhs_app a inner join
wwhs_svc d on a.app_id = d.app_id
where a.app_id in(
select top 50 app_id
from wwhs_app
Where app_create_dt > '2012-07-23 00:00:00')
I need a count of svd_id as well, but I keep getting errors every way I try. Suggestions?
You need to count for svd_id but it's not in the query.
Did you mean 'app_id'?
Try this...
SELECT a.app_id, a.first_name, a.last_name, d.svd_id
FROM wwhs_app a
INNER JOIN wwhs_svc d on a.app_id = d.app_id
WHERE a.app_id in (
SELECT TOP 50 app_id, COUNT(*) as id_count
FROM wwhs_app
WHERE app_create_dt > '2012-07-23 00:00:00'
GROUP BY app_id ORDER BY id_count)

record types that weren't found for a specific value in oracle query

I have this query
Select distinct p_id, p_date,p_city
from p_master
where p_a_id in(1,2,5,8,2,1,10,02)
and my IN clause contains 200 values. How do I get to know which ones weren't returned by the query. Each value in the IN clause may have a record in some cases they don't. I want to know all the records that weren't found for any selected p_a_id type.
Please help
This will do the trick but I'm sure there's an easier way to find this out :-)
with test1 as
(select '1,2,5,8,2,1,10,02' str from dual)
select * from (
select trim(x.column_value.extract('e/text()')) cols
from test1 t, table (xmlsequence(xmltype('<e><e>' || replace(t.str,',','</e><e>')|| '</e></e>').extract('e/e'))) x) cols
left outer join
(Select count(*), p_a_id from p_master where p_a_id in (1,2,5,8,2,1,10,02) group by p_a_id) p
on p.p_a_id = cols.cols
where p_a_id is null
;

Resources