ADD由其他列确定的SQL列中的特定值

时间:2016-03-03 17:10:00

标签: sql sql-server

我有一个数据库,可以根据标签确定不同的值。 标签确定其是否为豁免值。 例如,2 =非豁免,3 =豁免。如果我运行查询,我的结果看起来像这样

    SELECT NAME, EXEMPTIONSTATUS
           SUM(TOTAL_VALUE) AS 'TOTAL VALUE'
    FROM ORDER_ACCOUNT JOIN ACCOUNT_INVOICE
    WHERE ORDER_ACCOUNT.DATE BETWEEN 'M/D/YEAR' AND 'M/D/YEAR'
    GROUP BY NAME, EXEMPTIONSTATUS
    ORDER BY NAME ASC   

我的查询

    |Name  |NON EXEMPT VALUE|EXEMPT VALUE|
    |X     |100             |200         |

如何让我的查询为值创建新列,例如:

self.tableView.cellLayoutMarginsFollowReadableWidth=NO;

我只是不知道如何对它进行排序,无论它是否在我的Where子句中。

2 个答案:

答案 0 :(得分:0)

在SUM中使用CASE语句仅总计非EXEMPT,然后是EXEMPT,并将它们选为单独的列。与以下内容类似(可能需要将TOTAL_VALUE添加到GROUP BY,或删除EXEMPTIONSTATUS)

SELECT
NAME
,SUM(CASE WHEN EXEMPTIONSTATUS = 2 THEN TOTAL_VALUE ELSE 0 END) AS 'NON EXEMPT VALUE'
,SUM(CASE WHEN EXEMPTIONSTATUS = 3 THEN TOTAL_VALUE ELSE 0 END) AS 'EXEMPT VALUE'
FROM ORDER_ACCOUNT JOIN ACCOUNT_INVOICE
WHERE ORDER_ACCOUNT.DATE BETWEEN 'M/D/YEAR' AND 'M/D/YEAR'
GROUP BY NAME, EXEMPTIONSTATUS
ORDER BY NAME ASC 

编辑:下面的新代码会在现有表格中添加新列。你需要用你的桌子替换#Test,但我相信这会让你得到你想要的东西。

SELECT
NAME,
EXEMPTIONSTATUS
,[TOTAL_VALUE]
,(SELECT SUM(CASE WHEN EXEMPTIONSTATUS = 2 THEN TOTAL_VALUE ELSE 0 END) FROM #Test t WHERE t.NAME = NAME) 'NON EXEMPT VALUE'
,(SELECT SUM(CASE WHEN EXEMPTIONSTATUS = 3 THEN TOTAL_VALUE ELSE 0 END) FROM #Test t WHERE t.NAME = NAME) 'EXEMPT VALUE'
FROM #Test

这给了我以下输出

| NAME | EXEMPTIONSTATUS | TOTAL_VALUE | NON EXEMPT VALUE  | EXEMPT VALUE  |
| X    | 2               | 100         | 100               | 200           |
| X    | 3               | 200         | 100               | 200           |

答案 1 :(得分:0)

让我们说你的表结构是这样的:

CREATE TABLE tab(ID int, Name nvarchar(20), ExemptionStatus int, TotalValue int);
INSERT INTO tab(ID, Name, ExemptionStatus, TotalValue) values (1, 'X', 2, 100);
INSERT INTO tab(ID, Name, ExemptionStatus, TotalValue) values (2, 'X', 3, 200);

所以你的数据是这样的:

   ID   Name   ExemptionStatus   TotalValue
    1     X        2                100
    2     X        3                200

然后您使用的查询是:

SELECT      NotExempted.Name,
            NotExempted.NonExemptValue,
            Exempted.ExemptValue
FROM        (SELECT     Name,
                        CASE 
                            WHEN ExemptionStatus = 2 THEN TotalValue
                        END
                        AS 'NonExemptValue'
             FROM       #tab
            ) NotExempted
INNER JOIN  (SELECT     Name,
                        CASE 
                            WHEN ExemptionStatus = 3 THEN TotalValue
                        END
                        AS 'ExemptValue'
             FROM       #tab
            ) Exempted ON NotExempted.Name = Exempted.Name
WHERE        NotExempted.NonExemptValue IS NOT NULL
AND          Exempted.ExemptValue IS NOT NULL
GROUP BY     NotExempted.Name,
             NotExempted.NonExemptValue,
             Exempted.ExemptValue

您的结果将如下所示:

Name  NonExemptValue    ExemptValue
 X         100             200

你可以在这里看到 - > http://sqlfiddle.com/#!9/8902d3/2

现在,让我们说你有这样的数据:

CREATE TABLE #tab(ID int, Name nvarchar(20), ExemptionStatus int, TotalValue int)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (1, 'X', 2, 100)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (2, 'X', 3, 200)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (3, 'X', 2, 1000)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (4, 'X', 3, 2000)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (5, 'X', 2, 1045)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (6, 'X', 3, 2045)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (7, 'X', 2, 1034)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (8, 'X', 3, 2023)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (9, 'X', 2, 1023)
INSERT INTO #tab(ID, Name, ExemptionStatus, TotalValue) values (10, 'X', 3, 2076)

看起来像这样:

   ID   Name   ExemptionStatus   TotalValue
   1      X          2              100
   2      X          3              200
   3      X          2             1000
   4      X          3             2000
   5      X          2             1045
   6      X          3             2045
   7      X          2             1034
   8      X          3             2023
   9      X          2             1023
  10      X          3             2076

如果您需要将总值相加,那么您可以使用以下查询(这是对上述查询的略微修改):

SELECT      NotExempted.Name,
            NotExempted.NonExemptValue,
            Exempted.ExemptValue
FROM        (SELECT     Name,
                        CASE 
                            WHEN ExemptionStatus = 2 THEN (SELECT SUM(TotalValue) FROM #tab WHERE ExemptionStatus = 2)
                        END
                        AS 'NonExemptValue'
             FROM       #tab
            ) NotExempted
INNER JOIN  (SELECT     Name,
                        CASE 
                            WHEN ExemptionStatus = 3 THEN (SELECT SUM(TotalValue) FROM #tab WHERE ExemptionStatus = 3)
                        END
                        AS 'ExemptValue'
             FROM       #tab
            ) Exempted ON NotExempted.Name = Exempted.Name
WHERE        NotExempted.NonExemptValue IS NOT NULL
AND          Exempted.ExemptValue IS NOT NULL
GROUP BY     NotExempted.Name,
             NotExempted.NonExemptValue,
             Exempted.ExemptValue

您的结果将如下所示:

Name  NonExemptValue    ExemptValue
 X         4202             8344

你可以在这里看到 - > http://sqlfiddle.com/#!9/02c76/3

希望这有帮助!!!