我有一个复杂的SQL查询,我希望根据特定条件操作结果数据。
以下是我的数据结构。它是三个表的组合,它允许存储不同的活动及其注册期。

下面是一个基本查询(更复杂的查询的一部分):
select
act.ID, act.Name, arp.RegistrationPeriodId,
rp.StartDateTime, rp.EndDateTime
from
Activity act
join
ActivityRegistrationPeriod arp on arp.ActivityId = act.ID
join
RegistrationPeriod rp on rp.Id = arp.RegistrationPeriodId我想要实现的
我必须在以下条件下订购这些数据,优先处理。
现在正在注册的
到目前为止我尝试了什么,
我试着创建一个临时表,并为每种情况存储数据(尽管这不正常),但如果可能的话,我正在考虑排序,这就是为什么我把这个问题带到这里来,这样一个经验丰富的人就可以指导我了。
任何帮助都会很感激的。谢谢!
更新查询:
select
act.ID, act.Name, arp.RegistrationPeriodId,
rp.StartDateTime, rp.EndDateTime
from
Activity act
join
ActivityRegistrationPeriod arp on arp.ActivityId = act.ID
join
RegistrationPeriod rp on rp.Id = arp.RegistrationPeriodId
--where
-- act.AccountId = 3106
order by
(case when GETDATE() between StartDateTime and endDateTime then 1 else 2 end),
(case when rp.EndDateTime is null then 2 else 1 end),
rp.EndDateTime asc,
(case when rp.StartDateTime is null then 2 else 1 end),
rp.StartDateTime desc发布于 2021-02-11 13:16:29
您可以使用case表达式:
order by (case when current_date between StartDateTime and endDateTime then 1 else 2 end),
endDateTime asc,
StartDateTime deschttps://stackoverflow.com/questions/66150323
复制相似问题