您可以使用UNPIVOT
和這個PIVOT
:
select *
from
(
select id, order_type, order_name,
day + '_' + col as col,
val
from
(
select o.id,
o.order_type,
o.order_name,
d.day,
d.start_time,
d.end_time
from orders o
inner join details d
on o.id = d.id
) x
unpivot
(
val
for col in (start_time, end_time)
) u
) x1
pivot
(
min(val)
for col in ([Monday_start_time], [Monday_end_time],
[Tuesday_start_time], [Tuesday_end_time],
[Wednesday_start_time], [Wednesday_end_time],
[Thursday_start_time], [Thursday_end_time],
[Friday_start_time], [Friday_end_time],
[Saturday_start_time], [Saturday_end_time])
) p
如果你不想使用UNPIVOT
和PIVOT
或與D A版本正在OES沒有這些功能,那麼你就需要執行多個聯接:
select o.id,
o.order_type,
o.order_name,
mon.start_time Monday_Start_time,
mon.end_time Monday_End_time,
tues.start_time Tuesday_Start_time,
tues.end_time Tuesday_End_time,
wed.start_time Wednesday_Start_time,
wed.end_time Wednesday_End_time,
thur.start_time Thursday_Start_time,
thur.end_time Thursday_End_time,
fri.start_time Friday_Start_time,
fri.end_time Friday_End_time,
sat.start_time Saturday_Start_time,
sat.end_time Saturday_End_time
from orders o
left join details mon
on o.id = mon.id
and mon.day = 'Monday'
left join details tues
on o.id = tues.id
and tues.day = 'tuesday'
left join details wed
on o.id = wed.id
and wed.day = 'wednesday'
left join details thur
on o.id = thur.id
and thur.day = 'thursday'
left join details fri
on o.id = fri.id
and fri.day = 'friday'
left join details sat
on o.id = sat.id
and sat.day = 'saturday'
加入訂單表7人加入到Details表。 –
如何......應該是查詢 – lax
下面的一個會工作 – Pushpendra