sql - Sum/aggregate data based on dates -


i have following table items sold out in every shop.

table columns

  1. shop_id: specific id of shop,
  2. sold: purchase amount dollars ($)
  3. time: date , time of purchase.

data

shop_id sold    time 1       12.44   23/10/2014 20:20 1       12.77   24/10/2014 20:18 1       10.72   24/10/2014 20:18 1       14.51   24/10/2014 20:18 2       5.94    22/10/2014 20:11 2       15.69   23/10/2014 20:23 2       8.55    24/10/2014 20:12 2       6.96    24/10/2014 20:18 3       8.84    22/10/2014 20:21 3       7.82    22/10/2014 20:21 3       22.19   23/10/2014 20:23 3       13.21   23/10/2014 20:23 4       14.60   23/10/2014 20:20 4       12.19   23/10/2014 20:23 4       5.41    24/10/2014 20:18 4       10.93   24/10/2014 20:19 5       18.54   22/10/2014 20:21 5       7.48    22/10/2014 20:21 5       10.67   24/10/2014 20:18 5       15.96   24/10/2014 20:18 

i have 3 classifiers per purchase :

purchase classifiers

  1. low: 0-8 $
  2. medium: 8-12 $
  3. high: 12 , higher $

what write query using postgresql produce total purchase each day each type of purchase classifier.

desired output

date        low     medium  high 22/10/2014  29.10   14.51   12.77 23/10/2014  0       0       70.06 24/10/2014  16.34   51.24   41.39 

thank in advance kind help. not experienced postgresql. in r easily, since moving huge database table r cumbersome, need learn postgresql.

i believe can using conditional aggregation this:

select    cast(time date),   sum(case when sold > 0 , sold <= 8 sold else 0 end) low,   sum(case when sold > 8 , sold <= 12 sold else 0 end) medium,   sum(case when sold > 12 sold else 0 end) high your_table group cast(time date); 

you might have tweak ranges bit - should 8 fall both low , medium? also, can't remember if cast("time" date) correct syntax postgresql - suspect might not be, replace part correct function date datetime/timestamp column. should "time"::date


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -