Selecting one or N records within a sorted subset of a request is a common need.
This is handle in SQL with RANK OVER PARTITION clause and the VERTICA documentation for it is :
https://www.vertica.com/docs/9...nalyticFunctions.htmWebFOCUS is capable (sometimes if you are carefull with optilization) to generate this type of request. Here is one example
The main thing to remember is that you need to be able to sort your subset over a "unique key" to propery rank records and then be able to filter them.
TABLE FILE BQCITIE
PRINT
NUM_TIE
AGE_TIE
NUM_GUI_CTR
REF_CTR_EXI
COD_BQE_CTR AS 'COD_BQE_CTR'
BY NUM_GRP_CLI
BY HIGHEST 1 FLG_IP BY LOWEST 1 REF_CTR_EXI WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_décédés ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_RISK_EetF ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_PM ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_NON_RESIDENT ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_NPAI ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_INTERDITS ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_INCAPABLES ;
WHERE BQCITIE.FOLDERFILTER.EXCLUS_TIERS_SAL_GRP ;
WHERE COD_PRD EQ 'B 27400';
WHERE COD_ECV_CTR EQ '4';
WHERE NUM_GUI_CTR EQ '01001';
WHERE NUM_GRP_CLI IN ('GRP0300000826720', 'GRP0300000825495', 'GRP0300000820375','GRP0300000837270','GRP2000000989182','GRP2000000991365');
ON TABLE HOLD AS H1 FORMAT SQL_SCRIPT
END
TABLE FILE H1
PRINT *
BY NUM_GRP_CLI NOPRINT
END
would generate this
SELECT
T1."SK001_NUM_GRP_CLI",
T1."SK002_FLG_IP",
T1."SK003_REF_CTR_EXI",
T1."VB001_PRN_NUM_TIE",
T1."VB002_PRN_AGE_TIE",
T1."VB003_PRN_NUM_GUI_CTR",
T1."VB004_PRN_REF_CTR_EXI",
T1."VB005_PRN_COD_BQE"
FROM
( /* vvv */
SELECT
"SK001_NUM_GRP_CLI",
"SK002_FLG_IP",
"SK003_REF_CTR_EXI",
"VB001_PRN_NUM_TIE",
"VB002_PRN_AGE_TIE",
"VB003_PRN_NUM_GUI_CTR",
"VB004_PRN_REF_CTR_EXI",
"VB005_PRN_COD_BQE"
FROM (
SELECT
T1."NUM_GRP_CLI" AS "SK001_NUM_GRP_CLI",
T1."FLG_IP" AS "SK002_FLG_IP",
DENSE_RANK() OVER( PARTITION BY T1."NUM_GRP_CLI" ORDER BY T1."FLG_IP" DESC NULLS LAST) AS "RNK002", T10."REF_CTR_EXI" AS "SK003_REF_CTR_EXI",
DENSE_RANK() OVER( PARTITION BY T1."NUM_GRP_CLI", T1."FLG_IP" ORDER BY T10."REF_CTR_EXI" ASC NULLS FIRST) AS "RNK003", T1."NUM_TIE" AS "VB001_PRN_NUM_TIE",
(CASE (T1."DAT_NAI") WHEN DATE '0001-01-01' THEN NULL ELSE
TRUNC((EXTRACT(DAY FROM (TRUNC(DATE '2018-12-12') -
TRUNC(T1."DAT_NAI"))) / 365.25),0) END) AS
"VB002_PRN_AGE_TIE",
T10."NUM_GUI_CTR" AS "VB003_PRN_NUM_GUI_CTR",
T10."REF_CTR_EXI" AS "VB004_PRN_REF_CTR_EXI",
T12."COD_BQE" AS "VB005_PRN_COD_BQE"
FROM
( ( ( DAQ1V0.VDCQT10_TIE T1
LEFT OUTER JOIN
DAQ1V0.VDCQTXC_EQUTIE T10
ON T10."IDT_PI" = T1."IDT_PI" AND
(T10."DAT_FIN_REL" = DATE '9999-12-31') )
LEFT OUTER JOIN
DAQ1V0.VDSQC00_ICN T12
ON T12."IDT_AC" = T10."IDT_AC" )
LEFT OUTER JOIN
DAQ1V0.VDCQT14_RISTIE T38
ON T38."IDT_PI" = T1."IDT_PI" )
WHERE
(T1."NUM_GRP_CLI" IN('GRP0300000826720', 'GRP0300000825495',
'GRP0300000820375', 'GRP0300000837270', 'GRP2000000989182',
'GRP2000000991365')) AND
(T1."COD_SAL_GRP" NOT IN('G', 'C')) AND
(T1."COD_CPC_JUR" = ' ') AND
(T1."FLG_PND" <> 'O') AND
(T1."COD_PAY" = '000') AND
(T1."COD_TYP_TIE" = '1') AND
(T1."COD_RIS_FIN" NOT IN('E=', 'E+', 'E-', 'F')) AND
(((T1."COD_TYP_TIE" = '1') AND (T1."DAT_DCS" = DATE
'0001-01-01')) OR ((T1."COD_TYP_TIE" = '2') AND (T1."DAT_DSL"
= DATE '0001-01-01'))) AND
(T1."FLG_PCE_PDN_TIE" = 'O') AND
(T10."NUM_GUI_CTR" = '01001') AND
(T10."COD_ECV_CTR" = '4') AND
(T10."COD_PRD" = 'B 27400') AND
(T38."FLG_EXL_MKT" = 'N')
) X
WHERE
"RNK002" <= 1 AND "RNK003" <= 1 ) /* ^^^ */ T1
ORDER BY
T1."SK001_NUM_GRP_CLI";
Based on you example I suggest you contact me directly with your example