我有4个oracle SQL查询,我希望在一个结果中

时间:2012-03-24 15:17:42

标签: sql oracle

这4个查询产生了所需的结果,但是我想将这些查询包装成一个查询,该查询将每个查询作为水平列返回,并垂直显示相应的查询结果。

我已将所有查询限制为rownum< = 20.

SELECT * FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (1/24)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) WHERE 
rownum <= 20;

SELECT * FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (1)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) WHERE 
rownum <= 20;

SELECT * FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (7)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) WHERE 
rownum <= 20;

SELECT * FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (30)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) WHERE 
rownum <= 20;

有人对如何做到这一点有任何建议吗?

编辑:我想指出我不希望结果在彼此之上。不同的查询必须是水平列,而它们各自的结果是垂直的.a

1 个答案:

答案 0 :(得分:2)

通过他们的rownum加入他们(记得别名rownum)

select * from 
(
SELECT rownum rownr, a.* FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (1/24)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) a WHERE 
rownum <= 20
) a,
(
SELECT rownum rownr, b.* FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (1)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) b WHERE 
rownum <= 20
) b, 
(
SELECT rownum rownr, c.* FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (7)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) c WHERE 
rownum <= 20
) c,
(
SELECT  rownum rownr, d.* FROM (SELECT count(tl.usr) as "Antall oppslag" FROM TABLE tl WHERE 
tl.timestamp >= (sysdate - (30)) GROUP BY tl.usr ORDER BY count(tl.usr) desc) d WHERE 
rownum <= 20
) d
where a.rownr = b.rownr 
and a.rownr = c.rownr
and a.rownr = d.rownr