Teradata median calculation display - teradata

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)

Related

How to replicate the Model Comparison Tool report from Google Analytics to Google BigQuery

I have the following report in the demo account of Google Analytics:
https://analytics.google.com/analytics/web/?utm_source=demoaccount&utm_medium=demoaccount&utm_campaign=demoaccount#/report/bf-roi-calculator/a54516992w87479473p92320289/_u.date00=20211101&_u.date01=20211128&_r.attrSel2=preset6&_r.attrSel1=preset1&_r.attrSel3=preset7/
In this report, we can see the different models of conversion attribution, e.g. Last Interaction, Last Non-Direct Click, and Last Google Ads Click. There are also other models, like First Interaction, Position based. Here's Google's documentation about the multi-channel funnels report:
https://support.google.com/analytics/topic/1191164?hl=en&ref_topic=1631741
So far, I have managed to build the following query:
-- Sessions with source/medium, hits, and page path
WITH table_1 AS (
SELECT
fullVisitorId,
visitStartTime,
CONCAT(fullVisitorId, visitId, date) AS session,
trafficSource.medium,
trafficSource.source,
ANY_VALUE(social.hasSocialSourceReferral) AS social_source_referral,
trafficSource.campaign,
ARRAY_AGG(hitNumber ORDER BY hitNumber) AS hit_number,
ARRAY_AGG(page.pagePath ORDER BY hitNumber) AS page_path
FROM `bigquery-public-data.google_analytics_sample.ga_sessions_*`, UNNEST(hits) AS hits_
WHERE _TABLE_SUFFIX BETWEEN '20170727' AND '20170801'
GROUP BY fullVisitorId, visitStartTime, session, medium, source, campaign),
-- Adding the MCF channel grouping and creating a field that indicates sessions with conversions
table_2 AS (
SELECT
fullVisitorId,
visitStartTime,
CASE
WHEN source = '(direct)' AND (medium = '(not set)' OR medium = '(none)') THEN 'Direct'
WHEN medium = 'organic' THEN 'Organic Search'
WHEN social_source_referral = 'Yes' AND REGEXP_CONTAINS(medium, r'^(social|social-network|social-media|sm|social network|social media)$') THEN 'Social'
WHEN medium = 'email' THEN 'Email'
WHEN medium = 'affiliate' THEN 'Affiliate'
WHEN medium = 'referral' THEN 'Referral'
WHEN REGEXP_CONTAINS(medium, r'^(cpc|ppc|paidsearch)$') THEN 'Paid Search'
WHEN REGEXP_CONTAINS(medium, r'^(cpv|cpa|cpp|content-text)$') THEN 'Other Advertising'
WHEN REGEXP_CONTAINS(medium, r'^(display|cpm|banner)$') THEN 'Display'
ELSE 'Other'
END AS mcf_channel_grouping,
medium,
source,
campaign,
CAST(
EXISTS(
SELECT *
FROM UNNEST(page_path) AS x
WHERE REGEXP_CONTAINS(x, r'^/ordercompleted\.html')
)
AS INT64
) AS conversion
FROM table_1
ORDER BY fullVisitorId
),
-- Filtering by sessions with conversions
table_3 AS (
SELECT *
FROM table_2
WHERE TRUE
QUALIFY COUNTIF(conversion = 1) OVER (PARTITION BY fullVisitorId) > 0
),
-- Adding the attribution models
table_4 AS (
SELECT
fullVisitorId,
DATE(TIMESTAMP_SECONDS(visitStartTime)) AS date,
visitStartTime AS date_sec,
mcf_channel_grouping,
medium,
source,
campaign,
conversion,
CASE
WHEN conversion > 0 AND visitStartTime > LAG(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) THEN '1'
WHEN conversion > 0 AND visitStartTime = FIRST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) THEN '1'
ELSE 'null'
END AS last_touch_attribution,
CASE
WHEN conversion > 0 AND visitStartTime = FIRST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) THEN '1'
WHEN conversion = 0 AND visitStartTime = FIRST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) THEN 'null'
WHEN conversion = 0 AND LAG(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) = FIRST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) THEN 'null'
WHEN SUM(conversion) OVER (PARTITION BY fullVisitorId) > 0 AND visitStartTime < LAST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
AND LEAD(source) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) = 'direct' AND source != 'direct' THEN '1'
WHEN conversion > 0 AND visitStartTime = LAST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AND source != 'direct' THEN '1'
ELSE 'null'
END AS last_non_direct,
CASE
WHEN MAX(conversion) OVER (PARTITION BY fullVisitorId) = 1 AND visitStartTime = FIRST_VALUE(visitStartTime) OVER (PARTITION BY fullVisitorId ORDER BY visitStartTime) THEN '1'
ELSE 'null'
END AS first_touch_attribution,
CASE
WHEN MAX(conversion) OVER (PARTITION BY fullVisitorId) = 1 THEN '1'
ELSE 'null'
END AS any_touch_attribution,
CASE
WHEN MAX(conversion) OVER (PARTITION BY fullVisitorId) = 1 AND source = 'blog' THEN '1'
ELSE 'null'
END AS blog_only
FROM table_3
ORDER BY fullVisitorId, visitStartTime
)
SELECT *
FROM table_4
The issue I have is that the Last Non-Direct model is not calculated correctly and I don't know how to create the look-back window that allows me to set n days prior to conversion.
How could we replicate this report in BigQuery using Standard SQL? Thanks.

