我有三列“操作日期”,“文本”,“执行日期”。
我要获取“最小操作日期”,“最小操作日期”和“最大演奏日期”输入的文本。 详细信息如下:
Create Table Performance (
PerformanceId int identity primary key,
ActionDate datetime,
Notes varchar(500),
PerformanceDate datetime,
jobId int
)
我希望将动作日期设为2018-10-16 09:59:00.000,将文本作为作业保留,执行日期为2018-10-26。
Id ActionDate Notes PerformanceDate JobId
1 2018-10-16 Job to be on hold 2018-10-26 10
2 2018-10-26 Job to be Released 2018-10-16 10
3 2018-10-05 Job hold back 2018-10-11 10
针对一项工作完成了多个Performance条目。我想显示一个摘要报告,显示完成的第一项输入是最小Actin日期,而不是与该日期关联的,以及输入的最大PerformanceDate
答案 0 :(得分:0)
首先选择以下两个日期:
SELECT min(action_date) From table_name
UNION
SELECT max(performance_date) From table_name
然后获取这两个文本:
SELECT text, action_date From table_name
WHERE action_date IN (
SELECT min(action_date) From table_name
UNION
SELECT max(performance_date) From table_name
)
答案 1 :(得分:0)
如果不想使用子查询,可以使用窗口函数:
select distinct jobid,
min(action_date) over (partition by jobid),
max(performance_date) over (partition by jobid),
first_value(notes) over (partition by jobid order by action_date)
from t
where jobid = 10;
另一种方法是:
select top (1) t.*,
max(performance_date) over (partition by jobid)
from t
where jobid = 10
order by action_date;