SQL查询带有3个表返回重复行
希望这不会被标记为重复。当查询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
BranchNumber | BranchName | 1234 |
---|---|---|
1 | Branch1 | 5465 |
2 | Branch2 | ExtistCount |
:
Enteredon | Branch | 产品 | 集群 | 产品 | 满足 |
---|---|---|---|---|---|
9990 | 2022-07-07-07-07-01 | pr55 | 1 | 55 | 0 |
9991 | 2022-07-07-07-07-07 | 55 | ID | 分支 | 输出 |
: | : | 分支 | 1 2 | PR78 | 0 |
9993 | 2022-07-01 | Branch2 | 1 | Pr55 | 0 |
9995 | 2022-07-02 | 分支 | 2 1 | PR78 | 0 |
9996 | 2022-07-02 | 分支 | 2 Pr30 2 | Pr30 | 0 |
9998 | 2022-07-07-07-03 | 分支 | 2 Pr55 1 | pr55 | 1 |
TargetLineCount:
ID | Branch | product Groupt | target target |
---|---|---|---|
1 | 1 | pg1 | 7 |
2 | 1 | PG2 10 4 2 2 1 PG2 | 10 |
4 2 | 2 | PG1 | 8 |
5 | 2 | PG2 | 8 |
所需的结果将是:
分支 | NHS计算 | NHS每日计数 | OTC要计数 | OTC每日计数 |
---|---|---|---|---|
分支 | 1 1 | 10 | 2 | 7 |
分支 | 2 1 | 8 | 2 | 8 |
在解释中 - 分支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:
ContractorCode | BranchNumber | BranchName |
---|---|---|
1234 | 1 | Branch1 |
5465 | 2 | Branch2 |
OutstandingCount:
Id | EnteredOn | Branch | ProductGroup | Product | Satisfied |
---|---|---|---|---|---|
9990 | 2022-07-01 | Branch1 | 1 | pr55 | 0 |
9991 | 2022-07-01 | Branch1 | 1 | pr60 | 0 |
9992 | 2022-07-02 | Branch1 | 2 | pr78 | 0 |
9993 | 2022-07-01 | Branch2 | 1 | pr55 | 0 |
9995 | 2022-07-02 | Branch2 | 1 | pr78 | 0 |
9996 | 2022-07-02 | Branch2 | 2 | pr30 | 0 |
9998 | 2022-07-03 | Branch2 | 2 | pr55 | 1 |
TargetLineCount:
ID | Branch | ProductGroup | Target |
---|---|---|---|
1 | 1 | PG1 | 7 |
2 | 1 | PG2 | 10 |
4 | 2 | PG1 | 8 |
5 | 2 | PG2 | 8 |
Desired Result would be:
Branch | NHS To Count | NHS Daily Count | OTC To Count | OTC Daily Count |
---|---|---|---|---|
Branch1 | 1 | 10 | 2 | 7 |
Branch2 | 1 | 8 | 2 | 8 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我知道问题是什么。
正是通过您的加入,
应该是这样的:
唯一的是targetlinecount和downdecount之间的产品组连接在您的示例中似乎没有正确显示,因为一个是varchar,另一个是int?
Ok, I see what the issue is.
It is with your join
It should be like this:
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?