SQL查询带有3个表返回重复行

发布于 2025-02-14 01:30:20 字数 4167 浏览 0 评论 0原文

希望这不会被标记为重复。当查询2个以上的表格时,我正在努力理解重复行的返回。我已经在网上阅读了许多类似的问题和帖子,但无法将我的头缠绕在逻辑上。我希望,如果有人可以用我的特定用例解释,它最终应该沉入。

我有3张桌子

[dbo].[Branch](
    [ContractorCode] [int] NOT NULL,
    [BranchNumber] [int] NOT NULL,
    [BranchName] [varchar](50) NOT NULL,
    ...
    [HealthBoardID] [int] NULL
[dbo].[OutstandingCount](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [EnteredOn] [datetime2](7) NOT NULL,    
    [Branch] [varchar](50) NOT NULL,
    [productGroup] [int] NULL,  
    [Product] [nvarchar](100) NOT NULL, 
    ...
    [Satisfied] [bit] NOT NULL
[dbo].[TargetLineCount](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Branch] [int] NOT NULL,
    [ProductGroup] [nvarchar](10) NOT NULL,
    [TargetCount] [int] NOT NULL,
    ...

有一个1:m的关系,从分支表到出色的表格表,从分支表到Targetlinecount有一个1:1的关系表

我需要返回的是分支机构 仍需计算多少个项目,因此满足= 0的未偿还计数中有多少行 每天添加多少个项目,这是由分支机构和产品组决定的。

这是我的查询

SELECT b.BranchName,
SUM(CASE WHEN o.ProductGroup = 2 THEN 1 ELSE 0 END) AS [NHS to count],
MAX(CASE WHEN t.ProductGroup = 'DISP' THEN t.TargetCount ELSE 0 END) AS [NHS daily count],
SUM(CASE WHEN o.ProductGroup = 1 THEN 1 ELSE 0 END) AS [OTC to count],
MAX(CASE WHEN t.ProductGroup = 'OTC' THEN t.TargetCount ELSE 0 END) AS [OTC daily count]
FROM OutstandingCount o
INNER JOIN Branch b ON o.Branch = b.BranchName
INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber
WHERE o.Satisfied = 0 AND o.EnteredOn < '2022-07-01'
GROUP BY b.BranchNumber,b.BranchName
ORDER BY b.BranchNumber

目标计数返回正确,例如,分支1 [NHS/OTC每日计数]为7,分支2为8。但是[NHS/OTC计数]数字增加了一倍,例如分支1为65 但是14。

但是是130,分支2应该是7 , 如果我单独请求两个数据,我会得到所需的结果,只有当我尝试在一个查询中获取所有信息时,我才会挣扎。

样本数据和

ContractorCode

BranchNumberBranchName1234
1Branch15465
2Branch2ExtistCount

EnteredonBranch产品集群产品满足
99902022-07-07-07-07-01pr551550
99912022-07-07-07-07-0755ID分支输出
分支1 2PR780
99932022-07-01Branch21Pr550
99952022-07-02分支2 1PR780
99962022-07-02分支2 Pr30 2Pr300
99982022-07-07-07-03分支2 Pr55 1pr551

TargetLineCount:

IDBranchproduct Groupttarget target
11pg17
21PG2 10 4 2 2 1 PG210
4 22PG18
52PG28

所需的结果将是:

分支NHS计算NHS每日计数OTC要计数OTC每日计数
分支1 11027
分支2 1828

在解释中 - 分支1没有满意的项目,因此我们从productGroup1到计数2,从productGroup2有1个。 Branch2具有1个满足,因此我们计算了ProductGroup1的2和1 productGroup2。来自TargetLineCount表的计数数字为1:1,因此productGroup1的branch1值只有一个值,而productGroup2的branch1值只有一个值。

解决方案:

多亏了Dragon的帮助,我的具体问题需要我添加另一表以加入,然后扩展查询以使用该表

INNER JOIN ProductGroup p ON p.Branch = b.ContractorCode AND p.ProdGroupID = o.productGroup

INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber AND t.ProductGroup = p.Description

Hopefully this won't get flagged as duplicate. I am struggling to understand the return of duplicate rows when more than 2 tables are in query. I have read a number of similar questions and posts online but can't wrap my head around the logic. I am hoping that if someone could explain with my specific use case, it should finally sink in.

I have 3 tables

[dbo].[Branch](
    [ContractorCode] [int] NOT NULL,
    [BranchNumber] [int] NOT NULL,
    [BranchName] [varchar](50) NOT NULL,
    ...
    [HealthBoardID] [int] NULL
[dbo].[OutstandingCount](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [EnteredOn] [datetime2](7) NOT NULL,    
    [Branch] [varchar](50) NOT NULL,
    [productGroup] [int] NULL,  
    [Product] [nvarchar](100) NOT NULL, 
    ...
    [Satisfied] [bit] NOT NULL
[dbo].[TargetLineCount](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Branch] [int] NOT NULL,
    [ProductGroup] [nvarchar](10) NOT NULL,
    [TargetCount] [int] NOT NULL,
    ...

There is a 1:M relation from Branch table to OutstandingCount table and there is a 1:1 relation from Branch table to TargetLineCount table

What I need to return is by branch
how many items are still to count so how many rows in OutstandingCount with Satisfied = 0
how many items are added to the count each day, this is decided by the branch and product group.

This is my query

SELECT b.BranchName,
SUM(CASE WHEN o.ProductGroup = 2 THEN 1 ELSE 0 END) AS [NHS to count],
MAX(CASE WHEN t.ProductGroup = 'DISP' THEN t.TargetCount ELSE 0 END) AS [NHS daily count],
SUM(CASE WHEN o.ProductGroup = 1 THEN 1 ELSE 0 END) AS [OTC to count],
MAX(CASE WHEN t.ProductGroup = 'OTC' THEN t.TargetCount ELSE 0 END) AS [OTC daily count]
FROM OutstandingCount o
INNER JOIN Branch b ON o.Branch = b.BranchName
INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber
WHERE o.Satisfied = 0 AND o.EnteredOn < '2022-07-01'
GROUP BY b.BranchNumber,b.BranchName
ORDER BY b.BranchNumber

The target counts return correct, e.g. for branch 1 the [NHS/OTC daily count] is 7 and branch 2 it is 8. However the [NHS/OTC to count] figures are doubled, e.g. branch 1 should be 65 but is 130, branch 2 should be 7 but is 14.

As I say, it is all coming from my lack of understanding how to treat multiple table joins. If I request either of the data separately, I get the desired result, it is only when I try and get all the information in a single query I struggle.

Sample data and output:

Branch:

ContractorCodeBranchNumberBranchName
12341Branch1
54652Branch2

OutstandingCount:

IdEnteredOnBranchProductGroupProductSatisfied
99902022-07-01Branch11pr550
99912022-07-01Branch11pr600
99922022-07-02Branch12pr780
99932022-07-01Branch21pr550
99952022-07-02Branch21pr780
99962022-07-02Branch22pr300
99982022-07-03Branch22pr551

TargetLineCount:

IDBranchProductGroupTarget
11PG17
21PG210
42PG18
52PG28

Desired Result would be:

BranchNHS To CountNHS Daily CountOTC To CountOTC Daily Count
Branch111027
Branch21828

In explanation - Branch1 has no satisfied items so we have 2 from ProductGroup1 to count and 1 from ProductGroup2. Branch2 has 1 satisfied so we count 2 for ProductGroup1 and 1 for ProductGroup2. The to count figures from the TargetLineCount table are a 1:1 so there is only one value for Branch1 for ProductGroup1 and one value for Branch1 for ProductGroup2.

Solution:

Thanks to the help from Dragon, my specific issue needed me to add a further table for joining and then extend the queries to use that table as well

INNER JOIN ProductGroup p ON p.Branch = b.ContractorCode AND p.ProdGroupID = o.productGroup

INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber AND t.ProductGroup = p.Description

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2025-02-21 01:30:20

好的,我知道问题是什么。

正是通过您的加入,

INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber

应该是这样的:

INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber AND o.ProductGroup = t.ProductGroup

唯一的是targetlinecount和downdecount之间的产品组连接在您的示例中似乎没有正确显示,因为一个是varchar,另一个是int?

Ok, I see what the issue is.

It is with your join

INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber

It should be like this:

INNER JOIN TargetLineCount t ON t.Branch = b.BranchNumber AND o.ProductGroup = t.ProductGroup

The only thing is the Product Group connection between TargetLineCount and OutstandingCount doesn't appear to be shown properly in your example as one is a varchar and the other is an int?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文