【问题标题】:Calculate substraction by a specific quarter of year按一年的特定季度计算减法
【发布时间】:2012-04-10 18:09:21
【问题描述】:

我有一些SQL生成后的记录:

YEARS MONTHS SUMMONTH SUMQUARTER QTR
----- ------ -------- ---------- ---
 2009 Jan      363639     855922 1   
 2009 Feb      128305     855922 1   
 2009 Mar      363978     855922 1   
 2009 Apr      376633    1058871 2   
 2009 May      299140    1058871 2   
 2009 Jun      383098    1058871 2   
 2009 Jul      435000    1063577 3   
 2009 Aug      266227    1063577 3   
 2009 Sep      362350    1063577 3   
 2009 Oct      449366    1017906 4   
 2009 Nov      280943    1017906 4   
 2009 Dec      287597    1017906 4   
 2010 Jan      418277     661083 1   
 2010 Feb      129895     661083 1   
 2010 Mar      112911     661083 1   
 2010 Apr      163593     685625 2   
 2010 May      228505     685625 2   
 2010 Jun      293527     685625 2   
 2010 Jul      451608    1044364 3   
 2010 Aug      356683    1044364 3   
 2010 Sep      236073    1044364 3   
 2010 Oct      247365     798925 4   
 2010 Nov      414100     798925 4   
 2010 Dec      137460     798925 4   

 24 rows selected 

SUMQUARTER 列汇总一年中的每个季度... qtr 指定它属于哪个季度。

问题是如何在2个不同年份之间减去sumquarter得到具体的查询结果?

区别不是:最大值-最小值。 是他要输入的用户自定义值……

让我们说... 例如,用户想要查看 2009 年 qtr=2 和 2010 年 qtr=2 之间的减法(总和), 用户可以改变sql的参数(years,qtr)来查看记录。

这意味着结果应该是:(1058871 - 685625)

这是我目前使用的 SQL:

select years,months,summonth,sumquarter,qtr

from(
        select years,months,summonth,sumhour,hours,to_char(ym, 'Q') qtr,
                sum(sumhour) over(partition by years || to_char(ym, 'Q') order by years || to_char(ym, 'Q')) sumquarter,ym,
                count(days) over(partition by years,months,hours) days_month

        from(
                select   years, months, days, hours, mins, sumHour,
                         SUM (sumHour) OVER (PARTITION BY years,months,days) sumDay,
                         SUM (sumHour) OVER (PARTITION BY years,months) sumMonth,
                         SUM (sumHour) OVER (PARTITION BY years) sumyear,
                         to_date(years || months, 'YYYYMon', 'NLS_DATE_LANGUAGE=American') ym
                from  (

                        SELECT x.years, x.months, x.days, x.hours, x.mins, sum(x.value) as sumHour
                        FROM xmltest,
                             XMLTABLE ('$d/cdata/name' passing doc as "d"
                                        COLUMNS
                                          years integer path 'year',
                                          months varchar(3) path 'month',
                                          days varchar(2) path 'day',
                                          hours varchar(2) path 'hour',
                                          mins varchar(2) path 'minute',
                                          value float path 'value'
                                       ) as X
                        group by x.years, x.months, x.days, x.hours, x.mins
                        order by x.years, x.months, x.days
                      )
             )
    )

  group by years,months,summonth,sumquarter,qtr,ym
  order by ym

sql 模式可能是这样的:...??

select ((select sumquarter from table where years=2009 and qtr=2) - (select sumquarter from table where years=2010 and qtr=2)) from table

其实不行……

结果可能如下图所示:

SUBTRACT 
---------- 
373246

谢谢大家帮忙!!:)

【问题讨论】:

    标签: sql database oracle plsql


    【解决方案1】:

    我会使用以下内容:

    select 
    ((select sumquarter from table where years=2009 and qtr=2 and rownum=1) - 
     (select sumquarter from table where years=2010 and qtr=2 and rownum=1)) as substract
    from dual
    

    【讨论】:

    • 它不起作用... 00906. 00000 - “缺少左括号” *原因:*操作:第 39 行错误:第 31 列
    • 你是在运行那个语句吗,因为只有 4 行,错误指示第 39 行?
    • 如您所见,表数据是从问题中的 sql 生成的。我使用 with() 将其设为表并使用您的 sql 从该表中检索数据,但出现此错误。
    • 我怀疑with 有问题,因为括号在上面的查询中是平衡的(我已经在真实表上尝试过语法)。
    • 这只是为了这个声明吗?也可以是:stackoverflow.com/questions/7839907/…
    【解决方案2】:

    试试这个查询:

    select (case when years=2009 and qtr=2 then sumquater end) - (case when years=2010 and qtr=2 then sumquater end) from table
    

    【讨论】:

    • ORA-00904: "SUMQUATER": Invalid ID 00904. 00000 - "%s: invalid identifier" *原因:*操作:第 38 行错误,第 99 列
    • 我更正了拼写错误...但是该列在所有值中都返回 null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多