使用连接时,Sql Count显示错误的数据

时间:2018-03-02 13:23:24

标签: sql sql-server join count

我是Sql的新手,所以不确定我做错了什么。

我有两张桌子:

Tbl 1中的数据

[t0 + 1, t1 + 1, ..., t29 + 1]

TbL 2中的数据

EmpID---------PrdcType-------EmpName  
1000   -------- A -------------X1   
2000   -------- B -------------X2    
1000   -------- A -------------X1    
3000   -------- A -------------X3    
so on.....

结果应该是:

EmpID-------ExPrdc--------EmpName    
2000   ------ A -------------X2    
3000   ------ B -------------X3    
1000   ------ A -------------X1    
1000   ------ A -------------X1
So on...

但我得到了:

EmpName--------EmpID--------Table1_A_Prdc_Count--------Table2_A_Prdc_Count    
X1-------------1000---------2---------------------------2    
X2-------------2000---------0---------------------------1    
X3-------------3000---------1---------------------------0

So on..

这些数字是完全随机的,但每次数字相同,除非我将0值更改为null或任何其他值。我尝试检查模式但没有用。

我正在使用的代码:

EmpName--------EmpID--------Table1_A_Prdc_Count--------Table2_A_Prdc_Count
X1-------------1000---------1547-----------------------------------2131
X2-------------2000---------321------------------------------------987
X3-------------3000---------46546----------------------------------3265465

当我使用相同的代码而没有内部联接(分别针对不同的表使用它)时,我得到了正确的结果,但是一旦我添加了内部联接,它就无法正常工作。 请帮助我,因为我坚持了2个多星期。提前谢谢。

2 个答案:

答案 0 :(得分:1)

这完全不仅仅是产品A
让我考虑一下

select a.*, b.cnt as cntB
from ( select r.EmpName, r.EmpID, count(*) as cnt
         from Tbl1 r  
        where r.EmpID in('X1','X2','X3') 
        group by r.EmpName, r.EmpID 
     ) a
join ( select d.EmpName, d.EmpID, count(*) as cnt
         from TbL 2 d 
        where d.EmpID in('X1','X2','X3') 
        group by d.EmpName, d.EmpID 
     ) b
  on a.EmpID = b.EmpID

仅限产品A

select isnull(a.EmpName, b.EmpName) as name
     , isnull(a.EmpID,   b.EmpID)   as id 
     , isnull(a.cnt, 0) as cntA
     , isnull(b.cnt, 0) as cntB
from ( select r.EmpName, r.EmpID, count(*) as cnt
         from Tbl1 r  
        where r.EmpID in('X1','X2','X3')  
          and PrdcType = 'A'
        group by r.EmpName, r.EmpID 
     ) a
full outer join 
     ( select d.EmpName, d.EmpID, count(*) as cnt
         from TbL 2 d 
        where d.EmpID in('X1','X2','X3') 
          and ExPrdc = 'A'
        group by d.EmpName, d.EmpID 
     ) b
  on a.EmpID = b.EmpID 
order by isnull(a.EmpID, b.EmpID)

答案 1 :(得分:1)

SQL DEMO

您当前的查询有几个问题,实际上它不会运行。您可以通过实施一个简单的想法来获得您想要的结果。

使用查询从A计算Tbl1并使用其他查询从A计算Tbl2,然后如下JOIN计算:

select r.EmpName, r.EmpID, Table1_A_Prdc_Count, Table2_A_Prdc_Count 
from 
(
    select EmpName, EmpID, 
    sum(CASE WHEN PrdcType='A' Then 1 else 0 END) as Table1_A_Prdc_Count
    from Tbl1 d
    where d.EmpName in('X1','X2','X3')
    group by EmpName, EmpID
) as d
join 
(
    select EmpName, EmpID, 
    sum(CASE WHEN ExPrdc='A' Then 1 else 0 END) as Table2_A_Prdc_Count
    from Tbl2 
    where EmpName in('X1','X2','X3')
    group by EmpName, EmpID
) as r on d.EmpID = r.EmpID and d.EmpName = r.EmpName

<强>输出

EmpName EmpID   Table1_A_Prdc_Count     Table2_A_Prdc_Count
-----------------------------------------------------------
X1      1000    2                       2
X2      2000    0                       1
X3      3000    1                       0