mysql - SQL - Find Highest Single Day of Sales From Date Range -
i using mysql..
i have simple sales table follow:
o----o----------o-----------o | id | store_id | logdate | o----o----------o-----------o | 1 | 1 | 2015-1-13 | | 2 | 1 | 2015-1-14 | | 3 | 2 | 2015-1-11 | | 4 | 2 | 2015-1-18 | o----o----------o-----------o
and sale product table
o----o----------o---------o------------o | id | sale_id | qty | price | o----o----------o---------o------------o | 1 | 1 | 1 | 10 | | 2 | 2 | 1 | 10 | | 3 | 2 | 1 | 10 | | 4 | 3 | 1 | 10 | | 5 | 3 | 1 | 10 | | 6 | 3 | 1 | 10 | | 7 | 4 | 1 | 10 | | 8 | 4 | 1 | 10 | o----o----------o---------o------------o
expected result
o-- --------o----------------o---------------------o | store_id | sum(price*qty) | highest date on | o-----------o----------------o---------------------o | 1 | 20 | 2015-1-14 | | 2 | 30 | 2015-1-11 | o-----------o----------------o---------------------o
how achieve expected result? have tried follow didn't work expected:
select store_id, max(total), highestsingledateon ( select sum(price * qty) total, date(s.logdate) highestsingledateon, s.store_id store_id sale_product sp join sales s on s.id = sp.sales_id group date(s.logdate), s.store_id order date(s.logdate) asc ) result_for_highest_single_day group highestsingledateon, store_id
select store_id, max(total), highestsingledateon ( select sum(price * qty) total, date(s.logdate) highestsingledateon, s.store_id store_id sale_product sp join sales s on s.id = sp.sales_id group date(s.logdate), s.store_id order total desc ) result_for_highest_single_day group store_id
i have modified script order date(s.logdate) asc
>> order total desc
, group highestsingledateon, store_id
>> group store_id
.
*above sql script,it uses unstable features group by
of mysql.
*then according mysql standard,i write other version sql script.
select table1.* ( select sum(price * qty) total, date(s.logdate) highestsingledateon, s.store_id store_id sale_product sp join sales s on s.id = sp.sale_id group date(s.logdate), s.store_id) table1 , (select tmp.store_id,max(tmp.total) max_total (select sum(price * qty) total, date(s.logdate) highestsingledateon, s.store_id store_id sale_product sp join sales s on s.id = sp.sale_id group date(s.logdate), s.store_id ) tmp group tmp.store_id) table2 table1.store_id = table2.store_id , table1.total=table2.max_total
Comments
Post a Comment