如何在 select 语句中添加新列
How to add new column in select statement
我想在我的 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-when 表达式:
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
这只是对 datee
列为 curdate()
的那些行求和,如果今天根本没有行,则返回 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
我想在我的 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-when 表达式:
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
这只是对 datee
列为 curdate()
的那些行求和,如果今天根本没有行,则返回 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