我的查询
Declare @From DateTime='01 Feb 2013'
Declare @To DateTime='28 Feb 2013 23:59:59'
Select
Case
When
(
Select TOP 1 f1.UpdatedOn
From TicketTypeFollowUp As f1 with(nolock)
Where f1.UpdatedOn<T.UpdatedOn And f1.Ticket=T.Ticket
Order By f1.UpdatedOn Desc
)IS Null
Then Ticket.TicketRaisedOn
Else
(
Select TOP 1 f1.UpdatedOn
From TicketTypeFollowUp As f1 with(nolock)
Where f1.UpdatedOn<T.UpdatedOn And f1.Ticket=T.Ticket
Order By f1.UpdatedOn Desc
) End [Start Date]
From dbo.TicketTypeFollowUp T with(nolock)
--Some Tables Omitted
Where CAST(TicketRaisedOn As Date)Between ''+Convert(VarChar(19), @From, 100)+'' And ''+Convert(VarChar(19), @To, 100)+''
如果 Column[Start Date]
的值大于@To
Then @To
Value 应该打印 Else [Start Date] Column
我怎样才能在同一个查询中写它?
我试过的
Case When(
Case
When
(
Select TOP 1 f1.UpdatedOn
From TicketTypeFollowUp As f1 with(nolock)
Where f1.UpdatedOn<T.UpdatedOn And f1.Ticket=T.Ticket
Order By f1.UpdatedOn Desc
)IS Null
Then Ticket.TicketRaisedOn
Else
(
Select TOP 1 f1.UpdatedOn
From TicketTypeFollowUp As f1 with(nolock)
Where f1.UpdatedOn<T.UpdatedOn And f1.Ticket=T.Ticket
Order By f1.UpdatedOn Desc
) End [Start Date]
)> @To Then @To Else [Start Date] End,
数据库:SQL 服务器 2008