如何在select语句中添加新列



我想在选择语句中添加新列

SELECT name,line,style,operation,
7to8am,8to9am,9to10am,10to11am,11to12am,
1to2pm,2to3pm,3to4pm,4to5pm,5to6pm,6to7pm,7to8pm,8to9pm,9to10pm,10to11pm,11to12pm,
sum(7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm
+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+
8to9pm+9to10pm+10to11pm+11to12pm) as DailyTotal,id from new_hourly GROUP By line

我想添加一个新的列,显示今天是的DailyTotal的总和

这是我的sql备份文件http://www.uploadmb.com/dw.php?id=1446536983

请帮帮我!非常感谢!

所以您想要一个额外的列,只添加datee等于当前日期的行?

在这种情况下,当表达式为时,我会选择case

coalesce(
      sum(
        case 
          when datee = CURDATE() 
             then 7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+8to9pm+9to10pm+10to11pm+11to12pm 
             else null 
          end
         )
    ,0) as TodaysTotal

这只对列dateecurdate()的那些行求和,如果今天根本不存在行,则返回0。

完整SQL:

SELECT name,line,style,operation,
7to8am,8to9am,9to10am,10to11am,11to12am,
1to2pm,2to3pm,3to4pm,4to5pm,5to6pm,6to7pm,7to8pm,8to9pm,9to10pm,10to11pm,11to12pm,
sum(7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm
+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+
8to9pm+9to10pm+10to11pm+11to12pm) as DailyTotal,
coalesce(
  sum(
    case 
      when datee = CURDATE() 
         then 7to8am+8to9am+9to10am+10to11am+11to12am+1to2pm+2to3pm+3to4pm+4to5pm+5to6pm+6to7pm+7to8pm+8to9pm+9to10pm+10to11pm+11to12pm 
         else null 
      end
     )
,0) as TodaysTotal
,id from new_hourly GROUP By line

相关内容

  • 没有找到相关文章

最新更新