sql—有条件地将值放入列中

qnyhuwrf  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(351)

我正在尝试在sql server中创建一个视图,如下所示:

CREATE VIEW HeaquartersSales 
AS
    SELECT
        gs.district,
        gs.MonthToDateSales,
    FROM 
        GlobalSales AS gs
GO

不过,我只想把销售数字算进去 MonthToDateSales 如果地区名称中有“g0-”。
我该怎么做呢?

qcbq4gxm

qcbq4gxm1#

你好像在找一个 CASE 表达式:

CREATE VIEW HeaquartersSales AS
SELECT
  district,
  CASE WHEN district like '%G0-% THEN MonthToDateSales END as MonthToDateSales
FROM GlobalSales AS gs
GO

相关问题