2
根据未取消日期选择第一个活动以及是否取消所有活动以选择第一个活动的最佳方法是什么?Top 1 with where子句 - 如果没有找到记录,则忽略where子句
方案有样本数据:
create table SomeOtherTable
(
Id bigint primary key
)
create table activities
(
Id bigint identity(1,1) primary key,
SomeForeignKey bigint,
Description varchar(100),
Date datetime,
Canceled bit
)
insert into SomeOtherTable values (1),(2),(3)
insert into activities values (1, 'Activity 1', '20141201', 1),
(1, 'Activity 2', '20141203', 0),
(1, 'Activity 3', '20141205', 0),
(2, 'Activity 4', '20141207', 1),
(2, 'Activity 5', '20141209', 1),
(3, 'Activity 6', '20141209', 0)
希望的输出:
活动2 - 2014年12月3日 - 0
活动4 - 十二分之二千零一十四/ 07 - 1
活动6 - 2014/12/0 9 - 0
我目前使用此查询,但我认为必须有一个更好的解决办法...
select case when a1.Id is null then a2.Description else a1.Description end as Description,
case when a1.Id is null then a2.Date else a1.Date end as Date,
case when a1.Id is null then a2.Canceled else a1.Canceled end as Canceled
from SomeOtherTable t
outer apply (select top 1 *
from activities a
where t.id=a.SomeForeignKey
and a.Canceled = 0
order by a.Date) a1
cross apply (select top 1 *
from activities a
where t.id=a.SomeForeignKey
order by a.Date) a2
这实际上是有道理的......我太过于复杂。我也用'row_number'试过了,没有成功。感谢你! – Hatsjoem 2014-11-22 22:37:21