SELECT pd_end_dt,SUM(nrx_cnt) Total_Count
FROM wkly_lnd.lnd_wkly_plan_rx_summary
WHERE pd_end_dt >= '01-Sep-08' AND pd_end_dt < '30-Sep-08'
GROUP BY pd_end_dt
SELECT pd_end_dt,SUM(nrx_cnt) Total_Count
FROM wkly_lnd.lnd_wkly_plan_rx_summary
WHERE pd_end_dt >= '01-Sep-07' AND pd_end_dt < '30-Sep-07'
GROUP BY pd_end_dt
运行每个查询时的结果集将类似于
09/28/2007 00:00:00 702,457.36 09/21/2007 00:00:00 703,604.59 09/07/2007 00:00:00 636,619.92 09/14/2007 00:00:00 698,082.03
同样适用于前一年
我需要计算与去年相比销售单位的差异,并添加一列,找出百分比变化
答案 0 :(得分:1)
有很多事情没有说明。我希望你在日常工作中得到更明确的要求...... 无论如何,这里是你的情况的模拟。这是基于这样的假设:数据的天数(每周一次)在2007年与2008年相同:
SQL> create table lnd_wkly_plan_rx_summary (pd_end_dt,nrx_cnt)
2 as
3 select date '2008-09-07', 100000 from dual union all
4 select date '2008-09-07', 536619.92 from dual union all
5 select date '2008-09-14', 698082.03 from dual union all
6 select date '2008-09-21', 403604.59 from dual union all
7 select date '2008-09-21', 200000 from dual union all
8 select date '2008-09-21', 100000 from dual union all
9 select date '2008-09-28', 702457.36 from dual union all
10 select date '2007-09-07', 400000 from dual union all
11 select date '2007-09-14', 450000 from dual union all
12 select date '2007-09-21', 500000 from dual union all
13 select date '2007-09-28', 550000 from dual union all
14 select date '2007-09-28', 100000 from dual
15 /
Tabel is aangemaakt.
您的原始查询,稍加修改。
SQL> SELECT pd_end_dt
2 , SUM(nrx_cnt) Total_Count
3 FROM lnd_wkly_plan_rx_summary
4 WHERE pd_end_dt >= date '2008-09-01'
5 AND pd_end_dt < date '2008-09-30'
6 GROUP BY pd_end_dt
7 /
PD_END_DT TOTAL_COUNT
------------------- -----------
07-09-2008 00:00:00 636619,92
14-09-2008 00:00:00 698082,03
21-09-2008 00:00:00 703604,59
28-09-2008 00:00:00 702457,36
4 rijen zijn geselecteerd.
SQL> SELECT pd_end_dt
2 , SUM(nrx_cnt) Total_Count
3 FROM lnd_wkly_plan_rx_summary
4 WHERE pd_end_dt >= date '2007-09-01'
5 AND pd_end_dt < date '2007-09-30'
6 GROUP BY pd_end_dt
7 /
PD_END_DT TOTAL_COUNT
------------------- -----------
07-09-2007 00:00:00 400000
14-09-2007 00:00:00 450000
21-09-2007 00:00:00 500000
28-09-2007 00:00:00 650000
4 rijen zijn geselecteerd.
您可以使用该查询来比较2007年和2008年的数据:
SQL> select to_char(pd_end_dt,'dd-mm') day_and_month
2 , sum(case trunc(pd_end_dt,'yyyy') when date '2007-01-01' then nrx_cnt end) sum2007
3 , sum(case trunc(pd_end_dt,'yyyy') when date '2008-01-01' then nrx_cnt end) sum2008
4 , sum(case trunc(pd_end_dt,'yyyy') when date '2008-01-01' then nrx_cnt end)
5 - sum(case trunc(pd_end_dt,'yyyy') when date '2007-01-01' then nrx_cnt end) difference
6 , ( sum(case trunc(pd_end_dt,'yyyy') when date '2008-01-01' then nrx_cnt end)
7 - sum(case trunc(pd_end_dt,'yyyy') when date '2007-01-01' then nrx_cnt end)
8 ) / sum(case trunc(pd_end_dt,'yyyy') when date '2008-01-01' then nrx_cnt end) * 100 percentage_difference
9 from lnd_wkly_plan_rx_summary
10 where ( ( pd_end_dt >= date '2007-09-01'
11 and pd_end_dt < date '2007-09-30'
12 )
13 or ( pd_end_dt >= date '2008-09-07'
14 and pd_end_dt < date '2008-09-30'
15 )
16 )
17 group by to_char(pd_end_dt,'dd-mm')
18 /
DAY_A SUM2007 SUM2008 DIFFERENCE PERCENTAGE_DIFFERENCE
----- ---------- ---------- ---------- ---------------------
07-09 400000 636619,92 236619,92 37,1681615
14-09 450000 698082,03 248082,03 35,5376617
21-09 500000 703604,59 203604,59 28,9373595
28-09 650000 702457,36 52457,36 7,46769313
4 rijen zijn geselecteerd.
虽然相当冗长,但我认为它说明了一切。 您可能希望进行以下重写,因为它不会像上面的查询那样重复聚合函数:
SQL> select day_and_month
2 , sum2007
3 , sum2008
4 , sum2008-sum2007 difference
5 , 100*(sum2008-sum2007)/sum2008 percentage_difference
6 from ( select to_char(pd_end_dt,'dd-mm') day_and_month
7 , sum(case trunc(pd_end_dt,'yyyy') when date '2007-01-01' then nrx_cnt end) sum2007
8 , sum(case trunc(pd_end_dt,'yyyy') when date '2008-01-01' then nrx_cnt end) sum2008
9 from lnd_wkly_plan_rx_summary
10 where ( pd_end_dt >= date '2007-09-01'
11 and pd_end_dt < date '2007-09-30'
12 )
13 or ( pd_end_dt >= date '2008-09-07'
14 and pd_end_dt < date '2008-09-30'
15 )
16 group by to_char(pd_end_dt,'dd-mm')
17 )
18 /
DAY_A SUM2007 SUM2008 DIFFERENCE PERCENTAGE_DIFFERENCE
----- ---------- ---------- ---------- ---------------------
07-09 400000 636619,92 236619,92 37,1681615
14-09 450000 698082,03 248082,03 35,5376617
21-09 500000 703604,59 203604,59 28,9373595
28-09 650000 702457,36 52457,36 7,46769313
4 rijen zijn geselecteerd.
希望这有帮助。
此致 罗布。
答案 1 :(得分:0)
如果您想逐年比较查询结果(即每天与前一年的日期),您可以按一年中的某一天进行分组to_char('dd-mon')
:
SQL> WITH lnd_wkly_plan_rx_summary AS (
2 SELECT DATE '2007-09-28' pd_end_dt, 702457.36 nrx_cnt FROM dual
3 UNION ALL SELECT DATE '2007-09-21', 703604.59 FROM dual
4 --
5 UNION ALL SELECT DATE '2008-09-28' pd_end_dt, 702557.36 nrx_cnt FROM dual
6 UNION ALL SELECT DATE '2008-09-21', 703404.59 FROM dual
7 )
8 SELECT to_char(pd_end_dt, 'dd-mon') pd_end_dt,
9 SUM(CASE
10 WHEN to_char(pd_end_dt, 'yyyy') = '2007' THEN
11 nrx_cnt
12 END) Total_2007,
13 SUM(CASE
14 WHEN to_char(pd_end_dt, 'yyyy') = '2008' THEN
15 nrx_cnt
16 END) Total_2008,
17 SUM(CASE
18 WHEN to_char(pd_end_dt, 'yyyy') = '2008' THEN
19 nrx_cnt
20 ELSE
21 -nrx_cnt
22 END) delta
23 FROM lnd_wkly_plan_rx_summary
24 WHERE ((pd_end_dt >= DATE '2007-09-01' AND pd_end_dt < DATE '2007-09-30') OR
25 (pd_end_dt >= DATE '2008-09-01' AND pd_end_dt < DATE '2008-09-30'))
26 GROUP BY to_char(pd_end_dt, 'dd-mon');
PD_END_DT TOTAL_2007 TOTAL_2008 DELTA
------------ ---------- ---------- ----------
28-sep 702457,36 702557,36 100
21-sep 703604,59 703404,59 -200
答案 2 :(得分:-1)
为什么不看一下Oracle提供的分析功能?我假设您正在使用Oracle,因为您已使用Oracle标记标记了您的问题。您可以参考http://www.oracle-base.com/articles/misc/LagLeadAnalyticFunctions.php。
我将您的数据集简化为这样
09/08/2007 100
09/08/2008 200
09/09/2007 350
09/09/2008 400
09/10/2007 150
09/10/2008 175
这些是您在2007年和2008年9月8,9和10日的总计数
您可以使用以下查询:
假设桌子为T(end_date,cnt)
(你的名字太长了!抱歉)
Select end_date, cnt,
LAG(cnt,1,0) over (order by
to_number(to_char(end_dt,'dd')),to_number(to_char(end_dt,'mm'))) cntPrev,
cnt - LAG(cnt,1,0) over (order by
to_number(to_char(end_dt,'dd')),to_number(to_char(end_dt,'mm'))) cntDiff
from T
简单来说(如果你复制,粘贴,这将不起作用。)
Let X=LAG(cnt,1,0) over (order by
to_number(to_char(end_dt,'dd')),to_number(to_char(end_dt,'mm')))
您的查询是
Select end_date, cnt, X cntPrev, cnt-X cntDiff
from T;