0

我有以下查询:

Select TRUNC(create_dtime) as Day, Count(Create_Dtime) As SinS_Tot
  From player_chkin_hist
 Where Product_Id Is Not Null
   And Trunc(Create_Dtime) >= To_Date('2012-sep-01','yyyy-mon-dd')
   And Trunc(Create_Dtime) < To_Date('2012-sep-19','yyyy-mon-dd')
 Group By Trunc(Create_Dtime)
 Order By 1 Asc

Select TRUNC(Create_Dtime) As Day, Count(Create_Dtime) As Sins_Tot
  From Player_Chkin
 where Product_Id Is Not Null
   And Trunc(Create_Dtime) >= To_Date('2012-sep-01','yyyy-mon-dd')
   And Trunc(Create_Dtime) < To_Date('2012-sep-19','yyyy-mon-dd')
 Group By Trunc(Create_Dtime)
 Order By 1 Asc

查询 1 结果:

Day          SinS_Tot
01-Sep-12      10000
02-Sep-12       9000

查询两个结果:

 Day          SinS_Tot
03-Sep-12      11500
04-Sep-12       9800

有没有一种简单的方法可以组合这两个查询以便我看到所有数据?我考虑过使用连接,但由于日期不重叠,所以没有什么可以将内部连接设置为相等。

4

4 回答 4

1

您正在寻找的是UNION(或UNION ALL,取决于您是否要消除重复项):

Select 
trunc(create_dtime) as Day, Count(Create_Dtime) As SinS_Tot
From player_chkin_hist
Where
Product_Id Is Not Null And
Trunc(Create_Dtime) >= To_Date('2012-sep-01','yyyy-mon-dd')
And Trunc(Create_Dtime) < To_Date('2012-sep-19','yyyy-mon-dd')
Group By Trunc(Create_Dtime)

UNION

Select Trunc(Create_Dtime) As Day, Count(Create_Dtime) As Sins_Tot
From Player_Chkin
where
Product_Id Is Not Null And
Trunc(Create_Dtime) >= To_Date('2012-sep-01','yyyy-mon-dd')
And Trunc(Create_Dtime) < To_Date('2012-sep-19','yyyy-mon-dd')
Group By Trunc(Create_Dtime)
Order By 1 Asc
于 2012-09-18T19:32:31.740 回答
1

你可以UNION ALL得到两个结果

<<query 1>>
UNION ALL
<<query 2>>

您也可以使用 a UNION,但这会更昂贵,因为它会迫使 Oracle 查找并消除重复的行。如果您知道这两个查询永远不会返回同一行,或者如果您想要两行,这是不必要的开销。

于 2012-09-18T19:33:01.033 回答
0

也许UNION他们之间?将查询保留在ORDER BY外部,将其放在第二个查询的末尾

Query1
 UNION
Query2
于 2012-09-18T19:32:40.940 回答
0

您只需要使用 UNION 组运算符。

[查询1] UNION ALL [查询2]

于 2012-09-18T19:34:27.463 回答