SQL query Multiple Sums and Groups -


i have following table has multiple dishes in multiple quantities different dates

[dishes]     date                  |   dish_id    |  quantity  |   price   |   total ============================================================================ 2016-09-09 00:00:00.000          5             1           50.00       50.00 2016-09-09 00:00:00.000          3             1           30.00       30.00 2016-09-10 00:00:00.000          10            1          100.00      100.00 2016-09-09 00:00:00.000          5             1           50.00       50.00 2016-09-09 00:00:00.000          7             1           70.00       70.00 2016-09-09 00:00:00.000          7             1           70.00       70.00 2016-09-09 00:00:00.000          3             1           30.00       30.00 2016-09-10 00:00:00.000          3             1           30.00       30.00 

what want sql query combine every unique/distinct dish_id , sum equivalent quantities , total in groups acording date value without affecting price per item if ran query on table above produce following result sets:

[result]     date                  |   dish_id    |  quantity  |   price   |   total ============================================================================ 2016-09-09 00:00:00.000          5             2           50.00      100.00 2016-09-09 00:00:00.000          3             2           30.00       60.00 2016-09-09 00:00:00.000          7             2           70.00      140.00                 2016-09-10 00:00:00.000          10            1          100.00      100.00 2016-09-10 00:00:00.000          3             1           30.00       30.00 

im sure groupby correct approach im not sure how sum() multiple columns , using groupby "date" value without affecting "price" maybe

select date, dish_id, sum(quantity), price, sum(total) dishes group date, price 

edit************ got working everyone

how this?

select date     ,dish_id     ,sum(quantity) quantity_total     ,sum(price * quantity) totalprice table1 group date     ,dish_id 

enter image description here

see here online example/sandbox http://rextester.com/olaj52246


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -