SQL 在两行中查找有关 Person 的信息
SQL Finding information about Person in two rows
我必须关注名为 Data1 的数据库
DateOfBooking | Short | Long | TimeOfBooking | ID
-------------------------------------------------------
14.06.2016 | KAL | blabla| 13:02 | 1
14.06.2016 | DEF | del | 14:02 | 3
14.06.2016 | KAL | blabla| 17:34 | 2
14.06.2016 | DOL | blub | 13:02 | 1
我想找到此人的 ID 是 13:02 的 KAL 和 13:02 的 DOL,但前提是两者都被预订(同时)。
KAL 和 DOL 总是在同一个 TimeOfBooking 为一个 ID 预订,但我不知道如何得到结果。
我试过了
SELECT DISTINCT Data1.ID
FROM Data1
WHERE (((Data1.Short = 'KAL') AND (Data1.Long Like 'blabla'))
AND ((((Data1.Short = 'DOL') AND (Data1.Long Like 'blub')))
Group BY Data1.ID
当然这不起作用,因为它只查看一行。有没有办法查看两行并找到相应的 ID?
谢谢。
不太确定您在问什么,但是当 KOL 和 DOL 具有相同的 ID 和时间戳时,这将 return 数据:
select tk.*, td.*
from (select * from data1 where Short = 'KAL') tk
join (select * from data1 where Short = 'DOL') td
ON tk.id = td.id and tk.TimeOfBooking = td.TimeOfBooking
一种方法使用聚合,按 ID 和预订时间 -- 然后检查两个 short
值:
select d.id
from data1 d
where d.short in ('KAL', 'DOL')
group by d.id, d.timeofbooking
having count(distinct d.short) = 2;
如果你想要完整的记录,另一种方法是使用 exists
,但有点复杂:
select d.*
from data1 d
where (d.short = 'KAL' and
exists (select 1 from data1 d2
where d2.id = d.id and
d2.timeofbooking = d.timeofbooking and
d2.short = 'DOL'
)
) or
(d.short = 'DOL' and
exists (select 1 from data1 d2
where d2.id = d.id and
d2.timeofbooking = d.timeofbooking and
d2.short = 'KAL'
)
);
或者,甚至,使用 window 函数:
select d.*
from (select d.*,
min(short) over (partition by id, timeofbooking) as minshort,
max(short) over (partition by id, timeofbooking) as maxshort
from data1
where short in ('KAL', 'DOL')
) d
where minshort <> maxshort;
我必须关注名为 Data1 的数据库
DateOfBooking | Short | Long | TimeOfBooking | ID
-------------------------------------------------------
14.06.2016 | KAL | blabla| 13:02 | 1
14.06.2016 | DEF | del | 14:02 | 3
14.06.2016 | KAL | blabla| 17:34 | 2
14.06.2016 | DOL | blub | 13:02 | 1
我想找到此人的 ID 是 13:02 的 KAL 和 13:02 的 DOL,但前提是两者都被预订(同时)。
KAL 和 DOL 总是在同一个 TimeOfBooking 为一个 ID 预订,但我不知道如何得到结果。 我试过了
SELECT DISTINCT Data1.ID
FROM Data1
WHERE (((Data1.Short = 'KAL') AND (Data1.Long Like 'blabla'))
AND ((((Data1.Short = 'DOL') AND (Data1.Long Like 'blub')))
Group BY Data1.ID
当然这不起作用,因为它只查看一行。有没有办法查看两行并找到相应的 ID?
谢谢。
不太确定您在问什么,但是当 KOL 和 DOL 具有相同的 ID 和时间戳时,这将 return 数据:
select tk.*, td.*
from (select * from data1 where Short = 'KAL') tk
join (select * from data1 where Short = 'DOL') td
ON tk.id = td.id and tk.TimeOfBooking = td.TimeOfBooking
一种方法使用聚合,按 ID 和预订时间 -- 然后检查两个 short
值:
select d.id
from data1 d
where d.short in ('KAL', 'DOL')
group by d.id, d.timeofbooking
having count(distinct d.short) = 2;
如果你想要完整的记录,另一种方法是使用 exists
,但有点复杂:
select d.*
from data1 d
where (d.short = 'KAL' and
exists (select 1 from data1 d2
where d2.id = d.id and
d2.timeofbooking = d.timeofbooking and
d2.short = 'DOL'
)
) or
(d.short = 'DOL' and
exists (select 1 from data1 d2
where d2.id = d.id and
d2.timeofbooking = d.timeofbooking and
d2.short = 'KAL'
)
);
或者,甚至,使用 window 函数:
select d.*
from (select d.*,
min(short) over (partition by id, timeofbooking) as minshort,
max(short) over (partition by id, timeofbooking) as maxshort
from data1
where short in ('KAL', 'DOL')
) d
where minshort <> maxshort;