Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: Difference ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 9 Topic 3461 of 3799
Post > Topic >>

Re: Difference in columns

by chestercyoung@[EMAIL PROTECTED] (chester c young) May 11, 2008 at 10:54 AM

--- Mag Gam <magawake@[EMAIL PROTECTED]
> wrote:

> Hi All,
> 
> I have a view that generates output similar to this.
> 
> select * from foo.view;
> 
>        ts          | size
> -------------------+-----
>  2002-03-16        | 11
>  2002-03-17        | 16
>  2002-03-18        | 18
>  2002-03-19        | 12
> 
> I am trying to find the difference between the size column. So the
> desired
> output would be
> 
>        ts          | size| Diff
> -------------------+-----+------
>  2002-03-16        | 11  | 0
>  2002-03-17        | 15  | 4
>  2002-03-18        | 18  | 3
>  2002-03-19        | 12  | -6
> 
> 
> I need the first column to be 0, since it will be 11-11. The second
> colum is
> 15-11. The third column is 18-15. The fourth column is 12-18.
> 
> Any thoughts about this?
> 

select cur.ts, cur.size, 
  cur.size - coalesce( 
    (select size from view next
       where next.ts = cur.ts - '1 day'::interval), 
     cur.size ) as diff
from view cur;

alternately:

select cur.ts, cur.size, 
  case when cur.ts = '2002-03-16' then 0
  else cur.size - coalesce( 
    (select size from view next
       where next.ts = cur.ts - '1 day'::interval), 
     cur.size )
  end as diff
from view;



     
____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now. 
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

-- 
Sent via pgsql-sql mailing list (pgsql-sql@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 9 Posts in Topic:
Difference in columns
magawake@[EMAIL PROTECTED  2008-05-11 13:37:52 
Re: Difference in columns
chestercyoung@[EMAIL PROT  2008-05-11 10:54:38 
Re: Difference in columns
singh.gurjeet@[EMAIL PROT  2008-05-11 23:25:06 
Re: Difference in columns
depesz@[EMAIL PROTECTED]   2008-05-11 20:11:42 
Re: Difference in columns
craig@[EMAIL PROTECTED]   2008-05-12 02:17:45 
Re: Difference in columns
singh.gurjeet@[EMAIL PROT  2008-05-12 00:06:12 
Re: Difference in columns
tgl@[EMAIL PROTECTED] (T  2008-05-11 14:58:02 
Re: Difference in columns
craig@[EMAIL PROTECTED]   2008-05-12 03:58:56 
Re: Difference in columns
magawake@[EMAIL PROTECTED  2008-05-11 20:58:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 21:42:52 CST 2008.