我研究了这个,但没有发现任何有用的东西。我希望返回多列的最大日期,其中一些列具有空值。我的查询只为每列返回null。我怎样才能获得实际的最长日期?提前谢谢。
create table Results
(
PatientName varchar(50),
Bordetella datetime,
[Canine Distemper] datetime,
Rabies datetime
);
select
PatientName, Bordetella, [Canine Distemper], Rabies
from
(select
PatientName,
ReminderName,
DueDate
from
Results) d
pivot
(
max(DueDate)
for ReminderName in (Bordetella, [Canine Distemper], Rabies)
)piv
;
答案 0 :(得分:0)
这应该是:
create table Results
(
PatientName varchar(50)
,ReminderName varchar(50)
,DueDate datetime
);
select
PatientName
,Bordetella
,[Canine Distemper]
,Rabies
from (
select
PatientName
,ReminderName
,DueDate
from Results
) d pivot (
max(DueDate) for ReminderName in (Bordetella, [Canine Distemper], Rabies)
) pvt