如何从1个表中组合这两个Oracle查询

时间:2017-04-18 13:29:31

标签: sql oracle

我有一个Oracle表,其中包含多个活动的每日计数。我需要创建一个查询,显示一个月的每日活动和上一年的同一个月的总和。所以我最终会得到6个这样的列:

一年|本月| SumOfCount | PreviousYear | PreviousMonth | PreSumOfCount

我创建了这两个SQL查询,这个查询提供了有关输入的日期参数的信息:

select extract(year from activitydate) as yr, extract(MONTH from 
activitydate) as mon, Sum(dicalls)
from updstats
Where ACTIVITYDATE BETWEEN TO_DATE (:STARTDATE,'MM/DD/YYYY') AND 
TO_DATE(:ENDDATE,'MM/DD/YYYY')
GROUP BY extract(year from activitydate), extract(MONTH from activitydate)
order by yr, mon

,这个提供上一个日期范围的数据:

select extract(year from activitydate) as Pyr, extract(MONTH from 
activitydate) as Pmon, Sum(dicalls)
from updstats
Where ACTIVITYDATE BETWEEN add_months(TO_DATE (:STARTDATE,'MM/DD/YYYY'),-12) 
AND add_months(TO_DATE(:ENDDATE,'MM/DD/YYYY'),-12)
GROUP BY extract(year from activitydate), extract(MONTH from activitydate)
order by Pyr, Pmon

我陷入困境,无法弄清楚如何将这两个结合起来给我提供我需要的数据集。

4 个答案:

答案 0 :(得分:1)

获取每月和每年的dicalls总和,并使用lag获取上一年同月的值。使用where子句筛选外部查询中的日期范围。 (此查询输出表中的所有月份和年份。)

select yr,mth,dicalls
,lag(yr) over(partition by mth order by yr) prev_yr
,lag(mth) over(partition by mth order by yr) prev_mth
,lag(dicalls) over(partition by mth order by yr) prev_dicalls
from (select sum(dicalls) as dicalls,extract(month from activitydate) as mth,extract(year from activitydate) as yr
      from updstats
      group by extract(month from activitydate),extract(year from activitydate)
     ) x
order by 1,2

答案 1 :(得分:0)

您可以将这两个查询作为带有rownumber的子查询加入。

 select * from 
            (select extract(year from activitydate) as yr, 
            extract(MONTH from activitydate) as mon, 
            Sum(dicalls),
           row_number() over(partition by extract(year from activitydate),extract(MONTH from activitydate) order by extract(year from activitydate),extract(MONTH from activitydate)) rn1
            from updstats Where ACTIVITYDATE BETWEEN TO_DATE (:STARTDATE,'MM/DD/YYYY') AND TO_DATE(:ENDDATE,'MM/DD/YYYY')
            GROUP BY extract(year from activitydate), extract(MONTH from activitydate)
            order by yr, mon) as n 
       join (select extract(year from activitydate) as Pyr, 
           extract(MONTH from activitydate) as Pmon,             
           Sum(dicalls),
           row_number() over(partition by extract(year from activitydate) ,extract(MONTH from activitydate) order by extract(year from activitydate) ,extract(MONTH from activitydate) as rn2
            from updstats  Where ACTIVITYDATE BETWEEN add_months(TO_DATE (:STARTDATE,'MM/DD/YYYY'),-12) 
            AND add_months(TO_DATE(:ENDDATE,'MM/DD/YYYY'),-12)
            GROUP BY extract(year from activitydate), extract(MONTH from activitydate)
            order by Pyr, Pmon)) as p on (n.rn1=p.rn2) order by n.yr,n.mon

答案 2 :(得分:0)

我会在月份和年份加入两个表格的结果

data.table

月份将是相同的,因此您只需要使每年与前一年匹配

答案 3 :(得分:0)

只是因为没有人通过CTE回答,你走了

with tbl_A as (
select extract(year from activitydate) as yr, extract(MONTH from 
activitydate) as mon, Sum(dicalls)
from updstats
Where ACTIVITYDATE BETWEEN TO_DATE (:STARTDATE,'MM/DD/YYYY') AND 
TO_DATE(:ENDDATE,'MM/DD/YYYY')
GROUP BY extract(year from activitydate), extract(MONTH from activitydate)
order by yr, mon
),
tbl_B as(

select extract(year from activitydate) as Pyr, extract(MONTH from 
activitydate) as Pmon, Sum(dicalls)
from updstats
Where ACTIVITYDATE BETWEEN add_months(TO_DATE (:STARTDATE,'MM/DD/YYYY'),-12) 
AND add_months(TO_DATE(:ENDDATE,'MM/DD/YYYY'),-12)
GROUP BY extract(year from activitydate), extract(MONTH from activitydate)
order by Pyr, Pmon
)
select * from tbl_A a inner join tbl_B b on a.yr = b.yr and a.mon = b.mon;