2

I have some records after SQL generating:

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 

The SUMQUARTER column sum up each quarter of a year... The qtr specify it belongs to which quarter.

The problem is how to have a subtraction of sumquarter between 2 different years to get the specific query result?

The difference is not the: max value-min value. It is the user-defined value that he want to input...

Let say... For example, the user want to see the substraction(sumquarter) between 2009 qtr=2 and 2010 qtr=2, user may change the parameter(years,qtr) of sql to view the record.

This mean the result should be: (1058871 - 685625)

Here is the SQL that I am currently using:

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

The sql pattern maybe something like this:...??

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

Actually, it doesn't work...

The result maybe look in this view:

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

Thanks everyone helps!!:)

user1264222
  • 225
  • 2
  • 4
  • 12

2 Answers2

0

try this query:

select (case when years=2009 and qtr=2 then sumquater end) - (case when years=2010 and qtr=2 then sumquater end) from table
Vikram
  • 8,235
  • 33
  • 47
0

I'd use the following:

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
beny23
  • 34,390
  • 5
  • 82
  • 85
  • It does not work... 00906. 00000 - "missing left parenthesis" *Cause: *Action: Error at Line: 39 Column: 31 – user1264222 Mar 27 '12 at 08:46
  • Are you just running that statement, as there are only 4 lines and the error indicates line 39? – beny23 Mar 27 '12 at 08:51
  • As you can see, the table datas are generated from a sql in the question. I use with() to make it as table and use your sql to retrieve the data from that table, and I got this error. – user1264222 Mar 27 '12 at 09:09
  • I suspect that there's a problem with the `with` as the parenthesis are balanced in the query above (and I've tried the syntax on a real table). – beny23 Mar 27 '12 at 09:20
  • Is that just for this statement? Could also be: http://stackoverflow.com/questions/7839907/no-more-data-to-read-from-socket-error – beny23 Mar 27 '12 at 09:50
  • yes...I have tried the solution that your link provided....but it does not work... – user1264222 Mar 27 '12 at 15:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9401/discussion-between-user1264222-and-beny23) – user1264222 Mar 28 '12 at 12:13