SQL Max()返回多个值

时间:2016-06-08 16:41:24

标签: sql max

早上好/下午&谢谢你查看这个,我正在努力弄清楚我错过了什么。

尝试将结果系统与结果(单独的任务)放在一起,但由于我们的计划每天/每周更改几次,因此创建了太多的重复项。

Select modified, local_Start_Time, local_End_Time, seg_code, Date, employee

from schedule_table

Where employee= '###' and seg_code = ‘schedule’
and Date in ('20160606', '20160607', '20160608', '20160609') 

group by modified
,local_start_time
,local_end_time
,seg_code
,date
,employee

当我运行时,我按预期得到Table

我尝试在修改过的'周围添加max()。选择将其限制为最新条目,但这并未改变上述内容。然后我添加了

 max(Local_START_Time)
,max(Local_END_time)
,max(modified)

选择,它确实将每个员工限制为1行,但它现在从相应的条目中获取相应的条目[表2] [2]

我尝试在开始/结束时间从最大值调整到最小值,然后修复一些并打破其他人。

我对SQL很新,并尝试查看其他问题/视频,但无济于事。我理解它采用“最大”的逻辑。被修改为最近的时间条目,然后是来自那两个单元格的最大(最高开始/结束时间),但是在完美世界中,我只选择一个最大值(已修改),然后将其与开始和最大值对。结束时间。

任何人都可以用正确的方向指导我,使用1 max语句,或者完全使用所有3 /不同的语句来完成这项工作吗?

使用SQL Management Studio&如果我遗漏了任何其他关键细节,请向您致歉,感谢您的帮助,并期待了解更多信息。帮助他人!

修改为了澄清我所遇到的挑战,Updated Image,绿色单元格是每个日期的最新更新计划,而红色单元格是由最大值(开始/结束时间)。

希望有所帮助

3 个答案:

答案 0 :(得分:1)

如果你想要值的最大值,你应该在没有组的情况下执行查询,你已经为聚合函数(max)中的列设置了

Select max(modified), max(local_Start_Time), max(local_End_Time), seg_code, Date, employee

from schedule_table

Where employee= '###' and seg_code = ‘schedule’
and Date in ('20160606', '20160607', '20160608', '20160609') 

group by seg_code
,date
,employee 

要获取与max(已修改)相关的行,您应该使用子查询

    Select modified, local_Start_Time, local_End_Time, seg_code, Date, employee
    from schedule_table
    where modified = (  select max(modified)
                         from schedule_table
                         Where employee= '###' and seg_code = ‘schedule’
                        and Date in ('20160606', '20160607', '20160608', '20160609') 
    );

然后只获得员工

    Select modified, local_Start_Time, local_End_Time, seg_code, Date, employee
    from schedule_table
    where modified = (  select max(modified)
                         from schedule_table
                         Where employee= '###' and seg_code = ‘schedule’
                        and Date in ('20160606', '20160607', '20160608', '20160609') 
    )
    and employee= '###'
    ;

答案 1 :(得分:0)

添加max()后,您还需要删除group by中的字段:

Select max(modified), max(local_Start_Time), max(local_End_Time),
       seg_code, Date, employee
from schedule_table
where employee= '###' and seg_code = 'schedule' and
      Date in ('20160606', '20160607', '20160608', '20160609') 
group by seg_code, date, employee;

否则,查询会为列的每个值返回一个单独的行 - max()将只是行中的值。

答案 2 :(得分:0)

所以我得到的是:

SELECT DISTINCT 
    [date], 
    employee, 
    max(modified) OVER (PARTITION BY [date]) AS max_modified,
    max(start_time) OVER (PARTITION BY [date]) AS max_start, 
    max(end_time) OVER (PARTITION BY [date]) AS max_end
FROM Table_1
GROUP BY 
    [date], employee, modified, start_time, end_time

如果您想按日期和修改日期分开,

SELECT DISTINCT 
    [date], 
    employee, 
    max(modified) OVER (PARTITION BY [date],employee) AS max_modified, 
    max(start_time) OVER (PARTITION BY [date]) AS max_start, 
    max(end_time) OVER (PARTITION BY [date]) AS max_end
FROM Table_1
GROUP BY [date], employee, modified, start_time, end_time