日期:2014-05-18  浏览次数:20506 次

大虾们帮忙看条SQL语句哇
1.SELECT * FROM [Logging].[dbo].[ExceptionLog] 
WHERE (LogId IN(SELECT MIN(LogId) AS Expr1 FROM [Logging].[dbo].[ExceptionLog] 
  AS ExceptionLog_1 GROUP BY StackTrace ) )  
  
2. SELECT * FROM [Logging].[dbo].[ExceptionLog] WHERE 
  (LogId IN(SELECT MIN(LogId) AS Expr1 FROM [Logging].[dbo].[ExceptionLog] 
  AS ExceptionLog_1 where( (CreateDate<'2012-05-17' ) or
  (CreateDate='2012-05-17' and CreateTime<='15:02:21.9070000'))
  GROUP BY StackTrace))
   
3. SELECT * FROM [Logging].[dbo].[ExceptionLog] WHERE 
  (LogId IN(SELECT MIN(LogId) AS Expr1 FROM [Logging].[dbo].[ExceptionLog] 
  AS ExceptionLog_1 where ((CreateDate>'2012-05-17' ) 
or (CreateDate='2012-05-17' and CreateTime>'15:02:21.9070000'))
  GROUP BY StackTrace)) 

为啥1的执行结果不等于2、3两条执行结果的总和呢?

------解决方案--------------------
语句一是根据StackTrace分组取所有数据的最小ID,语句二是根据StackTrace分组,在指定时间范围内取每组最小ID,语句三跟语句二不完全相反,你语句二CreateDate<'2012-05-17',语句三CreateDate>'2012-05-17'
那么你忽略了CreateDate='2012-05-17',明白了吧??
------解决方案--------------------
探讨

语句一是根据StackTrace分组取所有数据的最小ID,语句二是根据StackTrace分组,在指定时间范围内取每组最小ID,语句三跟语句二不完全相反,你语句二CreateDate<'2012-05-17',语句三CreateDate>'2012-05-17'
那么你忽略了CreateDate='2012-05-17',明白了吧??

------解决方案--------------------
你的3句都是根据stacktrace分组,但是通过时间划分的,这就造成了以‘2012-05-17’为分界点语句2和语句3分别根据stacktrace分组取得了最小值,假如对应stacktrace=1时语句2取出的最小值为1,语句3取出的最小值为3,这样对于stacktrace=1的情况值就有2个了,而语句1单纯通过分组取最小值对应每个stacktrace只有一个最小值,所以语句1取出的是2,3取出的子集。