如何在以前的语句中添加和减去值

时间:2016-08-31 14:22:18

标签: sql sql-server-2008

我使用以下代码获取错误无效的列名:

SELECT 
    DS, AccNumber, PPeriod, 
    MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS Opening, 
    MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS electricity,
    MAX(CASE WHEN LinkAcc = '1045000' THEN amount END) AS water, 
    MAX(CASE WHEN LinkAcc = '1000000' THEN amount END) AS levy,
    MAX(CASE WHEN LinkAcc = '2750000' THEN amount END) AS Interest, 
    MAX(CASE WHEN LinkAcc = '4000000' THEN amount END) AS Legal,
    MAX(CASE WHEN LinkAcc = '1020000' THEN amount END) AS Sewer,
    SUM(CASE WHEN LinkAcc IN ('4350003','3850000','9250000') THEN amount END) As Other,
    SUM(CASE WHEN LinkAcc IN ('8420000','8400000') AND amount < 0 THEN amount END) As Payments,
   (Opening + electricity + water + levy + Interest + Legal + Sewer + Other - Payments) AS Due
FROM 
    dbo.Table1
WHERE 
    (AccNumber BETWEEN 'CC001' AND 'CC100')
GROUP BY 
    DataSource, AccNumber, PPeriod
HAVING 
    (DataSource = 'PAS11CEDCRE16') AND (PPeriod = 112)

我正在尝试进行上述添加和减法,并且我得到了无效的列名,我尝试将表名放在列名之前,但它仍然失败。

3 个答案:

答案 0 :(得分:2)

将当前查询用作派生表,或使用CTE。

派生表:

SELECT  *,
        (Opening + electricity + water + levy + Interest + Legal + Sewer + Other - Payments) AS Due
FROM (  SELECT DS, AccNumber, PPeriod, 
        MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS Opening, 
        MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS electricity,
        MAX(CASE WHEN LinkAcc = '1045000' THEN amount END) AS water, 
        MAX(CASE WHEN LinkAcc = '1000000' THEN amount END) AS levy,
        MAX(CASE WHEN LinkAcc = '2750000' THEN amount END) AS Interest, 
        MAX(CASE WHEN LinkAcc = '4000000' THEN amount END) AS Legal,
        MAX(CASE WHEN LinkAcc = '1020000' THEN amount END) AS Sewer,
        SUM(CASE WHEN LinkAcc IN ('4350003','3850000','9250000') THEN amount END) As Other,
        SUM(CASE WHEN LinkAcc IN ('8420000','8400000') AND amount < 0 THEN amount END) As Payments,
        FROM dbo.Table1
        WHERE (AccNumber BETWEEN 'CC001' AND 'CC100')
        GROUP BY DataSource, AccNumber, PPeriod
        HAVING (DataSource = 'PAS11CEDCRE16') AND (PPeriod = 112)) AS T;

CTE:

WITH CTE AS
(
    SELECT DS, AccNumber, PPeriod, 
    MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS Opening, 
    MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS electricity,
    MAX(CASE WHEN LinkAcc = '1045000' THEN amount END) AS water, 
    MAX(CASE WHEN LinkAcc = '1000000' THEN amount END) AS levy,
    MAX(CASE WHEN LinkAcc = '2750000' THEN amount END) AS Interest, 
    MAX(CASE WHEN LinkAcc = '4000000' THEN amount END) AS Legal,
    MAX(CASE WHEN LinkAcc = '1020000' THEN amount END) AS Sewer,
    SUM(CASE WHEN LinkAcc IN ('4350003','3850000','9250000') THEN amount END) As Other,
    SUM(CASE WHEN LinkAcc IN ('8420000','8400000') AND amount < 0 THEN amount END) As Payments,
    FROM dbo.Table1
    WHERE (AccNumber BETWEEN 'CC001' AND 'CC100')
    GROUP BY DataSource, AccNumber, PPeriod
    HAVING (DataSource = 'PAS11CEDCRE16') AND (PPeriod = 112)
)
SELECT  *,
        (Opening + electricity + water + levy + Interest + Legal + Sewer + Other - Payments) AS Due
FROM CTE;

答案 1 :(得分:0)

使用CTE

;WITH CTE AS (SELECT DS, AccNumber, PPeriod, 
MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS Opening, 
MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS electricity,
MAX(CASE WHEN LinkAcc = '1045000' THEN amount END) AS water, 
MAX(CASE WHEN LinkAcc = '1000000' THEN amount END) AS levy,
MAX(CASE WHEN LinkAcc = '2750000' THEN amount END) AS Interest, 
MAX(CASE WHEN LinkAcc = '4000000' THEN amount END) AS Legal,
MAX(CASE WHEN LinkAcc = '1020000' THEN amount END) AS Sewer,
SUM(CASE WHEN LinkAcc IN ('4350003','3850000','9250000') THEN amount END) As Other,
SUM(CASE WHEN LinkAcc IN ('8420000','8400000') AND amount < 0 THEN amount END) As Payments

FROM dbo.Table1
WHERE (AccNumber BETWEEN 'CC001' AND 'CC100')
GROUP BY DataSource, AccNumber, PPeriod,

HAVING (DataSource = 'PAS11CEDCRE16') AND (PPeriod = 112))

Select 
*,
(Opening + electricity + water + levy + Interest + Legal + Sewer + Other - Payments) AS Due 
FROM
     CTE

OR

派生表:

Select T.*,
(Opening + electricity + water + levy + Interest + Legal + Sewer + Other - Payments) AS Due  from (
SELECT DS, AccNumber, PPeriod, 
MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS Opening, 
MAX(CASE WHEN LinkAcc = '1010000' THEN amount END) AS electricity,
MAX(CASE WHEN LinkAcc = '1045000' THEN amount END) AS water, 
MAX(CASE WHEN LinkAcc = '1000000' THEN amount END) AS levy,
MAX(CASE WHEN LinkAcc = '2750000' THEN amount END) AS Interest, 
MAX(CASE WHEN LinkAcc = '4000000' THEN amount END) AS Legal,
MAX(CASE WHEN LinkAcc = '1020000' THEN amount END) AS Sewer,
SUM(CASE WHEN LinkAcc IN ('4350003','3850000','9250000') THEN amount END) As Other,
SUM(CASE WHEN LinkAcc IN ('8420000','8400000') AND amount < 0 THEN amount END) As Payments

FROM dbo.Table1
WHERE (AccNumber BETWEEN 'CC001' AND 'CC100')
GROUP BY DataSource, AccNumber, PPeriod,

HAVING (DataSource = 'PAS11CEDCRE16') AND (PPeriod = 112))T

答案 2 :(得分:0)

使用外部SELECT包装查询。您不能引入别名并在同一SELECT子句中使用它。

SELECT Opening, 
  electricity,
  water, 
  levy,
  Interest, 
  Legal,
  Sewer,
  Other,
  Payments,
  (Opening + electricity + water + levy + Interest + Legal + Sewer + Other - Payments) AS Due
FROM (
-- original query here
) q