Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.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
在SQL报表生成器中使用多种条件联接两个表_Sql_Sql Server_Report - Fatal编程技术网

在SQL报表生成器中使用多种条件联接两个表

在SQL报表生成器中使用多种条件联接两个表,sql,sql-server,report,Sql,Sql Server,Report,我正在使用SQL Report Builder 2016。 我有两个表,命名为资产和折旧信息, 以下是这些表格的结构。 表资产: ID |名称|成本|前期折旧|前期折旧期|前期使用| 价值观就像 123 |姓名| 10000 | 4000 | 06/03/2014 |真实| 表1-1信息: ID |结束日期|当前折旧|累计折旧|结转价值|每月| 价值观就像 123 | 2020-04-30 00:00:00.000 | 2000 | 5000 | 5000 | 0/1| 我想实现

我正在使用SQL Report Builder 2016。 我有两个表,命名为资产和折旧信息, 以下是这些表格的结构。 表资产:


ID |名称|成本|前期折旧|前期折旧期|前期使用|


价值观就像


123 |姓名| 10000 | 4000 | 06/03/2014 |真实|


表1-1信息:


ID |结束日期|当前折旧|累计折旧|结转价值|每月|


价值观就像


123 | 2020-04-30 00:00:00.000 | 2000 | 5000 | 5000 | 0/1|


我想实现以下目标

我想从表资产中选择id,并将根据“id”显示表资产中的所有上述字段以及表dep info中的文件,两个表中的“id”列是相同的

使用下面提到的查询,当Id在两个表中都是公共的时,我成功地获得了所有值

 SELECT
  Assets.ID
  ,Assets.Name
  ,Assets.Cost
  ,Assets.Prior Dep
  ,Assets.Prior Dep Period
  ,Assets.Use Prior
  ,DepreciationInfo.EndDate
  ,DepreciationInfo.CurrentDepreciation
  ,DepreciationInfo.AccumulatedDepreciation
  ,DepreciationInfo.CarryingValue
  ,DepreciationInfo.DepID
  ,DepreciationInfo.Monthly
FROM
  Assets
  INNER JOIN DepreciationInfo
    ON Assets.AssetID = DepreciationInfo.AssetID

where DepreciationInfo.EndDate=@EndDate and DepreciationInfo.Monthly=0
我想要的是,我想要显示来自表资产的所有结果,无论该id是否存在于表折旧信息中

我尝试了所有外部联接,结果是相同的,它显示了具有内部联接和外部联接的记录数


任何帮助都将不胜感激。

将您的内部联接更改为左联接,您将看到表资产的所有结果,无论表折旧信息中是否存在相应的assetid值。这里唯一的问题是查询仍然会受到where子句中的两个过滤器的限制。如果可能,我建议您将其更改为过滤掉资产表中的字段:

SELECT
  Assets.ID
  ,Assets.Name
  ,Assets.Cost
  ,Assets.Prior Dep
  ,Assets.Prior Dep Period
  ,Assets.Use Prior
  ,DepreciationInfo.EndDate
  ,DepreciationInfo.CurrentDepreciation
  ,DepreciationInfo.AccumulatedDepreciation
  ,DepreciationInfo.CarryingValue
  ,DepreciationInfo.DepID
  ,DepreciationInfo.Monthly
FROM
  Assets
  Left JOIN DepreciationInfo
    ON Assets.AssetID = DepreciationInfo.AssetID

where Asset.EndDateorOtherCorrespondingDateValue=@EndDate and Asset.MonthlyorOtherCorrespondingValue=0

将您的位置移动到连接中,并更改为左连接

 SELECT
  Assets.ID
  ,Assets.Name
  ,Assets.Cost
  ,Assets.Prior Dep
  ,Assets.Prior Dep Period
  ,Assets.Use Prior
  ,DepreciationInfo.EndDate
  ,DepreciationInfo.CurrentDepreciation
  ,DepreciationInfo.AccumulatedDepreciation
  ,DepreciationInfo.CarryingValue
  ,DepreciationInfo.DepID
  ,DepreciationInfo.Monthly
FROM
  Assets
  left JOIN DepreciationInfo
    ON Assets.AssetID = DepreciationInfo.AssetID
       and DepreciationInfo.EndDate=@EndDate 
       and DepreciationInfo.Monthly=0
根据您的条件返回所有资产以及与折旧匹配的资产


您不能在where子句中保留联接对象,也不能将该左联接转换为内部联接(因为已消除空值)。

可能的重复项我将研究一下,我不是程序员,我是一名试图学习报表生成器的审计员,因为我想使用自定义的会计报表。感谢您的快速回复。提示:LEFT-OUTER JOINquery正在生成与LEFT-OUTER JOIN、Full-OUTER JOIN和right-OUTER JOIN相同的报告,它显示了总共234个值中的195个值。您的where子句导致了问题,因为您正在使其成为与这些一致性的内部联接。谢谢,它会在这些更改的情况下再添加一页,我现在正在检查如何使用end date参数获得相同的结果。谢谢,这些更改会增加一页,我现在正在检查如何使用end date参数获得相同的结果。您和Daniel的答案都是一样的,基于所有的信息,还有其他方法可以用来获取表格形式的数据吗?我的查询中使用了@enddate。他的查询中根本没有您的参数。他建议在资产表中找到一个值,用作where子句中的过滤器(在您的情况下这是错误的),否则我可能会将其与以下内容混淆:“where asset.EndDate或其他对应的datevalue=@EndDate and asset.monthlyor其他对应的value=0”他基本上是说在资产表中找到一些不存在的东西。ON Assets.AssetID=decreditioninfo.AssetID和decreditioninfo.EndDate=@EndDate和decreditioninfo.Monthly=0——这包括所有参数