mysql - interval statistic with month name column -
last time used interval, don't think work want to, want declare year , month. can guru can teach me here?
this sample data request:
food_type | sales | date_sales pizza buy 02-01-2015 12:00:00 pizza buy 02-01-2015 03:00:00 pizza sell 03-01-2015 01:00:00 pizza sell 04-01-2015 05:00:00 pizza buy 05-01-2015 02:00:00
this sample trying do
declare @startdate datetime = '02-01-2015', @enddate datetime = '05-01-2015' select type_food, count(case when status = 'buy' 1 end) [incoming], count(case when status= 'sell' 1 end) [outgoing] shop [something_date] between @startdate , @enddate
or
select type_food, count(case when status = 'buy' 1 end) [incoming], count(case when status= 'sell' 1 end) [outgoing],datediff(month, date_of_purchase, @year ) [year] shop group type_food, datediff(month,date_of_purchase, @year )
-- cant use datediff because want between date..
this closest query can if using datediff
...not year anymore...
food type | month | buy | sell pizza 12 10 5 pizza 13 3 1 .... ... .. .. pizza 32 5 2
how can make sql output this:
food type | month | buy | sell | pizza feb-15 10 5 pizza mar-15 3 1 .... ... .. .. pizza may-15 5 2
please me guru, still beginner in sql
i think need sum , group type_food
select type_food, date_format(date_sales, '%m - %y'), sum(case when sales = 'buy' 1 else 0 end) incoming, sum(case when sales = 'sell' 1 else 0 end) outgoing shop group type_food
Comments
Post a Comment