【问题标题】:Joining distinct counts from another table per group从每个组的另一个表中加入不同的计数
【发布时间】:2021-07-23 15:51:06
【问题描述】:

我有 2 张桌子:

table_1

date    id_1         name           id_2        transaction_id
202116  1            Google         235         ABAF51
202116  1            Google         489         GHH512
202116  1            Google         973         JDDF12
202116  1            Google         1189        HDFTS1
202116  1            Amazon         207         HSDY12
202116  1            Amazon         3329        KFGJD88
202116  1            Amazon         3360        JHTJDS1
202116  1            Facebook       862         SYTAHJ4

table_2

date    id_1        name            id_2
202116  1           Google          22
202116  1           Google          102
202116  1           Google          104
202116  1           Google          196
202116  1           Amazon          228
202116  1           Facebook        230
202116  1           Google          235
202116  1           Google          240

我想有一张这样的桌子:

date    id_1        name            id_2        transactions
202116  1           Google          22          1
202116  1           Google          102         3
202116  1           Google          104         4
202116  1           Google          196         2
202116  1           Amazon          228         3
202116  1           Facebook        230         7
202116  1           Google          235         3
202116  1           Google          240         2

其中transactionstable_1 每组date, id, name, id_2transaction_idDISTINCT COUNT(映射到table_2 并由date, id, name, id_2 加入)

因此,我们的想法是计算 transaction_idtable_1 的不同值

date    id_1         name           id_2       
202116  1            Google         235 

并将值(假设为 1)分配给 table_2transactions 其中:

date    id_1         name           id_2       transactions
202116  1            Google         235        1

date, id_1, name, id_2 的每个组合以此类推。

我尝试过的:

select jp.date, jp.id_1, jp.name, jp.id_2, count(distinct(transaction_id)) from table_2 jp
left join table_1  using(date, id_1, name, id_2)
group by jp.date,jp.id_1, jp.name, jp.id_2,transaction_id

但它没有给我正确的输出。 我怎样才能达到预期的效果

【问题讨论】:

    标签: postgresql join group-by distinct


    【解决方案1】:

    不知道你的表结构的细节有点难,但你为什么不分两步解决这个问题:

    1. transaction_idtable_1 计数为date, id, name, id_2
    with first_selection as (
    select date, id, name, id_2, count(distinct transaction_id) nr_transactions
    )
    
    1. table_2 加入结果
    select t.date,
     t.id,t.name, 
     t.id_2,
     fs.nr_transactions 
    from table_2 t 
    join first_selection fs 
     on t.date=fs.date
     and t.id = fs.id
     and t.id_2 = fs.id_2
     and t.name = ft.name
    

    完整的查询是

    with first_selection as (
    select date, id, name, id_2, count(distinct transaction_id) nr_transactions
    )
    
    select t.date,
     t.id,t.name, 
     t.id_2,
     fs.nr_transactions 
    from table_2 t 
    join first_selection fs 
     on t.date=fs.date
     and t.id = fs.id
     and t.id_2 = fs.id_2
     and t.name = ft.name
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 2018-12-11
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      相关资源
      最近更新 更多