简体   繁体   中英

SQL select in AS in inner join

I have this kind of query, from that i want to select by category such as Region in ('CENTRAL','EASTERN') as A1 and Region in ('NORTHERN','SOUTHERN') as A2

How to add this in my query?

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

I think you can use IF or CASE if your database is common database such as mysql , oracle , mssql :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM