BIGQUERY : value filter with where and condition

David

In BIGQUERY,With this table i try to filter the value to get the team where first, the second and the third at the last stage ...

season |stage      |name              |team     | team_point   |   ranking |
----------------------------------------------------------------------------
2008   |1          |Italy Serie A     |Genoa    |   1          |     3     |
2008   |1          |Italy Serie A     |Milan    |   3          |     1     |
2008   |1          |Italy Serie A     |Udinese  |   3          |     2     |
2008   |1          |Italy Serie A     |Juventus |   0          |     5     |
2008   |1          |Italy Serie A     |Naples   |   1          |     4     |
2008   |2          |Italy Serie A     |Genoa    |   4          |     2     |
2008   |2          |Italy Serie A     |Milan    |   6          |     1     |
2008   |2          |Italy Serie A     |Udinese  |   3          |     3     |
2008   |2          |Italy Serie A     |Udinese  |   3          |     2     |
2008   |2          |Italy Serie A     |Juventus |   0          |     5     |
2008   |2          |Italy Serie A     |Naples   |   1          |     4     |
............................................................................
2008   |38         |Italy Serie A     |Genoa    |   45         |      5    |
2008   |38         |Italy Serie A     |Milan    |   76         |      3    |
2008   |38         |Italy Serie A     |Juventus |   81         |      1    |
2008   |38         |Italy Serie A     |Naples   |   80         |      2    |
............................................................................

i want this output.

    season |stage      |name              |team     | team_point   |   ranking |
    ----------------------------------------------------------------------------
    2008   |1          |Italy Serie A     |Milan    |   3          |     1     |
    2008   |1          |Italy Serie A     |Juventus |   0          |     5     |
    2008   |1          |Italy Serie A     |Naples   |   1          |     4     |
    2008   |2          |Italy Serie A     |Milan    |   6          |     1     |
    2008   |2          |Italy Serie A     |Juventus |   0          |     5     |
    2008   |2          |Italy Serie A     |Naples   |   1          |     4     |
    ............................................................................
    2008   |38         |Italy Serie A     |Milan    |   76         |      3    |
    2008   |38         |Italy Serie A     |Juventus |   81         |      1    |
    2008   |38         |Italy Serie A     |Naples   |   80         |      2    |
    ............................................................................

I try this query with clause "where" but it's not good because the where clause applies to all rows while I would like it to apply to the max(stage)

SELECT
  season,
  stage,
  name,
  team,
  team_point,
  ranking
FROM
  TABLE
WHERE
  ranking <= 3

I think i have to put a condition on the"Where" to choose the ranking at the last stage and keep the values for this teams before the last stage...

99_m4n

Try:

WITH a AS (
  SELECT
    team
  FROM
    TABLE
  ORDER BY stage DESC, ranking
  LIMIT 3
)
SELECT
  season,
  stage,
  name,
  team,
  team_point,
  ranking
FROM
  TABLE
WHERE team in (SELECT team FROM a)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

what is the difference between using filter condition in WHERE clause and JOIN condition

COUNT(*) FILTER (WHERE... In BigQuery

SQL left join with filter in JOIN condition vs filter in WHERE clause

SSRS Performance - Filter property vs WHERE condition

How to filter TOP 1 condition in WHERE clause

Filter a Pandas dataframe by a condition and a minimum value in a column

Can I have a condition inside of a where or a filter?

Update nested record with where condition in Bigquery

How to send value to filter in if condition django?

Use filter in aggregator with condition based on a subdocument value

Where clause with 80% true Condition in BigQuery?

Filter list where nested attributes match condition

BigQuery LAST_VALUE With Condition

Mysql filter query where condition

SQL Filter condition with value or Null

Can we apply filter on measures in where condition?

How do I filter a dataframe for the condition where values are returned if two previous values in the data are less than the third value in the data?

BigQuery filter WHERE by date for last 5 rows for each value of categorical column

Bigquery select records where array column in array value by 'And' condition

BigQuery IF condition then append value into Array - Standard SQL

Bigquery SQL - WHERE IN Col Value

Filter value with multiple condition in Google Sheets

Simple way to filter groups with all values matching a condition in BigQuery

Bigquery - How to filter records with certain condition

How in JS to filter a value on a condition from a map

Filter dataframe with condition where all substrings in a column start with particular value

How to apply 'where' filter to a transformed column in BigQuery?

Mongo Aggregation Query - Filter Condition where string contains value

filter(Importrange) with filter condition on value of cell and not a direct value