删除双左联接中的重复项

时间:2019-03-14 10:34:27

标签: sql sql-server

我知道已经有几个问题了。 但是我找不到他们关于我的案子的答案。

我需要生成一个报告,其中包含来自三个表的一些数据组合。 这些表之间存在关系,但是某些数据可能与相关表不匹配,所以我不能使用内部联接。

所以我尝试了左联接。有点用,但是由于left join,现在我回到很多行。

我在这里设置了一个测试用例-> http://sqlfiddle.com/#!18/637b1/1

CREATE TABLE [xOrder](
[ID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
[SomeText] [nvarchar](255) NOT NULL DEFAULT (''))
GO
CREATE TABLE [Order_Time](
[ID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
[OrderId] [uniqueidentifier] NOT NULL,
[SomeText] [nvarchar](255) NOT NULL DEFAULT (''),
[TimeTypeId] [int] NOT NULL DEFAULT ((-1)),
[TimeType2Id] [int] NOT NULL DEFAULT ((-1)))
GO
CREATE TABLE [Terms](
[ID] [int] NOT NULL,
[CategoryId] [int] NOT NULL,
[SomeText] [nvarchar](255) NOT NULL DEFAULT (''))
GO

Insert into [xOrder] (ID, SomeText) VALUES ('db63ddb9-40d9-4d41-9dfc-5335c400dbd8','aaa')
Insert into [xOrder] (ID, SomeText) VALUES ('ef19af2d-66e9-4de1-a9a2-178b61dfe958','bbb')
Insert into [xOrder] (ID, SomeText) VALUES (newid(),'ccc')
GO
Insert into Order_Time (ID, OrderId, SomeText, TimeTypeId, TimeType2Id) VALUES (newid(),'db63ddb9-40d9-4d41-9dfc-5335c400dbd8', 'time1.1', 1, 1)
Insert into Order_Time (ID, OrderId, SomeText, TimeTypeId, TimeType2Id) VALUES (newid(),'db63ddb9-40d9-4d41-9dfc-5335c400dbd8', 'time1.2', 1, -1)
Insert into Order_Time (ID, OrderId, SomeText, TimeTypeId, TimeType2Id) VALUES (newid(),'db63ddb9-40d9-4d41-9dfc-5335c400dbd8', 'time1.3', -1, -1)
GO
Insert into Order_Time (ID, OrderId, SomeText, TimeTypeId, TimeType2Id) VALUES (newid(),'ef19af2d-66e9-4de1-a9a2-178b61dfe958', 'time2.1', 2, 2)
Insert into Order_Time (ID, OrderId, SomeText, TimeTypeId, TimeType2Id) VALUES (newid(),'ef19af2d-66e9-4de1-a9a2-178b61dfe958', 'time2.2', 2, -1)
Insert into Order_Time (ID, OrderId, SomeText, TimeTypeId, TimeType2Id) VALUES (newid(),'ef19af2d-66e9-4de1-a9a2-178b61dfe958', 'time2.3', -1, -1)
GO
Insert into Terms (ID, CategoryId, SomeText) VALUES (1, 1, 'Term1')
Insert into Terms (ID, CategoryId, SomeText) VALUES (2, 1, 'Term2')
Insert into Terms (ID, CategoryId, SomeText) VALUES (3, 1, 'Term3')
Insert into Terms (ID, CategoryId, SomeText) VALUES (1, 2, 'Category1')
Insert into Terms (ID, CategoryId, SomeText) VALUES (2, 2, 'Category2')
Insert into Terms (ID, CategoryId, SomeText) VALUES (3, 2, 'Category3')
GO

这是我尝试过的查询。

select o.SomeText as OrderText, ot.SomeText as TimeText1, coalesce(t.SomeText, 'NotFound') as TermText, coalesce(tt.SomeText, 'NotFound') as CategoryText from xOrder o
inner join order_time ot on o.id = ot.OrderId
left join terms t on ot.TimeTypeId = t.Id
left join terms tt on (ot.TimeType2Id = t.Id and t.ID = 2)

我期望的结果是包含以下内容的6行:

----------------------------------------
| aaa | Time1.1 | Term1    | Category1 |
| aaa | Time1.2 | Term1    | NotFound  |
| aaa | Time1.2 | NotFound | NotFound  |
| bbb | Time2.1 | Term2    | Category2 |
| bbb | Time2.2 | Term2    | NotFound  |
| bbb | Time2.2 | NotFound | NotFound  |
----------------------------------------

但是那没有发生。那么如何从左侧联接中删除多余的行?

3 个答案:

答案 0 :(得分:2)

我想这就是你想要的:

select o.SomeText as OrderText, ot.SomeText as TimeText1,
       coalesce(t.SomeText, 'NotFound') as TermText,
       coalesce(tt.SomeText, 'NotFound') as CategoryText
from xOrder o inner join
     order_time ot
     on o.id = ot.OrderId left join
     terms t
     on ot.TimeTypeId = t.Id and t.CategoryId = 1 left join
     terms tt
     on ot.TimeType2Id = tt.Id and tt.CategoryId = 2;

Here是db <>小提琴。

您的查询表别名存在问题,这就是为什么您会得到重复项的原因。

答案 1 :(得分:1)

您需要更改加入条件

DEMO

select distinct o.SomeText as OrderText, ot.SomeText as TimeText1,
coalesce(t.SomeText, 'NotFound') as TermText, 
coalesce(tt.SomeText, 'NotFound') as CategoryText
from xOrder o
inner join order_time ot on o.id = ot.OrderId  
left join terms t on ot.TimeTypeId = t.id and  t.CategoryId=1
left join terms tt on ot.TimeTypeId = tt.id and  tt.CategoryId=2

输出:

OrderText   TimeText1   TermText    CategoryText
aaa         time1.1     Term1        Category1
aaa         time1.2     Term1        Category1
aaa         time1.3     NotFound     NotFound
bbb         time2.1     Term2        Category2
bbb         time2.2     Term2        Category2
bbb         time2.3     NotFound     NotFound

答案 2 :(得分:1)

请尝试以下查询:

select o.SomeText, ot.sometext,
       coalesce((select someText from terms where id = ot.TimetypeId and categoryid = 1), 'NotFound') TimeType,
       coalesce((select someText from terms where id = ot.Timetype2Id and categoryid = 2), 'NotFound') CategroyId
from xOrder o
join order_time ot on o.id = ot.OrderId
相关问题