Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用带窗口函数和Case when语句的除法_Sql_Postgresql_Window Functions_Case When - Fatal编程技术网

Sql 如何使用带窗口函数和Case when语句的除法

Sql 如何使用带窗口函数和Case when语句的除法,sql,postgresql,window-functions,case-when,Sql,Postgresql,Window Functions,Case When,它可以与sum()一起工作,但不能与sum()/sum()一起工作 与sum()一起工作: 如果我使用这个查询,它对所有24小时进行求和,这意味着不应用语句时的情况。如何解决这个问题 编辑4: 最后,我使用了这个查询,尽管它很长 (sum(case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59' and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'

它可以与sum()一起工作,但不能与sum()/sum()一起工作

与sum()一起工作:

如果我使用这个查询,它对所有24小时进行求和,这意味着不应用语句时的情况。如何解决这个问题

编辑4: 最后,我使用了这个查询,尽管它很长

 (sum(case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then amount_spent_usd else null end) over () /sum(case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then impressions else null end) over ())*1000 as new
(总和(当日时间/时区>='06:00:00-06:59:59'时的情况)
而时间(每天)(观众)(时区)='06:00:00-06:59:59'
和time\u of_day\u viewers\u time\u zone您不能对两个窗口函数使用
over()
,您需要为每个函数分别提供windowing子句。并且需要对整个表达式进行乘法:

sum(amount_spent_usd) over () / (sum(impressions) over () * 1000)

指定不工作。胡乱猜测:除以零?@Turo即使我使用sum(金额_花费_usd)/nullif(金额(印象),0)*1000,它仍然说“语法错误在或接近“结束””,问了这个问题后我怎么想出来很可笑。“哭吧,笑吧”谢谢。您好,我发现了一个大问题,如果我使用这个查询,它会对所有24小时进行求和,这意味着当语句不是applied@HaoHao…这回答了您在这里提出的问题--关于语法错误。如果您有更详细的问题,请提出一个新问题,并提供适当的示例数据、期望的结果和解释关于你想做的事。
sum(amount_spent_usd) over () / (sum(impressions) over () * 1000)
 (sum(case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then amount_spent_usd else null end) over () /sum(case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then impressions else null end) over ())*1000 as new
sum(amount_spent_usd) over () / (sum(impressions) over () * 1000)