如何使用Select Distinct

时间:2016-05-14 07:10:32

标签: mysql sql

我有以下数据库:

MappedByteBuffer

我希望看起来像:

npw  | sales_name | sales_type | Region |
-----------------------------------------
  1  |    Rob     | private 1  |   1    |
  2  |    Cait    | private 2  |   2    |
  3  |    Blue    | public     |   4    |
  4  |    ReD     | public     |   3    |
  5  |    Max     | private 1  |   2    |

所以我希望将Region列与行和不同的sales_type列区分为列标题。将来可能会添加新的sales_type。 对它的SQL查询&将新的sales_type自动添加到新列标题中?

2 个答案:

答案 0 :(得分:1)

您可以通过 CASE WHEN GROUP BY

来完成此类结果
mx = arr.max
[mx, arr.each_index.select { |i| arr[i] == mx }]
  #=> [3, [3, 6, 7]] 

答案 1 :(得分:1)

尝试以下

select Region, 
   case when sales_type in('private 1') then sales_name else '' end  private1, 
   case when sales_type in('private 2') then sales_name else '' end privat2, 
   case when sales_type in('public') then sales_name else '' end public
from Table1

检查sqlfiddle

相关问题