从同一列获取信息并显示在多列中

时间:2019-02-28 06:51:53

标签: mysql sql oracle

我想找出每种性别的销售总额,并将其显示在单独的列中。因此,性别列具有M和F,而SalesAmount具有这些性别的各种销售额。我要寻找的是在单独的列上显示M的总销售额和F的总销售额。你能帮我的SQL代码?谢谢

enter image description here

2 个答案:

答案 0 :(得分:0)

用例何时

  select  sum(case when Gender='M' then sales else 0 end) as M,
  sum(case when Gender='F' then sales else 0 end) as F from table

答案 1 :(得分:0)

这会有所帮助,

 SELECT Name,Gender,sum(sales), sum(case when Gender='M' then sales else 0 end) as MaleSales,
      sum(case when Gender='F' then sales else 0 end) as FemaleSales 
      FROM table
     group by Name,Gender