我想将以下两个查询合并为一个查询。这是两个查询。有人知道这是否可能吗?第二个查询中的汇总让我觉得不,但是我想把它丢给专家看看您的想法。
查询1:
SELECT a.id, a.ord_nb, a.scr,
a.fr,
a.rank,
CASE WHEN tp.tps_jnr_cn > 0 THEN 'Jnrs'
WHEN tp.tps_chld_cn > 0 THEN 'Chld'
WHEN tp.tps_ifnt_cn > 0 THEN 'Ifnt'
WHEN tp.tps_snr_cn > 0 THEN 'Snrs'
ELSE 'Non'
END AS Party_Seg,
CASE WHEN gx.id IS NOT NULL THEN 1 ELSE 0 END
AS rpt
FROM xi.aff a
LEFT JOIN oper.d_tps tp
ON tp.tps_id = a.ord_nb
LEFT JOIN XI.gst_exprnc gx
ON a.id = gx.id
AND tp.strt_dt > gx.strt_dts + INTERVAL '30' DAY
AND gx.exprnc_typ_id IN (309) --Park Experience
QUALIFY Row_Number() Over (
PARTITION BY ord_nb, scr, fr
ORDER BY rank DESC) = 1
ORDER BY ord_nb, fr, scr
查询2:
SELECT a.ord_nb,
a.fr, Max(a.rank), Min(a.rank),
Max(a.rank) - Min(a.rank)
FROM (
SELECT ord_nb, fr, rank
FROM xi.aff
QUALIFY Row_Number() Over (
PARTITION BY ord_nb, fr, scr
ORDER BY rank DESC) = 1
) a
GROUP BY 1, 2
ORDER BY ord_nb, fr
答案 0 :(得分:1)
考虑将两个查询作为派生表联接:
SELECT t1.*, t2.Min_Rank, t2.Max_Rank, t2.Max_Min_Diff_Rank
FROM
([--INSERT 1ST QUERY REMOVING ORDER BY AT END--]) t1
INNER JOIN
([--INSERT 2ND QUERY REMOVING ORDER BY AT END AND NAMING RANK COLUMNS--]) t2
ON t1.ord_nb = t2.ord_nbAND t2.fr = t2.fr
ORDER BY t1.ord_nb, t1.fr, t1.scr