Sql Case Column 1和Column的值相同

时间:2014-08-21 03:36:24

标签: sql

我想在CASE声明中应用计算,但结果不是我预期的,我不知道如何应用查询来过滤日期(取决于月份)。我的查询有什么问题?请帮忙......谢谢

这是我的查询

SELECT DISTINCT a.SERVICE_CODE, 
       CASE 
           WHEN b.SERVICE_CODE IS NULL THEN a.AMT 
           ELSE  SUM (a.AMT) - SUM(b.AMT) 
       END AS TOTAL_AMT
 FROM 
     Table a
     LEFT OUTER JOIN Table b ON (a.SERVICE_CODE = b.SERVICE_CODE)
 WHERE  b.SERVICE_CODE >='3000' AND c.SERVICE_CODE >='3000'

表a

Invoice_No               date          Service_code     Amt
001                     1/7/2014       6000             300
002                     1/8/2014       6003             700
003                     5/8/2014       6003             100
004                     10/8/2014      6005             1000

表b

Credit_No                 date          Service_code     Amt
c100                     1/7/2014       6000             300
c200                     13/8/2014      6003             700

期望的输出

Service_code              Total_Amt
6003                      100
6005                      1000

谢谢

3 个答案:

答案 0 :(得分:0)

您需要修改查询,以便始终获得两个表之间的一对一关系。在这种情况下,我建议您使用子查询,将您的金额分组,并将LEFT JOIN加入到您的其他表中,如下所示。此外,如果您需要按特定日期范围过滤它,只需将where子句添加到子查询中:

SELECT a.SERVICE_CODE, 
       CASE 
           WHEN b.SERVICE_CODE IS NULL THEN a.Amount 
           ELSE  a.Amount - b.AMT 
       END AS TOTAL_AMT
 FROM 
     (SELECT SERVICE_CODE, SUM(Amt) Amount
     FROM Tablea 
     WHERE date >= '2014-07-01' AND date < '2014-08-01'
     GROUP BY SERVICE_CODE) a
     LEFT OUTER JOIN Tableb b ON (a.SERVICE_CODE = b.SERVICE_CODE)
 WHERE  a.SERVICE_CODE >='3000' AND b.SERVICE_CODE >='3000'

答案 1 :(得分:0)

我猜了一下:

select service_code, tot_sum from (
    select a.service_code
         , sum(a.amt) - (select coalesce(sum(b.amt), 0) 
                         from b 
                         where a.SERVICE_CODE = b.SERVICE_CODE) as tot_sum 
    from a group by a.service_code
) as T 
where tot_sum <> 0

答案 2 :(得分:0)

您的问题与CASE SUM声明有关,应该是相反的:Sum(Case ...

试试这个:

 SELECT a.SERVICE_CODE,
           SUM(
               CASE 
                   WHEN b.SERVICE_CODE IS NULL THEN 
                      a.AMT 
               ELSE  
                      a.AMT- b.AMT
               END
           ) AS TOTAL_AMT
     FROM 
         Table a
         LEFT OUTER JOIN Table b ON (a.SERVICE_CODE = b.SERVICE_CODE)
GROUP BY a.SERVICE_CODE
     WHERE  b.SERVICE_CODE >='3000' AND c.SERVICE_CODE >='3000'

完整的工作示例,任何人都可以尝试:

SELECT 
    A.SERVICE_CODE, 
    SUM
    (
        CASE WHEN B.SERVICE_CODE IS NULL THEN 
            A.AMT
        ELSE
            A.AMT- B.AMT
        END 
    ) AS TOTAL
FROM
(
    SELECT 1 AS SERVICE_CODE, 101 AS AMT FROM DUAL UNION
    SELECT 1 AS SERVICE_CODE, 99  AS AMT FROM DUAL UNION
    SELECT 1 AS SERVICE_CODE, 96  AS AMT FROM DUAL UNION
    SELECT 1 AS SERVICE_CODE, 104 AS AMT FROM DUAL
) TABLE_A A --SAMPLE TABLE 1
LEFT JOIN 
(
    SELECT NULL AS SERVICE_CODE, 201 AS AMT FROM DUAL UNION
    SELECT NULL AS SERVICE_CODE, 199  AS AMT FROM DUAL UNION
    SELECT 1 AS SERVICE_CODE, 204  AS AMT FROM DUAL UNION
    SELECT 1 AS SERVICE_CODE, 99 AS AMT FROM DUAL
)TABLE_B B --SAMPLE TABLE 2
    ON A.SERVICE_CODE = B.SERVICE_CODE

GROUP BY A.SERVICE_CODE

输出:

enter image description here