我有一个选择像这样:
SELECT * FROM (SELECT DISTICT COL1, COL2, COL3
FROM view a WHERE conditions ....
) QUERY
WHERE CONDITIONS... LIMIT 20 OFFSET 0
我正在从Java执行此操作,我需要此查询返回唯一的ID。 所以我尝试:
SELECT TRIM(CHAR(HEX(GENERATE_UNIQUE()))) AS GUID, QUERY.* FROM (SELECT DISTICT COL1, COL2, COL3
FROM view a WHERE conditions ....
) QUERY
WHERE CONDITIONS... LIMIT 20 OFFSET 0
这个返回一个错误,告诉我我不能在那个地方使用这个功能。 如果我尝试:
SELECT * FROM (SELECT DISTINCT TRIM(CHAR(HEX(GENERATE_UNIQUE()))) AS GUID, COL1, COL2, COL3
FROM view a WHERE conditions ....
) QUERY
WHERE CONDITIONS... LIMIT 20 OFFSET 0
我有重复的行,因为这就像我执行查询时一样DISTINCT
有人知道这样做的方法吗?
我不知道DB2版本(我已经尝试过How to check db2 version中的所有解决方案)
答案 0 :(得分:1)
如果可以使用数字id
,那么仅使用row_number()
:
SELECT CAST(ROW_NUMBER() OVER (ORDER BY COL1, COL2, COL3) as VARCHAR(255)) as unique_id,
QUERY.*
FROM (SELECT DISTICT COL1, COL2, COL3
FROM view a
WHERE conditions ....
) QUERY
WHERE CONDITIONS...
LIMIT 20 OFFSET 0