简体   繁体   English

此汇总查询如何找到正确的值

[英]How does this aggregated query find the correct values

I am having trouble understanding this code. 我在理解此代码时遇到了麻烦。 It actually seems to work, but I don't understand how the correct value for activity year and month is "found" get the proper min and max? 它实际上似乎可行,但是我不明白如何“找到”活动年和月的正确值以获得正确的最小和最大? Or is it running all permutations getting the highest? 还是运行所有排列的最高排列? This is very strange to me. 这对我来说很奇怪。

I do understand how the dateadd works, just not how the query is actually working on the whole. 我确实了解dateadd的工作原理,而不是查询实际上如何整体工作。 This may be a bad question since I don't actually need help solving a problem, just insight into why this works. 这可能是一个不好的问题,因为我实际上并不需要解决问题的帮助,只需了解其工作原理即可。

select 
    EmployeeNumber,
    sum(BaseCalculation) as BaseCalculation,
    min(dateadd(mm, (ActivityYear - 1900) * 12 + ActivityMonth - 1 , 0)) as StartDate,
    max(dateadd(mm, (ActivityYear - 1900) * 12 + ActivityMonth - 1 , 0)) as EndDate
from 
    Compensation 
where 
    1=1
    -- and
group by 
    EmployeeNumber

It is converting the columns ActivityYear and ActivityMmonth to a date. 它将ActivityYearActivityMmonth列转换为日期。 It is doing so by counting the number of months since 1900 and adding them to time zero. 它通过计算自1900年以来的月份数并将其添加到零时间来实现。 So, Jan 2000 would become something like Jan, 100. This seems like a very arcane calculation, because dates that are about 2,000 years old are not really useful. 因此,2000年1月将变成100年1月。这似乎是一个非常不可思议的计算,因为大约有2,000年历史的日期并没有真正的用处。

Of course, this assumes that ActivityYear is a recognizable recent year. 当然,这假定ActivityYear是可识别的最近一年。

I would convert the year and month to the first day of the beginning of the month, with something like this: 我可以将年份和月份转换为该月初的第一天,如下所示:

min(cast(cast(ActivityYear * 10000 + ActivityMonth + 1 as varchar(255)) as date)

Sql Server will calculate every value of that statement, and then only return the min and max. Sql Server将计算该语句的每个值,然后仅返回最小值和最大值。

Although I cannot say for sure that sql server executes this way internally, the way I think about it I imagine that the engine strips off the group by and all the aggregate functions, runs that query. 尽管我不能肯定地说sql server在内部执行这种方式,但是考虑到我的方式,我想象引擎剥离了group by和所有聚合函数,然后运行该查询。 And then just sums/finds the min etc off of that. 然后仅求和/查找最小值。

for both the min and max function call, the algorithm is 对于minmax函数调用,算法为

dateadd(mm, (ActivityYear - 1900) * 12 + ActivityMonth - 1 , 0)

Your query compute all possible date from the Compensation table using this algorithm. 您的查询使用此算法从“ Compensation表计算所有可能的日期。 Then, you select the minimum date as StartDate and the maximum as EndDate . 然后,将最小日期选择为StartDate ,将最大日期选择为EndDate

This is how the proper max and min are returned. 这就是返回正确的最大和最小的方式。

Note that the dateadd signature is DATEADD (datepart , number , date ) 请注意,dateadd签名为DATEADD (datepart , number , date )

Since the last parameter is 0, you are addind to month(mm) the number calculated in the algorithm, and return the corresponding date starting from 0. 由于最后一个参数为0,因此会将算法计算出的数字加到month(mm),然后返回从0开始的相应日期。

Check this out for more information : https://msdn.microsoft.com/en-us/library/ms186819.aspx 请查看此以获取更多信息: https : //msdn.microsoft.com/en-us/library/ms186819.aspx

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

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