使用SQL Server

时间:2017-12-20 20:52:45

标签: sql sql-server sql-server-2008 sql-server-2005

我有2个表,父表(表a)和子表(表B)。对于每个父项,可以有多个子项(行)。我需要的是如果儿童符合某些标准,我需要将其视为1。

目前,我的代码正在将每个孩子都计入父母,并且它不应该以这种方式工作。这是我的代码简化。

SELECT 
  cl.clientID, cl.code, cl.cName, 
  COUNT(case when e.errorCode NOT IN('DP','RB','WP','PE','OV') then c.rID end) as rateCount,
  SUM(case when e.errorCode NOT IN('DP','RB','WP','PE','OV') then e.refundDue else 0.0 end) as rateAmount,
  COUNT(case when e.errorCode IN('DP','RB','WP','PE','OV') then c.rID end) as paymentCount, 
  SUM(case when e.errorCode IN('DP','RB','WP','PE','OV') then e.refundDue else 0.0 end) as paymentAmount 

  FROM claims c 

  INNER JOIN clients cl ON cl.code=c.client 
  INNER JOIN entries e ON e.rID=c.rID 

  WHERE 

  status='closed' AND c.carrierID IN(3909) AND 
  (c.dateon >= '20170624' AND c.dateon < '20171220') 

  GROUP BY cl.clientID, cl.code, cl.cName ORDER BY cl.cName ASC

因此,再次,在count语句中,不是在满足条件时计算1个实例,而是计算所有子项。将父(声明)与子(条目)连接的是父母rID。我希望这很清楚,但只是为了确定,

权利要求

rID   name
------------------
1     Damien
2     Jim

条目

eID  rID  name
------------------
1    1    Yeye
2    1    Juju
3    1    Nao
4    1    Ty
5    2    Pai
6    2    Cha
7    2    Jac

所以在这种情况下,对于Damien,我应该得到1,如果是,他有孩子,Jim也应该得到1。

以上是SQL的结果:

results

以下是表格:

Carriers Table

clients table

claims table

entries table

2 个答案:

答案 0 :(得分:2)

似乎你并没有真正“计算”,而是你正在设置一个标志。这应该可以帮到你找到你想要的东西:

SELECT 
  c.name 
  ,HasChild = CASE WHEN EXISTS (SELECT 1 FROM entries e WHERE e.rID = c.rID and e.errorCode NOT IN('DP','RB','WP','PE','OV')) THEN 1
                    ELSE 0
                    END    
FROM claims c

编辑: 如果没有来自每个表的示例数据,我无法对此进行测试,但您可能需要使用COUNT(DISTINCT)。试试这个:

SELECT
    cl.clientID
    ,cl.code
    ,cl.cName
    ,COUNT(DISTINCT CASE WHEN e.errorCode NOT IN ('DP', 'RB', 'WP', 'PE', 'OV') THEN
                    c.rID END
          )                                                                        AS rateCount
    ,SUM(CASE WHEN e.errorCode NOT IN ('DP', 'RB', 'WP', 'PE', 'OV') THEN
                  e.refundDue ELSE 0.0 END
        )                                                                          AS rateAmount
    ,COUNT(DISTINCT CASE WHEN e.errorCode IN ('DP', 'RB', 'WP', 'PE', 'OV') THEN c.rID END) AS paymentCount
    ,SUM(CASE WHEN e.errorCode IN ('DP', 'RB', 'WP', 'PE', 'OV') THEN
                  e.refundDue ELSE 0.0 END
        )                                                                          AS paymentAmount
FROM claims        c
INNER JOIN clients cl ON cl.code = c.client
INNER JOIN entries e ON e.rID = c.rID
WHERE status = 'closed'
    AND c.carrierID IN (3909)
    AND (c.dateon >= '20170624' AND c.dateon < '20171220')
GROUP BY cl.clientID
    ,cl.code
    ,cl.cName
ORDER BY cl.cName ASC;

答案 1 :(得分:0)

在子查询的帮助下,您可以尝试以下方法:

SELECT cl.clientID, cl.code, cl.cName,
case (select count(*) from entries e where e.rID=c.rID and e.errorCode
NOT IN('DP','RB','WP','PE','OV')) when 0 then 0 else 1 end  as rateCount 
FROM claims c 
    INNER JOIN clients cl ON cl.code=c.client 

GROUP BY cl.clientID, cl.code, cl.cName, rateCount  ORDER BY cl.cName ASC

另一种方法

使用公用表表达式可以尝试类似:

WITH CTE AS (
select c.client, CASE count(*) WHEN 0 THEN 0 ELSE 1 END AS rateCount  
from entries e inner join claims c on c.rID = e.rID where e.errorCode
    NOT IN('DP','RB','WP','PE','OV') GROUP BY c.client
)   
SELECT cl.clientID, cl.code, cl.cName, coalesce( rateCount,0) as rateCount  
from CTE c right join clients cl on cl.code = c.client