I want to select a few columns, but only keep the first row grouped by the columns.
If I use
select distinct item_no, item_type, item_name from `table` where item_name!='UNKNOWN'
It can give me two rows for one item_no, if item_type and item_name don’t match in the two rows.
sample output:
item_no item_name item_type 1 table A 1 tables A 2 chair B
How do I make sure I only get the first row grouped by three columns?
Expected output:
item_no item_name item_type 1 table A 2 chair B
I’m using BigQuery so standard SQL
Advertisement
Answer
You can do a group by item_no then get the mininum value of item_name and item_type.
SELECT item_no, MIN(item_name) as item_name, MIN(item_type) as item_type FROM `table` GROUP BY item_no