我有一张mysql表,用于在不同的市场上销售产品。
Date Marketplace Sale
1 flipkart 1000
1 flipkart 500
1 amazon 200
1 amazon 1500
1 amazon 700
2 myntra 500
2 flipkart 1000
我想要一个单独的mysql声明,它可以让我在不同的市场(亚马逊,flipkart等)上进行一个月的明确销售,就像这种情况一样 在
第1个月的flipkart毛销量为1500(1000 + 500),亚马逊毛销售2400(1500 + 700 + 200)
我有这个问题:
SELECT date, (Select sum(gross_sale) from sku where marketplace='flipkart') as flipkart, (Select sum(gross_sale) from sku where marketplace='Myntra') as myntra, sum(gross_sale) as countdate FROM sku WHERE date >= '2017-11-01' AND
date <= '2017-11-30' GROUP BY date ORDER BY date ASC limit 30 ;
但是,选择子查询并没有给出日期分配,而是总月销售额。如何解决这个问题。请帮忙。提前谢谢。
答案 0 :(得分:2)
Mysql为您提供sum()
并按任务分组。您可以通过以下查询
select Date, Marketplace, sum(Sale) from table group by Date, Marketplace
sum()
将在group by
发表评论后
如果您不想使用循环,并且您需要查询单个结果,那么您只需要where
子句
select Date, Marketplace, sum(Sale) from table where Marketplace = 'flipkart'
答案 1 :(得分:1)
答案 2 :(得分:1)
你有没有尝试过工会:
select date,marketplace,sum(sales) from yourtable where marketplace='flipkart'
union
select date,marketplace,sum(sales) from yourtable where marketplace='amazon'
答案 3 :(得分:1)
试试这个
SELECT
sk.date,
sum(sk1.sale) as flipkart_sum_sale,
sum(sk2.sale) as amanzon_sum_sale
FROM
sku sk
LEFT JOIN
sku sk1 ON sk.id = sk1.id
AND sk1.marketplace = 'flipkart'
LEFT JOIN
sku sk2 ON sk.id = sk2.id
AND sk2.marketplace = 'amazon'
GROUP BY sk.date;
答案 4 :(得分:0)
select Date,Marketplace, sum(Sale) from table group by Marketplace;