sql - Sum/aggregate data based on dates -
i have following table items sold out in every shop.
table columns
- shop_id: specific id of shop,
- sold: purchase amount dollars ($)
- 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
- low: 0-8 $
- medium: 8-12 $
- 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
Post a Comment