Case When 以不同方式陈述

问题描述 投票:0回答:2

我需要通过从 MSSQL 中的现有列分配值来创建新列。

有些客户可以包含1或2个品牌和佣金金额。

客户编号 计数客户 品牌 佣金 佣金2
1000 1 B -28 0
1001 2 B 12 12
1001 2 F 252 252
1002 1 B 62 62
1003 1 B 5 5
1004 1 B -61 0
1005 1 F 0 0
1006 1 F 32 32
1007 1 F 0 0
1008 1 B -496 0
1009 1 F 1 1
1010 1 B 1 1
1011 1 B 0 0
1012 1 F -10 0
1013 1 B 82 82
1014 1 B 54 54
1015 1 B 18 18
1016 1 B 0 0
1017 2 B 0 0
1017 2 F -21 0
1018 1 B 0 0
1019 1 B 5 5
1020 1 B -3 0
1021 1 F -1 0
1022 1 B 95 95
1023 1 B -20 0
1024 1 B 0 0
1025 2 B -65 10
1025 2 F 10 10
1026 2 B 24 24
1026 2 F -7 24
1027 2 B 0 0
1027 2 F 0 0

我想根据commission金额获得commission2栏。情况是这样的

  • 如果 CountCustomer=1 那么这很容易。如果佣金<=0 Then commission2 is 0 otherwise equals to commission number.
  • 如果 CountCustomer=2(表示一位客户有 2 个品牌),则其中一个佣金金额>0,另一个为 <0 then both commission2 should be the number which is positive. Both of commission numbers are above 0 then these numbers should be stayed same for commission2. If both commission <0 then commission2 should be 0 for both. You can understand more clear from bolded rows on the table above.

谢谢你,

维利

sql group-by case
2个回答
0
投票

我不认为这是你正在寻找的逻辑,但如果这是我的理解,那么 CASE WHEN 应该是这样的:

SELECT Customerid, CountCustomer, Brand, comission,
         CASE WHEN CountCustomer = 1 AND commission > 0 THEN commision
            WHEN CountCustomer = 1 AND commission < 0 THEN 0
            WHEN CountCustomer = 2 AND commission > 0 THEN comission
            WHEN CountCustomer = 2 AND commission < 0 THEN 0
            END AS commission2
FROM YourTable

0
投票

我使用 https://tableconvert.com/sql-generator 生成 SQL 代码来创建临时表并插入数据。 。然后我将其粘贴到https://sqlfiddle.com/sql-server/online-compiler。现在我们有一个好的起点

CREATE TABLE #tableName 
(
    Customerid  INT,
    CountCustomer   INT,
    Brand   VARCHAR(512),
    commission  INT,
    commission2 VARCHAR(512)
);

INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1000 ', '1 ', 'B ', '-28 ', '0');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1001 ', '2 ', 'B ', '12 ', '12');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1001 ', '2 ', 'F ', '252 ', '252');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1002 ', '1 ', 'B ', '62 ', '62');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1003 ', '1 ', 'B ', '5 ', '5');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1004 ', '1 ', 'B ', '-61 ', '0');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1005 ', '1 ', 'F ', '0 ', '0');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1006 ', '1 ', 'F ', '32 ', '32');
INSERT INTO #tableName (Customerid , CountCustomer , Brand , commission , commission2) VALUES ('1007 ', '1 ', 'F ', '0 ', '0');

Select *, 0 as NewColumn from  #tableName
© www.soinside.com 2019 - 2024. All rights reserved.