【发布时间】: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
其中transactions 是table_1 每组date, id, name, id_2 中transaction_id 的DISTINCT COUNT(映射到table_2 并由date, id, name, id_2 加入)
因此,我们的想法是计算 transaction_id 和 table_1 的不同值
date id_1 name id_2
202116 1 Google 235
并将值(假设为 1)分配给 table_2 列 transactions 其中:
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