简体   繁体   English

SQL Max()返回多个值

[英]SQL Max() Returning multiple values

Good morning/afternoon & thank you for checking on this, I'm struggling to figure out what I'm missing. 早上好/下午,感谢您对此进行检查,我正在努力找出我所缺少的内容。

Attempting to put together a schedule system off the results (separate task) but because our schedules change several times a day/week its creating too many duplicates. 试图将进度表系统放到结果之外(单独的任务),但由于我们的时间表每天/每周更改几次,因此创建了太多重复项。

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

When I run this, I get this Table as expected. 当我运行它时,将按预期方式获得该

I tried adding max () around the 'modified' select to limit it to the latest entry, but that did not change anything from the above. 我尝试在“修改后的”选择周围添加max(),以将其限制为最新条目,但这与上面的内容相同。 I then added 然后我加了

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

to the selects, which did limit it to 1 line per employee however its now taking the opposite entries from what it should per this [table 2][2] 到选择,这确实将其限制为每位员工1行,但是现在从此[表2] [2]中提取的条目与之相反

I tried adjusting it from max to min on the start/end times and it fixed some and broke others. 我尝试在开始/结束时间从最大到最小调整它,它修复了一些问题而破坏了其他问题。

I'm fairly new to SQL and have tried looking at other questions/videos but to no avail. 我对SQL相当陌生,并尝试查看其他问题/视频,但无济于事。 I understand the logic where it's taking the 'max' modified as the most recent time entry, and then max (highest starting / end time) from those 2 cells but in a perfect world I'd prefer only one max value (modified) and then it pairs that to the start & end times. 我了解将“ max”修改为最新时间条目,然后从这两个像元中获取max(最高开始/结束时间)的逻辑,但是在理想情况下,我只希望选择一个max(修改)值,然后将其与开始时间和结束时间配对。

Can anyone guide me in the right direction with what I'm missing to make this work either with 1 max statement, or if using all 3 / a different one entirely? 任何人都可以用1个max语句,或者如果全部使用3个/完全不同的一个,来指导我正确的方向,以使我无法实现这一目标?

Using SQL Management Studio & apologies if I'm missing any other key details, thank you for your help and looking forward to learning more & helping others! 如果我缺少任何其他关键细节,请使用SQL Management Studio和道歉,感谢您的帮助,并希望了解更多信息并帮助他人!

Edit To clarify the challenge I'm having, Updated Image , cells in green are both the latest updated schedule per date whereas the cells in red are what is being captured by the max(start/end time). 编辑为了阐明我所面临的挑战,“ 更新图像”中 ,绿色的单元格都是每个日期的最新更新时间表,而红色的单元格是最大(开始/结束时间)所捕获的内容。

Hope that helps 希望能有所帮助

If you want the max of the value you should perform the query without the group by you have setted for the column in aggregation function (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 

for getting the row related to max(modified) you should instead use a subquery 为了获得与max(modified)相关的行,您应该改用子查询

    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 for get only the employee then 然后只得到员工

    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= '###'
    ;

When you add the max() , you also need to remove the fields from the group by : 添加max() ,还需要group by以下方法从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;

Otherwise, the query returns a separate row for each value of the column -- and the max() will simply be the value in the row. 否则,查询将为该列的每个值返回单独的一行-并且max()将只是该行中的值。

So here's what I got: 所以这就是我得到的:

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

OR if you wanted to separate out by date and modified date, 或者,如果您想按日期和修改日期分开,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM