Я знаю, что уже есть несколько вопросов по этому поводу.Но я не могу найти от них ответа о моем случае.
Мне нужно подготовить отчет с некоторыми объединениями данных из трех таблиц.Между этими таблицами есть отношения, но некоторые данные могут не соответствовать связанной таблице, поэтому я не могу использовать внутреннее соединение.
Поэтому я попытался с левым соединением.Это сработало, но теперь я возвращаюсь ко многим строкам из-за 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 |
----------------------------------------
Но этого не происходит.Итак, как мне удалить лишние строки из левых соединений?