【问题标题】:What is the sql equivalent function of the python function .size()?python函数.size()的sql等效函数是什么?
【发布时间】:2021-06-08 14:33:12
【问题描述】:

我正在尝试解决 bigquery 的问题;连续6个月交易的客户名单。我已经用python解决了它,但我不知道如何在sql上复制代码。这是代码

df.groupby(['Month','accounttoken'])['transactionid'].value_counts()
a=df[df.groupby(['Month','accounttoken'])['transactionid'].transform('count')>=5]
df_grouped = a.groupby(['Month', 'accounttoken','Name']).size().reset_index(name='num_transactions')
a1 = df_grouped[df_grouped['num_transactions']>=5]

这就是我到目前为止用 sql 所做的事情

select Month, Name,accounttoken,count(transactionid) no_of_trans from data
group by Month, accounttoken,Name
having count(transactionid)>=5

我认为我需要的是 .size() 函数的等价物

【问题讨论】:

    标签: python pandas google-bigquery


    【解决方案1】:

    count(*) 正在计算组中的行数。

    SELECT count(*) as num_transactions
    FROM data
    GROUP BY Month, accounttoken, name
    HAVING count(*) >= 5
    

    您可以使用这些 SQL 查询来替换您给出的最后两行 Python 代码。我希望你给出的 SQL 查询也能正常工作。

    【讨论】:

      猜你喜欢
      • 2016-12-09
      • 2015-10-04
      • 2015-09-14
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      相关资源
      最近更新 更多