multiple-select语句

lg40wkob  于 2021-06-25  发布在  Mysql
关注(0)|答案(5)|浏览(315)

我有一个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语句,给我不同市场(amazon,flipkart等)一个月的总销售额,就像本例中的on
第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 ;

但是select子查询没有给出日期分布,而是每月的总销售额。如何解决这个问题。请帮忙。提前谢谢。

lndjwyie

lndjwyie1#

select Date,Marketplace, sum(Sale) from table group by Marketplace;
b4lqfgs4

b4lqfgs42#

试试这个:

SELECT Date,Marketplace, SUM(Sale) 
FROM tab111 where date =1
Group By Marketplace

3hvapo4f

3hvapo4f3#

试试这个

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;
zengzsys

zengzsys4#

你试过做一个联盟吗:

select date,marketplace,sum(sales) from yourtable where marketplace='flipkart'

union

select date,marketplace,sum(sales) from yourtable where marketplace='amazon'

这是小提琴

c9x0cxw0

c9x0cxw05#

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'

相关问题