简体   繁体   中英

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. 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:

SQL Output

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. I've tried to use LEFT JOIN, RIGHT JOIN and OUTER JOIN too but it doesn't work.

How can I develop this query to return only the 2 lines?

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. The other INNER JOINS apparently work. I'm using SQL Server

The MVCE to reproduce my problem:

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 .

Since you never use rt.id for filtering, it causes your result returning 4 lines.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM