If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Need Help with this query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-21-11, 05:28
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
Need Help with this query

Hi Guys,

I have the below requirement.

I have this table

Quote:
ID Mnth Value
1 00 05
1 01 10
1 01 15
1 01 22
1 02 32
1 02 38
1 03 45
1 03 52
The value is a Aggregated column. Now i want to have a record for each month which should give me difference between the current month maximum and previous month maximum.

So the output should be
Quote:
1 00 05
1 01 17(22-05)
1 02 16(38-22)
1 03 14(52-38)
Hope somebody can guide me..

Cheers!!!!
Reply With Quote
  #2 (permalink)  
Old 03-21-11, 05:44
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
What about the ID column? Is that always the same for the same month?
Reply With Quote
  #3 (permalink)  
Old 03-21-11, 05:59
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
Quote:
Originally Posted by shammat View Post
What about the ID column? Is that always the same for the same month?
Yes, it will be same..
Reply With Quote
  #4 (permalink)  
Old 03-21-11, 06:03
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Assuming you are on a current version of PostgreSQL (8.4 or later):
Code:
SELECT id, 
       mnth, 
       agg_value, 
       agg_value - lag(agg_value) over (order by mnth) as diff
FROM (
   SELECT id,
           mnth,
          sum(value) as agg_value
   FROM your_table
   GROUP BY id, mnth
) T
ORDER BY id, mnth
Reply With Quote
  #5 (permalink)  
Old 03-21-11, 06:13
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
Quote:
Originally Posted by shammat View Post
Assuming you are on a current version of PostgreSQL (8.4 or later):
Code:
SELECT id, 
       mnth, 
       agg_value, 
       agg_value - lag(agg_value) over (order by mnth) as diff
FROM (
   SELECT id,
           mnth,
          sum(value) as agg_value
   FROM your_table
   GROUP BY id, mnth
) T
ORDER BY id, mnth
will test it and let u knw incase of any issues...
Thanks for your speedy help..
cheers!!!!
Reply With Quote
  #6 (permalink)  
Old 03-21-11, 06:16
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
I am able to find one prob.. the Value column is already a aggregated column.. Then don we need to take the maximum value instead of summing it up??
And also Lag function is not working in mine. so i think my database is a previous version.. i am also not sure about what version it is..

Last edited by mac4rfree; 03-21-11 at 06:44.
Reply With Quote
  #7 (permalink)  
Old 03-21-11, 06:31
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Sorry I don't understand your last question (or I didn't understand what exactly is stored in the table and what exactly you need).

In your example output you show only 1 row for month = 01 but there are three rows for that month available. So which difference would you like to see? The one between the first and the second value, or the second and the third, or the first and the third?
Reply With Quote
  #8 (permalink)  
Old 03-21-11, 06:46
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
no.. i think u got it wrong..
I need the difference between the First month(01) maximum which is 22 and previous month(00) maximum which is 05.
Hope i made myself clear..
Reply With Quote
  #9 (permalink)  
Old 03-21-11, 06:51
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Then it should be max() instead of sum(), correct
Reply With Quote
  #10 (permalink)  
Old 03-21-11, 07:04
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
is there any alternative for lag function???? coz it not workin for me..
Reply With Quote
  #11 (permalink)  
Old 03-21-11, 07:11
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Upgrade to a current version of Postgres
Reply With Quote
  #12 (permalink)  
Old 03-21-11, 07:13
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
i don think my client will upgrade,,, lol nice suggestion though..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On