sqlite按均值对每个组均值丢失值

发布于 01-21 12:54 字数 643 浏览 3 评论 0原文

我有一个Sqlite表,如下所示。

学生成绩
尼克34
尼克42
尼克86
·纳尔约翰
尼克38
约翰12
约翰74
约翰·纳尔
·科林87
科林23
科林46
科林42

我想做的是将无效的值与每个学生的成绩的平均值相关。 例如,尼克的缺失价值将为54,而约翰41.3的价值将为54。 如何在SQL代码中执行此操作?我正在使用sqlite。

I have an SQLite table as shown below.

studentsgrades
Nick34
Nick42
Nick86
NickNull
John38
John12
John74
JohnNull
Colin87
Colin23
Colin46
Colin42

What I want to do is impute Null values with the mean of each student's grades.
For example, missing value for Nick will be 54 and for John 41.3.
How can I do this in SQL code? I am using SQLite.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2025-01-28 12:54:23

在更新语句中使用相关的子查询:

UPDATE tablename AS t1
SET grades = (
  SELECT ROUND(AVG(t2.grades), 1) 
  FROM tablename AS t2 
  WHERE t2.students = t1.students
)
WHERE t1.grades IS NULL;

请参阅

Use a correlated subquery in the UPDATE statement:

UPDATE tablename AS t1
SET grades = (
  SELECT ROUND(AVG(t2.grades), 1) 
  FROM tablename AS t2 
  WHERE t2.students = t1.students
)
WHERE t1.grades IS NULL;

See the demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文