在sql中的单独列中减去两个日期

时间:2017-03-23 09:29:42

标签: mysql sql

awbno       byhub        entrydatetime           uplopadtime
 111            ho       2017-01-01 10:10:13.000    2017-01-01 10:10:13.000
 222            ho       2017-01-01 18:10:13.00     2017-01-02 10:10:13.000
 333            ho       2017-01-01 13:10:13.000    2017-01-03 10:10:13.000
 444            ho       2017-01-01 10:10:13.000    2017-01-05 10:10:13.000

我想通过从uploaddatetime中减去entrydatetime来输出这样的方式:

byhub   total_awbno   sameday     oneday    twoday    morethan_two_day
 ho         4           1           1          1          1

我使用了此查询但未找到正确的解决方案: -

select byhub , 
       COUNT(awbno),    
       count(case when (datediff(day,uplopadtime,EntryDateTime)=0) then 1 end ),    
       count(case when (datediff(day,uplopadtime,EntryDateTime)=1) then 1 end ),    
       count(case when (datediff(day,uplopadtime,EntryDateTime)=2) then 1 end ),    
       count(case when (datediff(day, uplopadtime,EntryDateTime)>2) then 1 end )    
from MyTable  
group by byhub

我的查询输出是:

ho 4 1 0 0 0

3 个答案:

答案 0 :(得分:1)

MySQL中的

datediff()函数只有2个参数:2个日期值。您要查找的函数是TIMESTAMPDIFF(),如果您想从uploaddatetime中减去entrydatetime,还需要反转参数列表中字段的顺序:

  

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

     

返回datetime_expr2 - datetime_expr1,其中datetime_expr1和   datetime_expr2是日期或日期时间表达式。一个表达可能是   日期和另一个日期时间;日期值被视为日期时间   必要时有时间部分'00:00:00'。单位为   result(整数)由unit参数给出。法律价值观   对于单位与在说明中列出的单位相同   TIMESTAMPADD()函数。

select byhub , 
       COUNT(awbno),    
       count(case when timestampdiff(day,EntryDateTime,uplopadtime)=0 then 1 end ),    
       count(case when timestampdiff(day,EntryDateTime,uplopadtime)=1 then 1 end ),    
       count(case when timestampdiff(day,EntryDateTime,uplopadtime)=) then 1 end ),    
       count(case when timestampdiff(day,EntryDateTime,uplopadtime)>2 then 1 end )    
from MyTable  
group by byhub

timestampdiff(day,EntryDateTime,uplopadtime)datediff(EntryDateTime,uplopadtime)

相同

答案 1 :(得分:0)

试试这个:

SELECT byhub,
       COUNT(awbno) AS total_awbno,
       COUNT(CASE WHEN days_diff = 0 THEN 1 END) AS sameday,
       COUNT(CASE WHEN days_diff = 1 THEN 1 END) AS oneday,
       COUNT(CASE WHEN days_diff = 2 THEN 1 END) AS twoday,
       COUNT(CASE WHEN days_diff > 2 THEN 1 END) AS morethan_two_day
FROM (
   SELECT awbno, byhub, datediff(uplopadtime,EntryDateTime) AS days_diff
   FROM mytable) AS t
GROUP BY byhub;  

Demo here

答案 2 :(得分:-1)

检查Bellow

class CustomNavigationViewController: UINavigationController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
  }

  override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
  }
}