SQL在内连接中选择AS

时间:2018-06-08 01:35:21

标签: sql

我有这种查询,我希望按类别选择,例如 区域(' CENTRAL',' EASTERN')为A1 和(' NORTHERN' SOUTHERN')中的地区为A2

如何在我的查询中添加它?

SELECT
  locinvaisle.Region AS Region,
  sum(sales_data.QUANTITY/1000) AS UnitMT
FROM
  sales_data
  INNER JOIN locinvaisle ON locinvaisle.Location = sales_data.LOCATION
WHERE
 sales_data.unit = 'KG'
 and
 sales_data.CUSTOMERACCOUNT not in ('CT1008','CT1009')
 group by locinvaisle.Region

1 个答案:

答案 0 :(得分:1)

如果您的数据库是公共数据库,例如IFCASEmysql,我认为您可以使用oraclemssql

SELECT
  CASE WHEN locinvaisle.Region = 'CENTRAL' OR locinvaisle.Region = 'EASTERN' THEN 'A1'
  CASE WHEN locinvaisle.Region = 'NORTHERN' OR locinvaisle.Region = 'SOUTHERN' THEN 'A2'
  ELSE 'Default'
  END
    AS Region,
  sum(sales_data.QUANTITY/1000) AS UnitMT
FROM
  sales_data
  INNER JOIN locinvaisle ON locinvaisle.Location = sales_data.LOCATION
WHERE
 sales_data.unit = 'KG'
 and
 sales_data.CUSTOMERACCOUNT not in ('CT1008','CT1009')
 group by Region