Re: [SQL] Bucketing Row Data in columns

2009-06-25 Thread bricklen
Assuming you know your dates beforehand, you could try a CASE statement. Something like: select order_id, sum(case when timestamp::date = 01/01/2009'' then amount else 0 end) as amount_day1, sum(case when timestamp::date = '02/01/2009' then amount else 0 end) as amount_day2, sum(case when t

Re: [SQL] Bucketing Row Data in columns

2009-06-25 Thread Osvaldo Kussama
2009/6/25 James Kitambara : > > Hello Mr. Sandeep Bandela, > > I have gone through your scenario and come up with the following solution. > > SELECT USER_ID, CREATE_TIMESTAMP::DATE, SUM(AMOUNT) > FROM ORDERS > GROUP BY USER_ID, CREATE_TIMESTAMP > ORDER BY USER_ID, CREATE_TIMESTAMP; > > Maybe you ne

Re: [SQL] Bucketing Row Data in columns

2009-06-25 Thread Oliveiros Cristina
I admit that must be a more elegant and faster solution with pl/psql (or whatever other languages) As I don't know nothing about pl/psql I tried with pure sql (if you don't have a hunting dog, hunt with a cat) But obviously this solution doesn't scale well if you have a giant table with lots

Re: [SQL] Bucketing Row Data in columns

2009-06-25 Thread Rob Sargent
I would be suspicious of this sort of solution of turning rows into columns by mean of a series of correlated sub-selects. Once the data set gets large and the number of columns goes over 2 or 3 this will in all likelihood not perform well. I had the pleasure of re-writing a "report" which was

Re: [SQL] Bucketing Row Data in columns

2009-06-25 Thread Oliveiros Cristina
Hello, Sandeep, I am not sure if this is what you want. I came up with this query SELECT * FROM (SELECT user_id,SUM(amount) as day1 FROM t1 WHERE create_timestamp = '2009-1-1' GROUP BY "user_id") a NATURAL FULL JOIN (SELECT user_id,SUM(amount) as day2 FROM t1 WHERE create_timestamp = '2009-1-

Re: [SQL] Bucketing Row Data in columns

2009-06-25 Thread James Kitambara
  Hello Mr. Sandeep Bandela,   I have gone through your scenario and come up with the following solution.   SELECT USER_ID, CREATE_TIMESTAMP::DATE, SUM(AMOUNT) FROM ORDERS GROUP BY USER_ID, CREATE_TIMESTAMP ORDER BY USER_ID, CREATE_TIMESTAMP;   Maybe you need to do little modification on the query