如何仅从Db2中的记录集中选择第一条记录?
示例,我在下面进行了查询:
SELECT NU_DOC_CLIENTE_DIT,
NU_DOC_CLI_IND_CID,
TS_OPERACAO_CID
FROM CLIENTE
WHERE NU_DOC_CLI_IND_CID IN
(SELECT NU_DOC_CLI_IND_CID
FROM CLIENTE
WHERE CO_USERIDOPER_CID = X'0000000000000000')
GROUP BY NU_DOC_CLIENTE_DIT,
NU_DOC_CLI_IND_CID,
TS_OPERACAO_CID;
结果:
---------+---------+---------+---------+---------+---------+------
NU_DOC_CLIENTE_DIT NU_DOC_CLI_IND_CID TS_OPERACAO_CID
---------+---------+---------+---------+---------+---------+------
1 0 2016-10-06-10.17.38.000000
1 0 2016-10-06-11.08.10.000000
1 0 2016-10-06-11.11.12.000000
1 0 2016-10-21-11.20.51.000000
1 0 2016-10-21-14.08.52.000000
2 1 2015-03-06-13.41.00.000000
2 1 2016-10-06-09.55.39.000000
2 1 2016-10-18-14.32.00.000000
3 2 2016-10-18-14.35.11.000000
3 2 2016-10-18-14.36.28.000000
3 2 2016-10-18-14.43.45.000000
我想得到结果:
---------+---------+---------+---------+---------+---------+------
NU_DOC_CLIENTE_DIT NU_DOC_CLI_IND_CID TS_OPERACAO_CID
---------+---------+---------+---------+---------+---------+------
1 0 2016-10-06-10.17.38.000000
2 1 2015-03-06-13.41.00.000000
3 2 2016-10-18-14.35.11.000000
我不能用“FETCH FIRST 1 ROWS ONLY”,因为它给我带来了不必要的结果:
---------+---------+---------+---------+---------+---------+------
NU_DOC_CLIENTE_DIT NU_DOC_CLI_IND_CID TS_OPERACAO_CID
---------+---------+---------+---------+---------+---------+------
1 0 2016-10-06-10.17.38.000000
如何获得每个密钥组只有一行的结果(NU_DOC_CLIENTE_DIT,NU_DOC_CLI_IND_CID,TS_OPERACAO_CID)?
答案 0 :(得分:0)
如果你真的想先没有排序:
select * from (
SELECT NU_DOC_CLIENTE_DIT, NU_DOC_CLI_IND_CID, TS_OPERACAO_CID,
rownumber() over(partition by NU_DOC_CLIENTE_DIT, NU_DOC_CLI_IND_CID) rang
FROM CLIENTE
WHERE CO_USERIDOPER_CID =X'0000000000000000'
) tmp where rang=1
或其他简单
SELECT NU_DOC_CLIENTE_DIT,
NU_DOC_CLI_IND_CID,
min(TS_OPERACAO_CID) TS_OPERACAO_CID
FROM CLIENTE
WHERE CO_USERIDOPER_CID = X'0000000000000000'
GROUP BY NU_DOC_CLIENTE_DIT, NU_DOC_CLI_IND_CID