SQL 使用 INNER Join 的查询返回多个重复值

SQL Query with INNER Join is returning multiple duplicate values

一个视图和一个 table、vwGrossEnergyYields 和 tblTurbineLayouts 正在通过内部联接组合。每个 table/view 包含一个字段 ProjectID 和 IsLive。通过示例 ProjectID 和 IsLive = 1 returns 查询每个 table/view:

一行
SELECT [TurbineLayoutProjectID]
    ,[TurbineLayoutNumber]
    ,[Number]
    ,[ProjectID]
    
  FROM [GrossEnergy].[dbo].[vwGrossEnergyYields]
  
 WHERE [IsLive] = 1
 AND [ProjectID] IN
  (
    '2835'
  )

给出:

TurbineLayoutProjectID  TurbineLayoutNumber Number  ProjectID
2835    52  170 2835

SELECT  [Number]
    ,[ProjectID]
    
FROM [TurbineLayout].[dbo].[tblTurbineLayouts]
  
WHERE [IsLive] = 1
AND [ProjectID] IN
  (
    '2835'
  )

给予

Number  ProjectID
52  2835

编写了一个查询,使用 INNER 联接,在 TurbineLayoutNumber 上联接,打算 return 只有一行:

SELECT 
      [vwGrossEnergyYields].[TurbineLayoutProjectID] As TurbineLayoutProjID
      ,[vwGrossEnergyYields].[ProjectID]  As YieldProjID
      ,[vwGrossEnergyYields].[TurbineLayoutNumber]
      ,[vwGrossEnergyYields].[Number] As YieldNumber
  FROM [GrossEnergy].[dbo].[vwGrossEnergyYields]
  
  INNER JOIN [TurbineLayout].[dbo].[tblTurbineLayouts]
  ON [vwGrossEnergyYields].[TurbineLayoutNumber] = [tblTurbineLayouts].[Number]   
 
  WHERE [vwGrossEnergyYields].[IsLive] = 1
  AND [tblTurbineLayouts].[IsLive] = 1
  AND
  [vwGrossEnergyYields].[ProjectID] IN
  (
    '2835'
  )

这 return 五行:

TurbineLayoutProjID YieldProjID TurbineLayoutNumber YieldNumber
2835    2835    52  170
2835    2835    52  170
2835    2835    52  170
2835    2835    52  170
2835    2835    52  170

查询中的错误是什么导致它 return 五行而不是一行?

查询连接到一对多关系意味着 - [vwGrossEnergyYields].[TurbineLayoutNumber] = [tblTurbineLayouts].[Number] - 此连接在其中一个表中有多个值 - 尝试连接不同的字段或者在您的查询中放入不同的关键字,例如:

SELECT DISTINCT
  [vwGrossEnergyYields].[TurbineLayoutProjectID] As TurbineLayoutProjID
  ,[vwGrossEnergyYields].[ProjectID]  As YieldProjID
  ,[vwGrossEnergyYields].[TurbineLayoutNumber]
  ,[vwGrossEnergyYields].[Number] As YieldNumber
 FROM [GrossEnergy].[dbo].[vwGrossEnergyYields]

 INNER JOIN [TurbineLayout].[dbo].[tblTurbineLayouts]
 ON [vwGrossEnergyYields].[TurbineLayoutNumber] = [tblTurbineLayouts].[Number]   

 WHERE [vwGrossEnergyYields].[IsLive] = 1
 AND [tblTurbineLayouts].[IsLive] = 1
 AND
      [vwGrossEnergyYields].[ProjectID] IN
 (
     '2835'
 )
  • SELECT DISTINCT语句用于return只区分 (不同的)值。在table里面,一列往往包含很多 重复值;有时你只想列出不同的 (不同的)值。

    SELECT [vwGrossEnergyYields].[TurbineLayoutProjectID] 作为 TurbineLayoutProjID ,[vwGrossEnergyYields].[ProjectID] 作为 YieldProjID ,[vwGrossEnergyYields].[TurbineLayoutNumber] ,[vwGrossEnergyYields].[Number] 作为 YieldNumber 来自 [GrossEnergy].[dbo].[vwGrossEnergyYields]

    内部联接 [TurbineLayout].[dbo].[tblTurbineLayouts] ON [vwGrossEnergyYields].[TurbineLayoutNumber] = [tblTurbineLayouts].[Number]

    哪里 [vwGrossEnergyYields].[IsLive] = 1 AND [tblTurbineLayouts].[IsLive] = 1 和 [vwGrossEnergyYields].[ProjectID] IN ( '2835' )

正如我在评论中提到的,大概你有一个 One-To-Many 关系,所以每个相关行你得到 1 行。

由于您没有从 table tblTurbineLayouts 返回任何数据,因此 EXISTS 似乎是更好的解决方案:

SELECT GEY.TurbineLayoutProjectID AS TurbineLayoutProjID,
       GEY.ProjectID AS YieldProjID,
       GEY.TurbineLayoutNumber,
       GEY.Number AS YieldNumber
FROM GrossEnergy.dbo.vwGrossEnergyYields GEY
WHERE GEY.IsLive = 1
  AND GEY.ProjectID = '2835' --Change to =. Also should the value be an int, rather than a varchar(4)?
  AND EXISTS (SELECT 1
              FROM TurbineLayout.dbo.tblTurbineLayouts TL
              WHERE TL.Number = GEY.TurbineLayoutNumber
                AND TL.IsLive = 1)

我也做了一些 QoL 的改变,比如别名。