为什么sql union不起作用?

时间:2013-03-15 13:43:07

标签: sql oracle hibernate

你能告诉我,为什么在下面的运行查询之后我只收到第一次选择的回复?不考虑UNION语句后的第二次选择。你知道为什么吗?

SELECT new ViolationSummaryByDurationDTO('Date discovered to date reported' as durationMeasure, 
    v.counName, v.siteNumber, 
    COUNT(*) as number, 
    MAX(NVL(v.dateReported - v.dateIdentified, 0)) as maximum, 
    MIN(NVL(v.dateReported - v.dateIdentified, 0)) as minimum, 
    AVG(v.dateReported - v.dateIdentified) as mean)
FROM Violation v 
WHERE v.trial.id = 5 AND v.dateIdentified IS NOT NULL AND v.dateReported IS NOT NULL 
GROUP BY ROLLUP (v.counName, v.siteNumber) 
UNION 
SELECT new ViolationSummaryByDurationDTO('Date reported to date confirmed' as durationMeasure, 
    v.counName, v.siteNumber, 
    COUNT(*) as number, 
    MAX(NVL(v.dateConfirmed - v.dateReported, 0)) as maximum, 
    MIN(NVL(v.dateConfirmed - v.dateReported, 0)) as minimum, 
    AVG(v.dateConfirmed - v.dateReported) as mean)
FROM Violation v 
WHERE v.trial.id = 5 AND v.dateReported IS NOT NULL AND v.dateConfirmed IS NOT NULL 
GROUP BY ROLLUP (v.counName, v.siteNumber)

我正在使用Hibernate以这种方式在Oracle 11g DB中运行sql查询:

List<ViolationSummaryByDurationDTO> results = em.createQuery(sb.toString(), ViolationSummaryByDurationDTO.class).getResultList();

注意:sb是带有sql查询的StringBuilder。

现在,结果我只得到第一个选择,为什么不执行union之后的第二个选择?在我收到的对象ViolationSummaryByDurationDTO中,只有第一个选择存在结果,为什么?

编辑:

我可以提一下,这个查询是直接在SQL Client(SQL Developer)中正确执行的,但问题只是在用Hibernate执行它时。 sql客户端查询和hibernate sql查询之间的唯一区别是为hibernate中的每个select添加了构造函数。

0 个答案:

没有答案