[英]Select and Inner Join Returning Duplicates
I'm developing a query to print a report about user travel.我正在开发一个查询来打印有关用户旅行的报告。 However, this query is returning 4 rows instead of two.
但是,此查询返回 4 行而不是两行。 Explaining the business rule: A user creates a travel solicitation and adds different destinations as a route.
解释业务规则:用户创建旅行请求并将不同的目的地添加为路线。 The user must define the percentage that each subsidiary of the route must pay for the travel.
用户必须定义路线的每个子公司必须为旅行支付的百分比。 The trip can have multiple destinations or just one.
旅行可以有多个目的地,也可以只有一个。 When I execute the following query:
当我执行以下查询时:
Select Distinct sv.Description,
st.Description,
us.Code, us.Name,
rt.Destination, rt.Description, rt.BeginDate, rt.FInishDate,
rat.Percentage,
fl.Code, fl.Name
FROM TravelSolicitation sv
INNER JOIN Situation st ON sv.IdSituation = st.Id
INNER JOIN Duser us ON sv.IdUser = us.Id
INNER JOIN Itinerary rt ON rt.IdSolicitation = sv.Id
INNER JOIN Apportionment rat ON rat.IdSolicitation = sv.Id
INNER JOIN Subsidiary fl ON rat.IdSubsidiary = fl.Id
WHERE sv.Id = 1
I get the following result:我得到以下结果:
My Problem: Considering the Apportionment table the query result should show only 2 lines since the Apportionment for each subsidiary is 80 and 20. If I use 3 different apportionment (ie 50,30,20) for 3 subsidiaries the query result will show 9 lines.我的问题:考虑到分配表,查询结果应该只显示 2 行,因为每个子公司的分配是 80 和 20。如果我对 3 个子公司使用 3 种不同的分配(即 50,30,20),查询结果将显示 9 行. I've tried to use LEFT JOIN, RIGHT JOIN and OUTER JOIN too but it doesn't work.
我也尝试使用 LEFT JOIN、RIGHT JOIN 和 OUTER JOIN 但它不起作用。
How can I develop this query to return only the 2 lines?如何开发此查询以仅返回 2 行?
My Expected output:我的预期输出:
+----------+-------------+-----+------+------------+------+-----------+------------+------+---+------------+
| Travel 1 | Credit Test | 110 | Paul | Ny | Work | BeginDate | FinishDate | 20.0 | 4 | City B Sub |
+----------+-------------+-----+------+------------+------+-----------+------------+------+---+------------+
| Travel 1 | Credit Test | 110 | Paul | Washington | Fun | BeginDate | FinishDate | 80.0 | 2 | City A Sub |
+----------+-------------+-----+------+------------+------+-----------+------------+------+---+------------+
Note: I notice that the Apportionment INNER JOIN generates the 4 lines.注意:我注意到 Apportionment INNER JOIN 生成了 4 行。 The other INNER JOINS apparently work.
其他 INNER JOINS 显然有效。 I'm using SQL Server
我正在使用 SQL Server
The MVCE to reproduce my problem: MVCE重现我的问题:
Tables:表格:
CREATE TABLE [Subsidiary] (
[Id] bigint NOT NULL,
[Code] varchar(4) NOT NULL,
[Name] varchar(40) NOT NULL,
CONSTRAINT [PK_Subsidiary_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION=NONE) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [Apportionment] (
[Id] bigint NOT NULL,
[IdSubsidiary] bigint NOT NULL,
[IdSolicitation] bigint NOT NULL,
[Percentage] decimal(19,4) NOT NULL,
CONSTRAINT [PK_Apportionment_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION=NONE) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [Itinerary] (
[Id] bigint NOT NULL,
[IdSolicitation] bigint NOT NULL,
[Destination] varchar(200) NULL,
[Description] varchar(50) NOT NULL,
[BeginDate] datetime NOT NULL,
[FInishDate] datetime NULL,
[MultipleItinerary] bit NOT NULL,
CONSTRAINT [PK_Itinerary_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION=NONE) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [Situation](
[Id] bigint NOT NULL,
[Name] varchar(40) NOT NULL,
[Description] varchar(150) NOT NULL,
[ValidateDate] smallint NOT NULL,
CONSTRAINT [PK_Situation_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [TravelSolicitation]
([Id] bigint IDENTITY(1,1) NOT NULL,
[IdSituation] bigint NOT NULL,
[IdUser] bigint NOT NULL,
[Description] varchar(150) NOT NULL,
[GoDate] datetime NOT NULL,
[BackDate] datetime NOT NULL,
CONSTRAINT [PK_TravelSolicitation_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION=NONE) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [Duser] (
[Id] bigint NOT NULL,
[Code] varchar(10),
[Name] varchar(70) NOT NULL,
CONSTRAINT [PK_Duser_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, DATA_COMPRESSION=NONE) ON [PRIMARY]
) ON [PRIMARY]
GO
Foreign Keys:外键:
ALTER TABLE Apportionment
ADD FOREIGN KEY (IdSubsidiary) REFERENCES Subsidiary(Id);
ALTER TABLE Apportionment
ADD FOREIGN KEY (IdSolicitation) REFERENCES TravelSolicitation(Id);
ALTER TABLE Itinerary
ADD FOREIGN KEY (IdSolicitation) REFERENCES TravelSolicitation(Id);
ALTER TABLE TravelSolicitation
ADD FOREIGN KEY (IdSituation) REFERENCES Situation(Id);
ALTER TABLE TravelSolicitation
ADD FOREIGN KEY (IdUser) REFERENCES Duser(Id);
Inserts插入
INSERT INTO Duser (Id,Code,Name) VALUES (1,110,'Paul')
INSERT INTO Situation (Id,Name,Description,ValidateDate) VALUES (1,'Situation A','Credit Test',1)
INSERT INTO Subsidiary (Id,Code,Name) VALUES (1,'2','City A Sub');
INSERT INTO Subsidiary (Id,Code,Name) VALUES (2,'4','City B Sub');
INSERT INTO TravelSolicitation(IdSituation,IdUser,Description,GoDate,BackDate) VALUES (1,1,'Travel 1','20-10-2021','20-11-2021');
INSERT INTO Itinerary (Id,IdSolicitation,Destination,Description,BeginDate,FInishDate,MultipleItinerary) VALUES (1,1,'Washington','Fun','20-10-2021','10-11-2021',1)
INSERT INTO Itinerary (Id,IdSolicitation,Destination,Description,BeginDate,FInishDate,MultipleItinerary) VALUES (2,1,'NY','Work','10-11-2021','20-11-2021',1)
INSERT INTO Apportionment(Id,IdSubsidiary,IdSolicitation,Percentage) VALUES (1,1,1,80.0)
INSERT INTO Apportionment(Id,IdSubsidiary,IdSolicitation,Percentage) VALUES (2,2,1,20.0)
Just add and rt.id = rat.id
on INNER JOIN Apportionment rat ON rat.IdSolicitation = sv.Id
.只需添加
and rt.id = rat.id
在INNER JOIN Apportionment rat ON rat.IdSolicitation = sv.Id
。
Since you never use rt.id
for filtering, it causes your result returning 4 lines.由于您从不使用
rt.id
进行过滤,它会导致您的结果返回 4 行。
Select Distinct sv.Description,
st.Description,
us.Code, us.Name,
rt.Destination, rt.Description, rt.BeginDate, rt.FInishDate,
rat.Percentage,
fl.Code, fl.Name
FROM TravelSolicitation sv
INNER JOIN Situation st ON sv.IdSituation = st.Id
INNER JOIN Duser us ON sv.IdUser=us.Id
INNER JOIN Itinerary rt ON rt.IdSolicitation = sv.Id
INNER JOIN Apportionment rat ON rat.IdSolicitation = sv.Id and rt.id = rat.id
INNER JOIN Subsidiary fl ON rat.IdSubsidiary=fl.Id
WHERE sv.Id = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.