I am getting the error
Teradata Orderer Analytical Function not allowed in group by clause
when I run this query:
SELECT
CC.CASE_ID as CASE_ID,
FIRST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as FST_AGNT_CASE_OWN_NM,
FIRST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as FST_AGNT_PRFL_NM,
LAST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_CASE_OWN_NM,
LAST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_PRFL_NM,
case when CC.CASE_OWN_NM is not null then MIN(CC.REC_DTTM_PST) end as FST_AGNT_EDIT_DTTM,
case when CC.CASE_OWN_NM is not null then MAX(CC.REC_DTTM_PST) end as LST_AGNT_EDIT_DTTM,
case when CC.CASE_STS_CD='Open' then MIN(CC.REC_DTTM_PST) end as CASE_OPEN_DTTM,
case when CC.CASE_STS_CD in ('Closed', 'Auto Closed') then MIN(CC.REC_DTTM_PST) end as CASE_CLSE_OR_AUTO_CLSE_DTTM,
count(distinct CC.CASE_OWN_NM) as CASE_OWN_CHGS_IN_NUM,
LAST_VALUE(CC.ESCL_RSN_TXT) OVER(PARTITION BY CC.CASE_ID) as ESCL_RSN_TXT,
LAST_VALUE(CC.ESCL_DTLS_TXT) OVER(PARTITION BY CC.CASE_ID) as ESCL_DTLS_TXT
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
group by
1,2,3,4,5
sample 10
I tried the answer by anwaar_hell and modified it a bit with a few more columns, but I'm still getting an error:
ordered analytical function not allowed in subqueries
Query:
SELECT
distinct CC.CASE_ID as CASE_ID,
CCC.FST_AGNT_CASE_OWN_NM,
CCC.FST_AGNT_PRFL_NM,
CCC.LST_AGNT_CASE_OWN_NM,
CCC.LST_AGNT_PRFL_NM,
CCC.FST_CHNL_NM,
CCC.LST_CHNL_NM,
CCC.LST_VEND_NM,
case when MAX(CC.CASE_TFR_TO_L2)='1' then 'Yes' else 'No' end as ESCL_FL,
case when CC.LSTMOD_BY_AGNT_ROLE_NM='L1' then count(distinct CC.LSTMOD_BY_AGNT_ROLE_NM) end as XFER_BTWN_L1_NUM,
MAX(CC.CASE_SAVED_ORD_NUM) as SAVES_OR_MODS_ON_CASE_NUM,
MIN(CC.REC_DTTM_PST) as FST_QUE_TIME_IN_SECS2,
case when CC.RMTE_ASST_USED_IN>=1 then 'Yes' else 'No' end as RMTE_SESS_FL,
case when CC.OUTB_CALL_TYPE_CD='Outbound' then 1 else 0 end as IS_OUTB_CALL_TYPE_IN,
case when CC.L2CALL_BK_SCEHDULED_PST_DT is NOT NULL then 'Yes' else 'No' end as L2_OUTB_CALL_SCHD_FL,
MAX(CC.L2CALL_BK_SCEHDULED_PST_DT) as L2_CLBCK_SCEHDULED_DTTM,
CC.PU_DTTM as LMI_PU_DTTM,
CC.CLS_DTTM as LMI_CLS_DTTM,
( select
FIRST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as FST_AGNT_CASE_OWN_NM,
FIRST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as FST_AGNT_PRFL_NM,
LAST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_CASE_OWN_NM,
LAST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_PRFL_NM,
FIRST_VALUE(CC.CHNL_NM) OVER(PARTITION BY CC.CASE_ID ) as FST_CHNL_NM,
LAST_VALUE(CC.CHNL_NM) OVER(PARTITION BY CC.CASE_ID) as LST_CHNL_NM,
LAST_VALUE(CCE.VEND_NM) OVER(PARTITION BY CC.CASE_ID) as LST_VEND_NM
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='23268760'
)
group by
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
You need to calculate the first and last values in derive table and then group by
SELECT CC.CASE_ID as CASE_ID, CC.FST_AGNT_CASE_OWN_NM, CC.FST_AGNT_PRFL_NM,
CC.LST_AGNT_CASE_OWN_NM, CC.LST_AGNT_PRFL_NM,CC.ESCL_RSN_TXT,CC.ESCL_DTLS_TXT,
case
when CC.CASE_OWN_NM is not null then MIN(CC.REC_DTTM_PST)
end as FST_AGNT_EDIT_DTTM,
case
when CC.CASE_OWN_NM is not null then MAX(CC.REC_DTTM_PST)
end as LST_AGNT_EDIT_DTTM,
case
when CC.CASE_STS_CD='Open' then MIN(CC.REC_DTTM_PST)
end as CASE_OPEN_DTTM,
case
when CC.CASE_STS_CD in ( 'Closed', 'Auto Closed') then MIN(CC.REC_DTTM_PST)
end as CASE_CLSE_OR_AUTO_CLSE_DTTM, count( distinct CC.CASE_OWN_NM) as CASE_OWN_CHGS_IN_NUM
FROM (select FIRST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID )as FST_AGNT_CASE_OWN_NM,
FIRST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID)as FST_AGNT_PRFL_NM,
LAST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_CASE_OWN_NM,
LAST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_PRFL_NM
,LAST_VALUE(CC.ESCL_RSN_TXT) OVER(PARTITION BY CC.CASE_ID )as ESCL_RSN_TXT,
LAST_VALUE( CC.ESCL_DTLS_TXT) OVER(PARTITION BY CC.CASE_ID ) as ESCL_DTLS_TXT
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
group by 1,2,3,4,5 ,6,7
sample 1
Related
below query only give minor difference between weighted average and normal average, what should be good model and how to get probability of job failing in peoplesoft
SELECT DISTINCT TRUNC(SUM(ntile1) over(partition BY prcsname,status order by status)/SUM(ntile1) over(partition BY prcsname),2)*100 as weighted_pecentage,
TRUNC(count(1) over(partition BY prcsname,status order by status)/count(1) over(partition BY prcsname),2)*100 as normal_percentage,
prcsname,
status
FROM
(SELECT 100-ntile(100) over (partition by prcsname order by prcsname ,prcsinstance DESC)AS ntile1,
DECODE(runstatus,9,'Success','UnSuccess') AS status ,
p.*
FROM psprcsrqst p
WHERE prcsname LIKE '%'
ORDER BY
prcsinstance desc, prcsname,status DESC
) p2 order by prcsname,
status;
!! detail
SELECT 100-ntile(100) over (partition by prcsname order by prcsinstance desc )AS ntile1,
DECODE(runstatus,9,'Success','UnSuccess') AS status ,
p.*
FROM psprcsrqst p
WHERE prcsname LIKE '%'
ORDER BY prcsname,
prcsinstance desc, status DESC;
In DB2 is there a way to basically say:
case when sku (select * from table1 where tb1field = 'SMOMD') then 'True' end
Okay so this is my query so far, I've been going at this for at least a month now so any help would be great.
select tb4.customer, tb4.sku, tb4.qty, tb4.retqty, tb4.stipqty, tb4.lastdate, tb4.firstdate, tb4.stipdate
from(
--Table 4
select tb3.Customer as Customer, tb3.sku as SKU, tb3.qty as Qty, tb3.retqty as RetQty, tb3.stipqty as STIPQty,
case when tb3.lastdate is null then '00/0000' else substr(tb3.lastdate,5,2)||'/'||substr(tb3.lastdate,1,4) end as LastDate,
case when tb3.firstdate is null then '00/0000' else substr(tb3.firstdate,5,2)||'/'||substr(tb3.firstdate,1,4) end as FirstDate,
case when tb3.stipdate is null then '00/0000' else substr(tb3.stipdate,5,2)||'/'||substr(tb3.stipdate,1,4) end as STIPDate
from(
--Table 3
select tb2.Customer as Customer, tb2.SKU as SKU, tb2.Qty as Qty, tb2.RetQty as RetQty, tb2.STIPQty as STIPQty,
max(case when tb2.TranID in ('010','100') then tb2.datenum end) as LastDate,
min(case when tb2.TranID in ('010','100') then tb2.datenum end) as FirstDate,
case when tb2.RC = '4M' then tb2.datenum end as STIPDate
from(
--Table 2
select tb1.Customer as Customer, tb1.SKU as SKU,
sum(case when tb1.TranID in ('010','100') then abs(tb1.OrdNet) else '0' end) as Qty,
sum(case when tb1.TranID = '500' and tb1.rc != '4M' then abs(tb1.OrdNet) else '0' end) as RetQty,
count(case when tb1.rc = '4M' then tb1.sku end) as STIPQty,
tb1.datenum as datenum, tb1.TranID as tranid, tb1.RC as rc
from(
--Table 1
select distinct stkund as Customer, sthptg||space(1)||stmodl||space(1)||stvari||space(1)||stfarb||space(1)||stgroe as SKU,
stvorg as TranID, stggru as RC, stprg09 as PG9, stprg08 as PG8, stperi as datenum, ormne1 as OrdNet
from st_usus.s_stati_pv
join caspdtau.cospf440 on stadrn = jadr40
where trim(stvert) in ('111S','122S')
and sthptg != 'V'
and aktv40 = 'A'
and stprg01 in ('01','04')
and stprg02 = '01'
and stvorg in ('500','010','100')
and stperi >= '20160100'
) as tb1
group by tb1.Customer, tb1.SKU, tb1.datenum, tb1.tranid, tb1.rc
) as tb2
group by tb2.customer, tb2.sku, tb2.qty, tb2.retqty, tb2.stipqty, tb2.tranid, tb2.rc, tb2.datenum
) as tb3
group by tb3.customer, tb3.sku, tb3.qty, tb3.retqty, tb3.stipqty, tb3.lastdate, tb3.firstdate, tb3.stipdate
) as tb4
order by tb4.Customer, tb4.sku
I'm not going to try to decipher exactly what you're trying to do...
Some general advice, rather than using Nested Table Expressions (NTE)
select <..> from (select <...>from mytable)
Consider Common Table Expressions (CTE)
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
, table2 as (select <...> from table1)
, table3 as (select <...> from table2)
, table4 as (select <....> from table3)
select <...> from table4;
Each CTE (ie. tableX) can refer to a prior CTE or a physical table/view as needed. The final select can refer to one or more CTE's along with one or more physical tables or views.
Nice thing about building with CTE's, is that you can check your results after each step..
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
select * from table1;
I'm trying to figure out a way to pull Order Counts per customer id as well as date of first and last order within a date range from an Orders table where each order has both a buyer_id and seller_id. The Orders table contains OrderNumber, Buyer_ID, Seller_ID, OpenDate, ClosedDate. I can run the following queries individually to achieve my goals, but I would like to have everything in the same query if possible.
Order_Table:
OrderNumber, Buyer_ID, Seller_ID, OpenDate, ClosedDate
Buyer_ID Orders:
select Buyer_ID, COUNT(*)as BuyerOrders
from
(
select Buyer_ID
from Orders
where OpenDate between #StartDate and #EndDate
)
a
group by Buyer_ID
Seller_ID Orders:
select Seller_ID, COUNT(*)as SellerOrders
from
(
select Seller_ID
from Orders
where OpenDate between #StartDate and #EndDate
)
a
group by Seller_ID
Dates of First and Last Order within that range: ??
Any input is greatly appreciated!
Since the result is a union and the same customer_id may have an entry as both buyer and seller, how can I put the information in the same row? My first attempt was to create a temporary table from the result of the Union, but I'm drawing a blank on how to display Buyer OrderCount, Seller OrderCount etc on the same row for each Customer_ID in the resulting table.
select 'Buyer' as Type,
Buyer_ID ID,
Count(*) OrderCount,
Min(OpenDate) FirstOrder,
Max(OpenDate) LastOrder
from Orders
where OpenDate between #StartDate and #EndDate
group by Buyer_ID
union
select 'Seller',
Seller_ID,
Count(*),
Min(OpenDate) FirstOrder,
Max(OpenDate) LastOrder
from Orders
where OpenDate between #StartDate and #EndDate
group by Seller_ID
[EDIT] Yes, a little bit of a cheek accepting my answer, then un-accepting it and changing the question! Anyway, try the following:
;with BuyerFirst (Buyer_ID, RowNum, BuyerCount, OrderID, OpenDate)
As
(select Buyer_ID,
ROW_NUMBER() over (partition by Buyer_ID order by OpenDate, OrderID) as RowNum,
count(*) over (partition by Buyer_ID) As BuyerCount,
OrderID,
OpenDate
from Orders
where OpenDate between #StartDate and #EndDate),
BuyerLast (Buyer_ID, RowNum, OrderID, OpenDate)
As
(select Buyer_ID,
ROW_NUMBER() over (partition by Buyer_ID order by OpenDate Desc, OrderID Desc) as RowNum,
OrderID,
OpenDate
from Orders
where OpenDate between #StartDate and #EndDate),
SellerFirst (Seller_ID, RowNum, SellerCount, OrderID, OpenDate)
As
(select Seller_ID,
ROW_NUMBER() over (partition by Seller_ID order by OpenDate, OrderID) as RowNum,
count(*) over (partition by Buyer_ID) As SellerCount,
OrderID,
OpenDate
from Orders
where OpenDate between #StartDate and #EndDate),
SellerLast (Seller_ID, RowNum, OrderID, OpenDate)
As
(select Seller_ID,
ROW_NUMBER() over (partition by Seller_ID order by OpenDate Desc, OrderID Desc) as RowNum,
OrderID,
OpenDate
from Orders
where OpenDate between #StartDate and #EndDate)
select c.*,
bf.BuyerCount,
bf.OpenDate As BuyerFirstOrderDate,
bf.OrderID As BuyerFirstOrderID,
bl.OpenDate As BuyerLastOrderDate,
bl.OrderID As BuyerLastOrderID,
sf.SellerCount,
sf.OpenDate As SellerFirstOrderDate,
sf.OrderID As SellerFirstOrderID,
sl.OpenDate As SellerLastOrderDate,
sl.OrderID As SellerLastOrderID
from Customers c
left join BuyerFirst bf on c.CustomerID = bf.Buyer_ID and bf.RowNum = 1
left join SellerFirst sf on c.CustomerID = sf.Seller_ID and sf.RowNum = 1
left join BuyerLast bl on c.CustomerID = bl.Buyer_ID and bl.RowNum = 1
left join SellerLast sl on c.CustomerID = sl.Seller_ID and sl.RowNum = 1
The cod below gives the output as shown. I would like to display the median values for all the respective rows instead of "?". What am I doing wrong?
It should display the median value.
SELECT
sales_segment,
pickup_yyyymm,
Days,
COUNT(*) over(partition by sales_segment,pickup_yyyymm order by sales_segment,pickup_yyyymm desc) AS no_of_records,
SUM(Days) over(partition by sales_segment,pickup_yyyymm order by sales_segment,pickup_yyyymm desc) AS sum_days,
AVERAGE(Days) over(partition by sales_segment,pickup_yyyymm order by sales_segment,pickup_yyyymm desc) AS AVG_days,
Min(Days) over(partition by sales_segment,pickup_yyyymm order by sales_segment,pickup_yyyymm desc) AS Min_days,
MAX (Days) OVER(PARTITION BY sales_segment,pickup_yyyymm ORDER BY sales_segment,pickup_yyyymm DESC) AS Max_days,
CASE
WHEN ROW_NUMBER( ) OVER (PARTITION BY sales_segment,pickup_yyyymm ORDER BY Days) = COUNT(*) OVER (PARTITION BY sales_segment,pickup_yyyymm) / 2 +1
THEN
CASE
WHEN COUNT (*) OVER (PARTITION BY sales_segment,pickup_yyyymm) MOD 2=1 THEN Days
ELSE AVERAGE(Days) OVER(PARTITION BY sales_segment,pickup_yyyymm ORDER BY Days ROWS 1 PRECEDING)
END
END AS Median_Days
FROM
(SELECT
sales_segment,
pickup_yyyymm,
Days
FROM
(SELECT
A.shp_pro_nbr,
CASE
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 and b.sales_terr_nbr in (20,21,22,23,24,25,26,27,28,29,70,71,72,73,74,75,76,77,78,79) then 'FSAD'
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 and b.sales_terr_nbr in (30,31,32,33,34,35,36,37,38,39,50,51,52,53,54,55,56,57,58,59) then 'FSMD'
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 and b.sales_terr_nbr in (40,41,42,43,44,45,46,47,48,49) then 'FSSD'
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 then 'FS Other'
when b.sales_div_nbr=1 and b.sales_grp_nbr=2 and b.sales_org_nbr=7 and b.sales_area_nbr=3 then 'BSF'
when b.sales_div_nbr=1 and b.sales_grp_nbr=2 and b.sales_org_nbr=7 then 'BSI'
when b.sales_div_nbr=1 and b.sales_grp_nbr=2 and b.sales_org_nbr=8 then 'Presls'
when b.sales_div_nbr=2 then 'WWS'
when b.sales_div_nbr=3 then 'Specialty'
when b.sales_div_nbr=8 then 'Non-US'
when b.sales_div_nbr=90 then 'MKTG'
when b.sales_div_nbr=80 then 'WWS'
else 'None' end as sales_segment,
--A.eff_dt,
--A.pckup_dt,
SUBSTR( CAST(CAST (A.pckup_dt AS DATE) AS DATE FORMAT 'yyyy/mm/dd'),1,7) AS pickup_yyyymm,
(CAST( A.eff_dt AS DATE) - CAST (A.pckup_dt AS DATE) ) AS Days
FROM ISH_FEDXFGT_PROD_VIEW_DB.fxf_ship_rev_comp A
INNER JOIN UI_ISH_PROD_DB.sales_quarter_end_alignment B
ON A.payor_cust_nbr = B.cf_cust_nbr AND B.align_typ_cd ='P'AND fscl_qtr_nbr = 4 AND fscl_yr_nbr = 2016 AND B.prim_cvge_flg= 'Y'
AND CAST(A.pckup_dt AS DATE) BETWEEN ADD_MONTHS(CURRENT_DATE,-24) AND CURRENT_DATE
GROUP BY
1,2,3,4
) a
GROUP BY 1,2,3)b
--QUALIFY ROW_NUMBER( ) OVER (PARTITION BY b. sales_segment,b.pickup_yyyymm ORDER BY b.days)= COUNT(*) OVER PARTITION BY b.sales_segment,b. pickup_yyyymm) /2+1;
--GROUP BY 1,2;
--ORDER BY 1,2
The last query in my post on MEDIAN shows how to get the it as OLAP result, you need to add another nesting level:
SELECT
sales_segment,
pickup_yyyymm,
...,
MIN(Median_Days) over(partition by sales_segment,pickup_yyyymm) AS Median_Days
FROM
(
SELECT
sales_segment,
pickup_yyyymm,
Days,
COUNT(*) over(partition by sales_segment,pickup_yyyymm) AS no_of_records,
SUM(Days) over(partition by sales_segment,pickup_yyyymm) AS sum_days,
AVERAGE(Days) over(partition by sales_segment,pickup_yyyymm) AS AVG_days,
Min(Days) over(partition by sales_segment,pickup_yyyymm) AS Min_days,
MAX (Days) OVER(PARTITION BY sales_segment,pickup_yyyymm) AS Max_days,
CASE
WHEN ROW_NUMBER( ) OVER (PARTITION BY sales_segment,pickup_yyyymm ORDER BY Days) = COUNT(*) OVER (PARTITION BY sales_segment,pickup_yyyymm) / 2 +1
THEN
CASE
WHEN COUNT (*) OVER (PARTITION BY sales_segment,pickup_yyyymm) MOD 2=1 THEN Days
ELSE AVERAGE(Days) OVER(PARTITION BY sales_segment,pickup_yyyymm ORDER BY Days ROWS 1 PRECEDING)
END
END AS Median_Days
FROM
(
SELECT
sales_segment,
pickup_yyyymm,
Days
FROM
(
SELECT
A.shp_pro_nbr,
CASE
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 and b.sales_terr_nbr in (20,21,22,23,24,25,26,27,28,29,70,71,72,73,74,75,76,77,78,79) then 'FSAD'
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 and b.sales_terr_nbr in (30,31,32,33,34,35,36,37,38,39,50,51,52,53,54,55,56,57,58,59) then 'FSMD'
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 and b.sales_terr_nbr in (40,41,42,43,44,45,46,47,48,49) then 'FSSD'
when b.sales_div_nbr=1 and b.sales_grp_nbr<>2 then 'FS Other'
when b.sales_div_nbr=1 and b.sales_grp_nbr=2 and b.sales_org_nbr=7 and b.sales_area_nbr=3 then 'BSF'
when b.sales_div_nbr=1 and b.sales_grp_nbr=2 and b.sales_org_nbr=7 then 'BSI'
when b.sales_div_nbr=1 and b.sales_grp_nbr=2 and b.sales_org_nbr=8 then 'Presls'
when b.sales_div_nbr=2 then 'WWS'
when b.sales_div_nbr=3 then 'Specialty'
when b.sales_div_nbr=8 then 'Non-US'
when b.sales_div_nbr=90 then 'MKTG'
when b.sales_div_nbr=80 then 'WWS'
else 'None' end as sales_segment,
--A.eff_dt,
--A.pckup_dt,
SUBSTR( CAST(CAST (A.pckup_dt AS DATE) AS DATE FORMAT 'yyyy/mm/dd'),1,7) AS pickup_yyyymm,
(CAST( A.eff_dt AS DATE) - CAST (A.pckup_dt AS DATE) ) AS Days
FROM ISH_FEDXFGT_PROD_VIEW_DB.fxf_ship_rev_comp A
INNER JOIN UI_ISH_PROD_DB.sales_quarter_end_alignment B
ON A.payor_cust_nbr = B.cf_cust_nbr AND B.align_typ_cd ='P'AND fscl_qtr_nbr = 4 AND fscl_yr_nbr = 2016 AND B.prim_cvge_flg= 'Y'
AND CAST(A.pckup_dt AS DATE) BETWEEN ADD_MONTHS(CURRENT_DATE,-24) AND CURRENT_DATE
GROUP BY 1,2,3,4
) a
GROUP BY 1,2,3
)b
) as dt
I removed all the order by sales_segment,pickup_yyyymm desc because it's not needed.
Another remark on the pickup_yyyymm calculation, you don't need a substring:
TRIM(CAST (A.pckup_dt FORMAT 'yyyy/mm')) AS pickup_yyyymm,
Would be more efficient if there's no cast to string at all:
EXTRACT(YEAR FROM A.pckup_dt) * 100 + EXTRACT(MONTH FROM A.pckup_dt)
I'm getting the error:
Ordered Analytical Function not allowed in group by clause
for below query.
SELECT
CC.CASE_ID as CASE_ID,
FIRST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID )as FST_AGNT_CASE_OWN_NM,
FIRST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID)as FST_AGNT_PRFL_NM,
LAST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_CASE_OWN_NM,
LAST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_PRFL_NM,
case when CC.CASE_OWN_NM is not null then MIN(CC.REC_DTTM_PST) end as FST_AGNT_EDIT_DTTM,
case when CC.CASE_OWN_NM is not null then MAX(CC.REC_DTTM_PST) end as LST_AGNT_EDIT_DTTM,
case when CC.CASE_STS_CD='Open' then MIN(CC.REC_DTTM_PST) end as CASE_OPEN_DTTM,
case when CC.CASE_STS_CD in ( 'Closed', 'Auto Closed') then MIN(CC.REC_DTTM_PST) end as CASE_CLSE_OR_AUTO_CLSE_DTTM,
count( distinct CC.CASE_OWN_NM) as CASE_OWN_CHGS_IN_NUM,
--IIF((MAX(CC.CASE_TFR_TO_L2) = 1),'Yes','No') as ESCL_FL,
LAST_VALUE(CC.ESCL_RSN_TXT) OVER(PARTITION BY CC.CASE_ID )as ESCL_RSN_TXT,
LAST_VALUE( CC.ESCL_DTLS_TXT) OVER(PARTITION BY CC.CASE_ID ) as ESCL_DTLS_TXT
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
group by 1,2,3,4,5
sample 1
SELECT CC.CASE_ID as CASE_ID, CC.FST_AGNT_CASE_OWN_NM, CC.FST_AGNT_PRFL_NM,
CC.LST_AGNT_CASE_OWN_NM, CC.LST_AGNT_PRFL_NM,CC.ESCL_RSN_TXT,CC.ESCL_DTLS_TXT,
case
when CC.CASE_OWN_NM is not null then MIN(CC.REC_DTTM_PST)
end as FST_AGNT_EDIT_DTTM,
case
when CC.CASE_OWN_NM is not null then MAX(CC.REC_DTTM_PST)
end as LST_AGNT_EDIT_DTTM,
case
when CC.CASE_STS_CD='Open' then MIN(CC.REC_DTTM_PST)
end as CASE_OPEN_DTTM,
case
when CC.CASE_STS_CD in ( 'Closed', 'Auto Closed') then MIN(CC.REC_DTTM_PST)
end as CASE_CLSE_OR_AUTO_CLSE_DTTM, count( distinct CC.CASE_OWN_NM) as CASE_OWN_CHGS_IN_NUM
FROM (select FIRST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID )as FST_AGNT_CASE_OWN_NM,
FIRST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID)as FST_AGNT_PRFL_NM,
LAST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_CASE_OWN_NM,
LAST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID) as LST_AGNT_PRFL_NM
,LAST_VALUE(CC.ESCL_RSN_TXT) OVER(PARTITION BY CC.CASE_ID )as ESCL_RSN_TXT,
LAST_VALUE( CC.ESCL_DTLS_TXT) OVER(PARTITION BY CC.CASE_ID ) as ESCL_DTLS_TXT
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
group by 1,2,3,4,5 ,6,7
sample 1