mysqlvisual-studiomysql-error-1064mysql-select-db

How to add new column in select statement


i want to add new column in my select statement

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

i want to add a new column that will show sum of DailyTotal that the day is today

This is my sql backup file http://www.uploadmb.com/dw.php?id=1446536983

Please help me! thank you so much!


Solution

  • So you want one additional columns that adds only lines where datee equals current date?

    I'd go for case-when expression in this 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
    

    That's summing up only those rows where column datee is curdate() and returning 0 if no rows at all are present for today.

    Full 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