将每月费用填充到每日记录中

时间:2015-02-03 05:52:24

标签: sql oracle

我有3列(日期,旗帜,费用) 日期从年初开始,标志是每日或每月和费用。

对于每日价值,它很好。对于每月价值,我想 将每月的折旧值相加并除以该月的天数。结果率,在整个月填充

Date      Flag      Cost
1/1/2014        
1/2/2014  DAILY     10
1/3/2014  DAILY     15
1/4/2014  DAILY     56
1/5/2014  DAILY     22
1/6/2014  DAILY     32
1/7/2014        
1/8/2014  MONTHLY   3500
1/9/2014        
1/10/2014

结果应为


Date        Cost
1/1/2014    112.9032258 
1/2/2014    122.9032258 
1/3/2014    127.9032258 
1/4/2014    168.9032258 
1/5/2014    134.9032258 
1/6/2014    144.9032258 
1/7/2014    112.9032258 
1/8/2014    112.9032258 
1/9/2014    112.9032258 
1/10/2014   112.9032258 
          .
          .
          .
1/30/2014   112.9032258 
1/31/2014   112.9032258

4 个答案:

答案 0 :(得分:1)

如果我理解得很清楚,这应该会给你每天的平均每月"值:

SELECT "Cost" / EXTRACT(DAY FROM LAST_DAY("Date")) "cost_per_day",
       LAST_DAY("Date") "month"
FROM T
WHERE "Flag" = 'MONTHLY'

一旦你有了这个,你的最终查询可以写成:

WITH monthly AS (
    SELECT "Cost" / EXTRACT(DAY FROM LAST_DAY("Date")) "cost_per_day",
           LAST_DAY("Date") "month"
    FROM T
    WHERE "Flag" = 'MONTHLY'
)

SELECT T."Date", NVL("Cost",0) + NVL("cost_per_day",0) "cost"
FROM T FULL JOIN monthly ON LAST_DAY(T."Date") = "month"
WHERE T."Flag" = 'DAILY'
ORDER BY T."Date";

请参阅http://sqlfiddle.com/#!4/cea34/14

关于获得"整月和#34;这已经多次回答(oracle sql query to list all the dates of previous monthGenerate a range of dates using SQL

答案 1 :(得分:0)

这个解决方案是什么?

SELECT THE_DATE, Flag, COST, 
    CASE Flag
    WHEN 'DAILY' THEN COST
    WHEN 'MONTHLY' THEN
        COST/EXTRACT(DAY FROM LAST_DAY(THE_DATE))
    ELSE NULL
    END AS AVG_COST
FROM THE_TABLE;

答案 2 :(得分:0)

首先让我们看一下 ROW GENERATOR ,以获取当月所有日期的列表,包括相同数量的存储费用以及每个月的每一天

我很难对这个值进行编码" 3500/3"只是为了方便演示。在您的原始查询中,您只需要加入您的表格并获取** MONTHLY"来自表的价值。

SQL> WITH DATA AS
  2    (SELECT to_date('01/01/2014', 'DD/MM/YYYY') date1,
  3      to_date('31/01/2014', 'DD/MM/YYYY') date2
  4    FROM dual
  5    )
  6  SELECT TO_CHAR(date1+level-1, 'DD/MM/YYYY') the_date,
  7    3500/31 AS "cost"
  8  FROM data
  9    CONNECT BY LEVEL <= date2-date1+1
 10  /

THE_DATE         cost
---------- ----------
01/01/2014 112.903226
02/01/2014 112.903226
03/01/2014 112.903226
04/01/2014 112.903226
05/01/2014 112.903226
06/01/2014 112.903226
07/01/2014 112.903226
08/01/2014 112.903226
09/01/2014 112.903226
10/01/2014 112.903226
11/01/2014 112.903226
12/01/2014 112.903226
13/01/2014 112.903226
14/01/2014 112.903226
15/01/2014 112.903226
16/01/2014 112.903226
17/01/2014 112.903226
18/01/2014 112.903226
19/01/2014 112.903226
20/01/2014 112.903226
21/01/2014 112.903226
22/01/2014 112.903226
23/01/2014 112.903226
24/01/2014 112.903226
25/01/2014 112.903226
26/01/2014 112.903226
27/01/2014 112.903226
28/01/2014 112.903226
29/01/2014 112.903226
30/01/2014 112.903226
31/01/2014 112.903226

31 rows selected.

SQL>

您需要做的就是将 nvl(&#34;费用&#34;,0)添加到上面的费用。您可以使用表格执行 OUTER JOIN ,并为具有&#34;值的行添加 nvl(&#34; cost&#34;,0)成本&#34 ;.

答案 3 :(得分:0)

此请求只对表中已存在的行执行

SELECT TO_CHAR(D.DAY, 'MM/DD/YYYY') "Date",
       NVL(D.COST, 0) + M.COST / EXTRACT(DAY FROM LAST_DAY(M.DAY)) "Cost"
    FROM SO28292226 D
    JOIN SO28292226 M ON EXTRACT(MONTH FROM M.DAY) = EXTRACT(MONTH FROM D.DAY)
    WHERE NVL(D.FLAG, 'DAILY') = 'DAILY'
        AND M.FLAG = 'MONTHLY';