Polyeder
Benutzer
- Beiträge
- 10
ich habe unter einer MS SQL-DB 2 Tabellen (tab_z1, tab_z2) mit unterschiedlichen Zeiträumen.
Ich will nun die Zeiträume ermitteln, die in tab_z1 vorhanden sind, aber nicht in tab_z2.
Hier meine Tabellen:
Code:
--drop table tab_z1
--drop table tab_z2
CREATE TABLE [dbo].[tab_z1](
[nr] [int],
[zeit_von_1] [varchar](255) NULL,
[zeit_bis_1] [varchar](255) NULL)
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('1', '07:00', '07:15');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('2', '07:15', '07:30');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('3', '07:30', '07:45');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('4', '07:45', '08:00');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('5', '08:00', '08:15');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('6', '08:15', '08:30');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('7', '08:30', '08:45');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('8', '08:45', '09:00');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('9', '09:00', '09:15');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('10', '09:15', '09:30');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('11', '09:30', '09:45');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('12', '09:45', '10:00');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('13', '10:00', '10:15');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('14', '10:15', '10:30');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('15', '10:30', '10:45');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('16', '10:45', '11:00');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('17', '11:00', '11:15');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('18', '11:15', '11:30');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('19', '11:30', '11:45');
insert into [dbo].[tab_z1] ([nr], [zeit_von_1], [zeit_bis_1]) values ('20', '11:45', '12:00');
GO
CREATE TABLE [dbo].[tab_z2](
[nr] [int],
[zeit_von_2] [varchar](255) NULL,
[zeit_bis_2] [varchar](255) NULL)
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('1', '08:00', '08:15');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('2', '08:15', '08:30');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('3', '08:30', '08:45');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('4', '08:45', '09:00');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('5', '10:00', '10:15');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('6', '10:15', '10:30');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('7', '10:30', '10:45');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('8', '10:45', '11:00');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('9', '11:00', '11:15');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('10', '11:15', '11:30');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('11', '11:30', '11:45');
insert into [dbo].[tab_z2] ([nr], [zeit_von_2], [zeit_bis_2]) values ('12', '11:45', '12:00');
GO
Code:
nr zeit_von_1 zeit_bis_1
1 07:00 07:15
2 07:15 07:30
3 07:30 07:45
4 07:45 08:00
9 09:00 09:15
10 09:15 09:30
11 09:30 09:45
12 09:45 10:00
select zeit_von_1, zeit_bis_1
from tab_z2 inner join tab_z1 on zeit_von_1 = zeit_von_2
scheitern natürlich, da alleine mit dem inner join schon eine Vorbedingung vorhanden ist.
Die Versuche mit right/left outer join und zeit_von_1 <> zeit_von_2 sind leider alle nicht erfolgreich.
Vielleicht hat jemand von euch eine tolle Idee.
Zuletzt bearbeitet: