使用联接时显示错误数据的 SQL 计数



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

我有两个表:

Tbl 1 中的数据

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

TbL 2 中的数据

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..

但我得到:

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

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

我正在使用的代码:

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

当我使用相同的代码而不进行内部连接(将其分别用于不同的表(时,我得到了正确的结果,但是一旦我添加内部连接,它就不起作用。请帮助我,因为我被困了 2 个多星期。提前谢谢你。

SQL 演示

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

使用查询从Tbl1中计数A,从Tbl2使用另一个查询中计数A,然后按如下所示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

不仅仅是产品 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)

相关内容

  • 没有找到相关文章

最新更新