简体   繁体   中英

SQL/BigQuery - How to eliminate duplicates based on two columns

I have a table in BigQuery with the list of items sold together within a sales database:

original_SKU  bought_with  quantity
12345         98765        130
98765         12345        130
abcde         fghij        88
fghij         abcde        88

however you can see that the combinations are repeated... apparently an easy command but I'm having difficulties lol

thanks in advance

Consider below approach

select any_value(struct(original_SKU, bought_with)).*, sum(quantity) quantity
from your_table
group by least(original_SKU, bought_with) || greatest(original_SKU, bought_with)              

if applied to sample data in your question - output is

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM