SQL“按“”分区求和”命令从原始数据获得不同的结果
我真的很纠结于我的求和除以的问题 我有一个名为entity_table的表:SQL“按“”分区求和”命令从原始数据获得不同的结果,sql,sum,window-functions,Sql,Sum,Window Functions,我真的很纠结于我的求和除以的问题 我有一个名为entity_table的表: --------------------------------- entity_id other_attributes... 1 xxxx 2 xxxx 3 xxxx --------------------------------- 和另一个名为score_table的表 -----------------------
---------------------------------
entity_id other_attributes...
1 xxxx
2 xxxx
3 xxxx
---------------------------------
和另一个名为score_table的表
-------------------------------
entity_id score date
1 0 20170101
1 5 20170101
2 5 20170101
2 0 20170101
3 5 20170101
-------------------------------
我的问题如下:
select someother_attributes
, total_score = sum(score) over (partition by entity_id)
, more_other_attributes
from entity_table
left join score_table
on entity_table.entity_id = score_table.entity_id
where date = 20170101
为了确认我从上面的查询中得到了正确的结果,我进行了一些抽样测试。例如,对于实体_id=10:
select * from score_table where date = 20170101
结果是
--------------------------------
entity_id score date
10 0 20170101
10 5 20170101
10 0 20170101
10 0 20170101
10 5 20170101
--------------------------------
所以总分是10分。然后在我的大查询中取相应的实体,我发现总和是30。这显然是错误的
谁能帮我一下吗?非常感谢您的帮助。第一个猜测是,entity_表有三行10。你还应该用你正在使用的数据库标记你的问题。你应该添加一些数据样本,以及预期的结果,只是为了检查为什么不起作用。运行select sumscore from from entity_table left join score_table on entity_id=score_table.entity_id,其中date=20170101,entity_table.entity_id=10,当您运行select sumscore from from entity_table时,您会得到什么结果?首先猜测,entity_table有三行,共有。你还应该用你正在使用的数据库标记你的问题。你应该添加一些数据样本,以及预期的结果,只是为了检查为什么不起作用。当运行select sumscore from entity_table left join score_table时,您会得到什么结果。entity_id=score_table.entity_id,其中date=20170101,entity_table.entity_id=10?