Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery - Select only first row in BigQuery

I have a table with data where in Column A I have groups of repeating Data (one after another).

I want to select only first row of each group based on values in column A only (no other criteria). Mind you, I want all corresponding columns selected also for the mentioned new found row (I don't want to exclude them).

Can someone help me with a proper query.

Here is a sample: SAMPLE

Thanks!

like image 734
zerina Avatar asked Jun 19 '18 08:06

zerina


4 Answers

You can now use qualify for a more concise solution:

select
   *
from
   your_dataset.your_table
where true
qualify ROW_NUMBER() OVER(PARTITION BY columnA ORDER BY columnA) = 1
like image 97
David Masip Avatar answered Oct 16 '22 20:10

David Masip


Add LIMIT 1 at the end of the query

something like

SELECT name, year FROM person_table ORDER BY year LIMIT 1
like image 36
Mahesh Uligade Avatar answered Oct 16 '22 18:10

Mahesh Uligade


#standardSQL
SELECT row.*
FROM (
  SELECT ARRAY_AGG(t LIMIT 1)[OFFSET(0)] row
  FROM `project.dataset.table` t
  GROUP BY columnA
) 
like image 10
Mikhail Berlyant Avatar answered Oct 16 '22 18:10

Mikhail Berlyant


you can try smth like this:

#standardSQL
SELECT
   * EXCEPT(rn)
FROM (
   SELECT
   *,
   ROW_NUMBER() OVER(PARTITION BY columnA ORDER BY columnA) AS rn
FROM
   your_dataset.your_table)
WHERE rn = 1

that will return:

Row   columnA       col2       ...   
1     AC1001        Z_Creation   
2     ACO112BISPIC  QN
...
like image 8
Vasily Bronsky Avatar answered Oct 16 '22 20:10

Vasily Bronsky