Sql 组内排序表连接另一个表使用first func
首先,我使用window func按Sql 组内排序表连接另一个表使用first func,sql,apache-spark,pyspark,Sql,Apache Spark,Pyspark,首先,我使用window func按charge\u time对我的表t1排序,并将t1与t2连接在用户id上。 如果t1有多条记录,我想获取第一条记录。 我使用firstfunc来实现这一点 _df = ss.sql(""" SELECT t1.user_id, t1.pay_id, t1.sku_mo
charge\u time
对我的表t1
排序,并将t1
与t2
连接在用户id
上。
如果t1
有多条记录,我想获取第一条记录。
我使用first
func来实现这一点
_df = ss.sql("""
SELECT
t1.user_id,
t1.pay_id,
t1.sku_mode,
t1.charge_time,
t1.exchange_type_t01,
ROW_NUMBER() OVER(PARTITION BY t1.user_id ORDER BY t1.charge_time)
FROM
{} t1
WHERE
t1.refund_state = 0
""".format(exchange_info_table))
_df.createOrReplaceTempView('d_exchange_info')
df = ss.sql("""
SELECT
first(t1.sku_mode) AS sku_mode,
first(t1.exchange_type_t01) AS exchange_type_t01,
first(t1.user_id) AS user_id,
first(t1.pay_id) AS pay_id,
first(t1.charge_time) AS charge_time,
first(t2.has_yxs_payment) AS has_yxs_payment,
first(t2.has_sxy_payment) AS has_sxy_payment,
first(t2.has_cxy_payment) AS has_cxy_payment,
first(t2.has_sxy19_payment) AS has_sxy19_payment,
first(t2.sxy19_join_time) AS sxy19_join_time,
first(t2.yxs_join_time) AS yxs_join_time
FROM
d_exchange_info t1
JOIN
analytics_db.md_day_dump_users t2
ON
t2.the_day = '{}'
AND t1.user_id = t2.user_id
GROUP BY
t1.user_id
""".format(st))
我首先使用func
func,但我将通过对charge\u time记录进行排序得到一个不稳定的记录。
如果记录不止一个,有时我得到一个,有时我得到另一个
为什么会发生以及如何解决?
这是sparksql问题还是我的sql有问题
PS:我已经知道如何用另一种方法修复它,但我想知道为什么first func无效
谢谢 我对spark了解不多,但从文档中可以看出:
The function is non-deterministic because its results depends on order of rows
which may be non-deterministic after a shuffle.
您的窗口函数似乎正在生成一个行号,但您没有在任何地方使用它
您需要对结果集进行排序,或者如果您打算使用生成的行号,则添加
,其中row_number=1
。您还必须为行编号列命名,除非spark明确指定。是。正是我用这种方法来修复它。你在哪里找到的这个文件?我刚刚发现一个func doc,如“”聚合函数:返回组中的第一个值。默认情况下,该函数返回它看到的第一个值。当ignoreNulls设置为true时,它将返回它看到的第一个非null值。如果所有值都为null,则spark doc将返回null。doc是标准的apache doc页面。这是链接: