Teradata case statement - case

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

Related

Oracle Json object DISTINCT keep getting repeating columns

So i have a query and it need to return a JSON object but the DISTINCT doesn't work no matter what I try. I've been trying a series of other tests and no matter what the 'WBS' always shows up with 3 or more duplicated columns. Anyone got any ideas?
I am working in Asp.net 6 MVC
PROCEDURE GET_BASELINE_RPT (in_WBS_LEVEL_ID IN NUMBER, in_FISCAL_YEAR IN VARCHAR2,
in_FISCAL_MONTH IN VARCHAR2, RET OUT CLOB) AS
BEGIN
WITH cte AS (
SELECT /*+MATERIALIZE*/ DISTINCT L.WBS_LEVEL_ID
FROM
WBS_LEVEL L
)
SELECT
JSON_ARRAYAGG (
JSON_OBJECT (
'WBS' VALUE L.WBS_LEVEL_NAME,
'Title' VALUE W.DESCRIPTION,
'Rev' VALUE B.REV_NUMBER,
'ScopeStatus' VALUE W.STATUS,
'BCP' VALUE CASE WHEN BC.FISCAL_YEAR = 0 THEN '' ELSE
SUBSTR(BC.FISCAL_YEAR,3,2)||'-'||LPAD(BC.BCP_FISCAL_ID, 3, '0') END,
'BCPApprovalDate' VALUE BC.APPROVAL_DATE,
'Manager' VALUE P1.NICK_NAME,
'ProjectControlManager' VALUE P2.NICK_NAME,
'ProjectControlEngineer' VALUE P3.NICK_NAME,
'FiscalYear' VALUE W.FISCAL_YEAR,
'FiscalMonth' VALUE W.FISCAL_MONTH,
'WBSNumber' VALUE L.WBS_LEVEL_ID
)RETURNING CLOB)
INTO RET
FROM WBS_LEVEL L
LEFT OUTER JOIN BASELINE_RPT B ON L.WBS_LEVEL_ID = B.WBS_LEVEL_ID
JOIN BCP BC ON BC.BCP_ID = B.BCP_ID
LEFT OUTER JOIN WBS_TREE_MOD W ON L.WBS_LEVEL_ID = W.WBS_LEVEL_ID
LEFT OUTER JOIN VW_SITEPEOPLE P1 ON W.WBS_MANAGER_SNUMBER = P1.SNUMBER
LEFT OUTER JOIN VW_SITEPEOPLE P2 ON W.PCM_SNUMBER = P2.SNUMBER
LEFT OUTER JOIN VW_SITEPEOPLE P3 ON W.PCE_SNUMBER = P3.SNUMBER
ORDER BY L.WBS_LEVEL_NAME, B.REV_NUMBER DESC;
END GET_BASELINE_RPT;
so it turns out I wasn't getting duplicates at all. There were differences in the data but there were so many columns that had the same data that I didn't notice the differences until another review. I will try to restart my query but to be honest I may just put in a filter in my C#.

executing multiple select statements to pick a value in where condition using Case statement

Case statement with select statement as loops in where condition
need to bring values by referring two tables. if the value doesn't exist in table a it has to refer the 2nd table
Sel * from Table A
where city = (case when (sel distinct city from Table A) is null
then (sel city from Table B) end)
expected output is as shown below
Sel * from Table A
where City = 'XYZ'
if value is not present in table A it has to refer Table B statement and show the value in where condition
The one thing you need to be careful with here is to make sure you return a single value in your scalar sub-queries -- (sel distinct city from Table A) and (sel city from Table B). If you can always guarantee that then I think your query will work as is.
A safer way to do it is guarantee you always get one row. Here's one option:
SELECT *
FROM TableA
WHERE City = (
SELECT city
FROM (
-- Get all cities from TableA
SELECT city, 1 AS table_priority
FROM tableA
UNION ALL
-- Get all cities from TableB
SELECT city, 2
FROM tableB
) src
QUALIFY ROW_NUMBER() OVER(ORDER BY src.table_priority, src.city) = 1 -- Return one row
)

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

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

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.

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