Join unique UserIDs with previous month' first time UserIDs

I want to identify users that had a "first_open" event in month a (here: january) and came back to our in month b (here: february) with an "user_engagement" event.
My idea:
1. Create a table with all users who had a "first_open" event
2. Create a table with all users who had a "user_engagement" event
3. Join both tables on userID
4. Count Users who both had a "first_open" event in month a and month b and count all users from january with the "first_open" event
With the following query I am currently overcounting both the users in month a and b, because I am not counting all unqiue users for both event types.
With
users_first_open as (select
user_pseudo_id,
EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS install_month,
event_name as firstopen
FROM
`table.events_*`
where _TABLE_SUFFIX BETWEEN '20190101'
AND '20190108' and event_name = "first_open" and
EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 1
),
user_enagement_next_month as (select
user_pseudo_id,
EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS engagement_month,
event_name as engagament_next_month
FROM
`table.events_*`
where _TABLE_SUFFIX BETWEEN '20190109'
AND '20190116' and event_name = "user_engagement"
and EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 1),
cohort_raw as(
select
user_pseudo_id,
install_month,
engagement_month,
case when firstopen = "first_open" then 1 else 0 end as cohort_count_first_open,
case when engagament_next_month = "user_engagement" then 1 else 0 end as cohort_count_engagement
from
user_enagement_next_month
full join
users_first_open using (user_pseudo_id))--,
select
sum(case when cohort_count_first_open is not null then 1 else 0 end) as users_first_open,
(select sum(case when cohort_count_engagement is not null then 1 else 0 end) as u_engagement_open from cohort_raw where cohort_count_first_open = 1) as users_engagement_open
from cohort_raw
What I tried next was the following: group in table 2 "user_enagement_next_month" by userID, etc.
and create a sum of "first_open" case when and "engagement" case when results. With the later I then included the query to only count users whose count of these two was = 2
-
With
users_first_open as (select
user_pseudo_id,
EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS install_month,
event_name as firstopen
FROM
`table.events_*`
where _TABLE_SUFFIX BETWEEN '20190101'
AND '20190131' and event_name = "first_open" and
EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 1
),
user_enagement_next_month as (select
user_pseudo_id,
EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS engagement_month,
event_name as engagament_next_month
FROM
`table.events_*`
where _TABLE_SUFFIX BETWEEN '20190201'
AND '20190228' and event_name = "session_start"
and EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 2
group by 1,2,3)--,
--cohort_raw as(
select
user_pseudo_id,
install_month,
engagement_month,
case when firstopen = "first_open" then 1 else 0 end as cohort_count_first_open,
case when engagament_next_month = "session_start" then 1 else 0 end as cohort_count_engagement
--case when user_pseudo_id is not null then 1 else 0 end as cohort_count_engagement
from
user_enagement_next_month
full join
users_first_open using (user_pseudo_id)),
cohort_agg as (
select *, cohort_count_first_open+cohort_count_engagement as cohort_sum
from cohort_raw
group by 1,2,3,4,5
order by 6 desc)
select
(select count(*) from users_first_open) as cohort_jan,
(select Sum(cohort_sum) from cohort_agg where cohort_sum = 2) as ret,
sum(case when cohort_count_first_open is not null then 1 else 0 end) as users_first_open,
(select sum(case when cohort_count_engagement is not null then 1 else 0 end) as u_engagement_open from cohort_raw where cohort_count_first_open = 1) as users_engagement_open
from cohort_agg
I expect a return rate of around 20%. My output at the moment is 54%, because in my query I am either overcounting or counting to little, because I assume my join does not work.
Maybe I don't clearly understand what you want, but try this one
with
users_first_open as (
select distinct -- is there duplicates for one user_id?
user_pseudo_id,
extract(
month from
timestamp_micros(user_first_touch_timestamp)
) as install_month
from
`table.events_201901*` -- longer prefixes generally perform better
where
_table_suffix between '01' and '31'
and event_name = 'first_open'
and extract(
month from
timestamp_micros(user_first_touch_timestamp)
) = 1
),
user_enagement_next_month as (
select distinct
user_pseudo_id,
extract(
month from
timestamp_micros(user_first_touch_timestamp)
) as engagement_month
from
`table.events_201902*` -- longer prefixes generally perform better
where
_table_suffix between '01' and '28'
and event_name = 'user_engagement'
and extract(
month from
timestamp_micros(user_first_touch_timestamp)
) = 2
)
select
ufo.install_month,
uenm.engagement_month,
count(*) as first_open_event_users_cnt,
count(uenm.user_pseudo_id) as user_engagement_event_users_cnt
from
users_first_open as ufo
left join user_enagement_next_month as uenm
on ufo.user_pseudo_id = uenm.user_pseudo_id
group by
1, 2

SQL Server Multiple count dates subselect in same query

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

Teradata error Ordered Analytical Function not allowed in group by clause

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

Teradata Orderer Analytical Function not allowed in group by clause

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

Resources