Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Union All返回表中没有记录的值_Mysql_Sql - Fatal编程技术网

Mysql Union All返回表中没有记录的值

Mysql Union All返回表中没有记录的值,mysql,sql,Mysql,Sql,我想根据相关表的值分别显示重新提交和首次提交都是索赔类型 因此,我使用下面的查询分别获取这两条记录 SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, MAX(c.ClaimID), count(c.ClaimID) as claims, MAX(h.TransactionDate) as TransactionDate, 'Resubmission' AS 'Claim Type' FRO

我想根据相关表的值分别显示重新提交和首次提交都是索赔类型

因此,我使用下面的查询分别获取这两条记录

SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, 
       MAX(c.ClaimID), count(c.ClaimID) as claims, 
       MAX(h.TransactionDate) as TransactionDate, 
       'Resubmission' AS 'Claim Type' 
FROM Claim c 
LEFT OUTER JOIN ClaimHeader h on h.HeaderID = c.HeaderPKID 
INNER JOIN Resubmission r ON r.ClaimID = c.ClaimPKID WHERE h.HeaderType=2 

UNION ALL 

SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, 
       MAX(c.ClaimID), count(c.ClaimID) as claims, 
       MAX(h.TransactionDate) as TransactionDate, 
      'First Submission' AS 'Claim Type' 
FROM Claim c 
LEFT OUTER JOIN ClaimHeader h on h.HeaderID = c.HeaderPKID 
WHERE ClaimPKID NOT IN 
         ( SELECT ClaimID FROM Resubmission GROUP BY ClaimID ) AND HeaderType=2
在上面的查询中,我使用UNIONALL从索赔表中获取数据。如果索赔表主键在重新提交表中用作外键,则它是重新提交索赔类型,如果它未在重新提交表中使用,则它是首次提交

我的问题是,尽管表中没有特定select查询的记录,但它返回空值并提及索赔类型。请参考下面的屏幕截图

试试这个:

select * 
from
(SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, 
       MAX(c.ClaimID) claimid, count(c.ClaimID) as claims, 
       MAX(h.TransactionDate) as TransactionDate, 
       'Resubmission' AS 'Claim Type' 
FROM Claim c 
LEFT OUTER JOIN ClaimHeader h on h.HeaderID = c.HeaderPKID 
INNER JOIN Resubmission r ON r.ClaimID = c.ClaimPKID WHERE h.HeaderType=2 

UNION ALL 

SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, 
       MAX(c.ClaimID), count(c.ClaimID) as claims, 
       MAX(h.TransactionDate) as TransactionDate, 
      'First Submission' AS 'Claim Type' 
FROM Claim c 
LEFT OUTER JOIN ClaimHeader h on h.HeaderID = c.HeaderPKID 
WHERE ClaimPKID NOT IN 
         ( SELECT ClaimID FROM Resubmission GROUP BY ClaimID ) AND HeaderType=2) claims
where claims.claims <> 0
试试这个:

select * 
from
(SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, 
       MAX(c.ClaimID) claimid, count(c.ClaimID) as claims, 
       MAX(h.TransactionDate) as TransactionDate, 
       'Resubmission' AS 'Claim Type' 
FROM Claim c 
LEFT OUTER JOIN ClaimHeader h on h.HeaderID = c.HeaderPKID 
INNER JOIN Resubmission r ON r.ClaimID = c.ClaimPKID WHERE h.HeaderType=2 

UNION ALL 

SELECT ROUND(coalesce(SUM(c.ClaimNet), 0), 2) as net, 
       MAX(c.ClaimID), count(c.ClaimID) as claims, 
       MAX(h.TransactionDate) as TransactionDate, 
      'First Submission' AS 'Claim Type' 
FROM Claim c 
LEFT OUTER JOIN ClaimHeader h on h.HeaderID = c.HeaderPKID 
WHERE ClaimPKID NOT IN 
         ( SELECT ClaimID FROM Resubmission GROUP BY ClaimID ) AND HeaderType=2) claims
where claims.claims <> 0

没有Group By的聚合始终返回一行,即使没有数据。为了避免这种情况,您可以简单地添加COUNT*>0:


没有Group By的聚合始终返回一行,即使没有数据。为了避免这种情况,您可以简单地添加COUNT*>0:


ROUNDcoalesceSUMc.ClaimNet,0,2是worng,roundsumcalescecc.ClaimNet,0,2是正确的,谢谢…但这不是问题oundcoalescesumc.ClaimNet,0,2是worng,roundsumcalescecc.ClaimNet,0,2是正确的,谢谢……但这不是一个问题。它可能有助于描述它是如何解决问题的。它可能有助于描述它是如何解决问题的。