Can anyone please help me modify this below code with using analytical/aggregate function aas i have to use it creating a materialized view with fast on commit refresh option.
SELECT col1, col2
FROM ( SELECT DISTINCT table_A.column3, table_b.column4,
CASE WHEN table_b.col5 = 'A' THEN RANK() OVER (PARTITION BY table_A.col1, table_b.col5 ORDER BY table_b.col6, table_b.col7 DESC) END AS rnk_a,
CASE WHEN table_b.col5 = 'Z' THEN RANK() OVER (PARTITION BY table_A.col1, table_b.col5 ORDER BY table_b.col6 DESC, table_b.col7 DESC) END AS rnk_z
FROM table_A
INNER JOIN table_B sc ON tale_B.col4 = table_a.col3
)
WHERE (RNK_A = 1) OR (RNK_Z = 1)
*sample dataset:*
id col5 col6 col7
A1A150 A 001 20245
A1A150 A 002 20246
A1A150 A 002 20244
A1A150 Z 001 1023
A1A150 Z 002 1022
Expected resultset:
id col5 col6 col7
A1A150 A 002 20246
A1A150 Z 001 1023
**If multiple records for col5 = 'A' or col5 = 'Z' found then: take min(col6) and max(col7)
when multiple records with loc = 'A' or loc = 'Z' with same col5 and diff col7 value is found, the max logic is not working. please correct me where i'm going wrong.
SELECT col1, col2 FROM ( SELECT DISTINCT table_A.column3, table_b.column4,
CASE WHEN table_b.col5 = 'A' AND table_b.col7 = (SELECT MAX(table_b.col7) FROM table_b cp1 WHERE cp1.id = table_A.col1 GROUP BY table_A.col1)THEN 1 ELSE 0 END AS rnk_a,
CASE WHEN table_b.col5 = 'Z' AND table_b.col7 = (SELECT MAX(table_b.col7) FROM table_b cp1 WHERE cp1.id = table_A.col1 GROUP BY table_A.col1)THEN 1 ELSE 0 END AS rnk_z
FROM table_A
INNER JOIN table_B sc ON tale_B.col4 = table_a.col3
)
WHERE (RNK_A = 1) OR (RNK_Z = 1)
You can achieve this using sub query -
SELECT T1.id, T1.col5, T1.col6, T1.col7
FROM table_A T1
JOIN (SELECT id, col5, MAX(col7) col7
FROM table_B
GROUP BY id, col5) T2
ON T1.id = T2.id
AND T1.col5 = T2.col5
AND T1.col7 = T2.col7
You can use a CASE
and ROW_NUMBER()
to rank the records in a subquery (or RANK()
, if that better fits your use case), and then filter in the outer query:
SELECT id, col5, col6, col7
FROM (
SELECT
t.*,
CASE col5
WHEN 'A' THEN ROW_NUMBER() OVER(PARTITION BY col5 ORDER BY col6 DESC, col7 DESC)
WHEN 'Z' THEN ROW_NUMBER() OVER(PARTITION BY col5 ORDER BY col6 ASC, col7 DESC)
END rn
FROM mytable t
) x
WHERE rn = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.