如何用SQL中类别的平均值替换为空值?

发布于 2025-02-03 06:34:33 字数 2331 浏览 2 评论 0原文

具有

无效一个
数据
​-014273774760
2021-07-03427367483060.0
2021-07-07-03427357471542.62
2021-07-07-07-04 42734 427344273474830nan
42734 -054273474830100.0
2021-07-10427387471550.72
2021-08-12427397483073.43

我希望用“ datemention_id,actionitioner_id,timentioner_id,pertioner_duration_duration_duration_min_min “是一样的。

使用pandas dataframe

df['revenues_from_appointment'].fillna(df.groupby(['patient_id','practitioner_id','appointment_duration_min'])['revenues_from_appointment'].transform('mean'), inplace = True)

通过

最终

约会获得
如何相同结果
SQL输出使用
2021-07-014273774760150.0
2021-07-0342736 427367483060.0
2021-07-07-07-03427357471542.62 2021-07-07-07-07-07-07-07-04
427344273474830 30<强> 95.0 <> 95.0
2021-07-054273474830100.0
2021-07-10427387471550.72
2021-08-12427397483073.43

I have a dataset with null values in the column 'revenues_from_appointment'

Dataset

appointment_datepatient_idpractitioner_idappointment_duration_minrevenues_from_appointment
2021-06-28427347483090.0
2021-06-294273774760150.0
2021-07-014273774760NaN
2021-07-03427367483060.0
2021-07-03427357471542.62
2021-07-044273474830NaN
2021-07-054273474830100.0
2021-07-10427387471550.72
2021-08-12427397483073.43

I wish to replace NULL values by the mean value of rows where "patient_id, practitioner_id, appointment_duration_min" is the same.

I did it using pandas dataframe,

df['revenues_from_appointment'].fillna(df.groupby(['patient_id','practitioner_id','appointment_duration_min'])['revenues_from_appointment'].transform('mean'), inplace = True)

How can we obtain the same result by using SQL?

Final Output

appointment_datepatient_idpractitioner_idappointment_duration_minrevenues_from_appointment
2021-06-28427347483090.0
2021-06-294273774760150.0
2021-07-014273774760150.0
2021-07-03427367483060.0
2021-07-03427357471542.62
2021-07-04427347483095.0
2021-07-054273474830100.0
2021-07-10427387471550.72
2021-08-12427397483073.43

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

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

发布评论

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

评论(1

呢古 2025-02-10 06:34:33

您可以使用avg窗口函数,该函数将在感兴趣的三列上分区,并使用cocecce函数替换null值:

SELECT appointment_date,
       patient_id,
       practitioner_id,
       appointment_duration_min,
       COALESCE(revenues_from_appointment, 
                AVG(revenues_from_appointment) OVER(PARTITION BY patient_id, 
                                                                 practitioner_id, 
                                                                 appointment_duration_min))
FROM tab

尝试在这里

You can use the AVG window function, that will partition on the three column of interest and replace null values using the COALESCE function:

SELECT appointment_date,
       patient_id,
       practitioner_id,
       appointment_duration_min,
       COALESCE(revenues_from_appointment, 
                AVG(revenues_from_appointment) OVER(PARTITION BY patient_id, 
                                                                 practitioner_id, 
                                                                 appointment_duration_min))
FROM tab

Try it here.

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