Event
Range
value
A
0-30
20
A
30-80
70
A
80-100
10
output : A falls under 30-80 Range
We can use the SPLIT() function here along with a cast to integer, on both ends of each range:
SELECT *
FROM yourTable
WHERE value BETWEEN CAST(SPLIT("Range", '-')[offset(0)] AS int64) AND
CAST(SPLIT("Range", '-')[offset(1)] AS int64);
You can use Array_AGG to get the values and use offset. For your requirement, you can try below BigQuery query:
Query :
SELECT AS VALUE ARRAY_AGG(t ORDER BY value DESC LIMIT 1)[OFFSET(0)]
FROM `project.dataset.table` t
GROUP BY event
Output :
Consider below approach
select * from your_table
qualify 1 = row_number() over(partition by event order by value desc)
if applied to sample data in your question - output is
Related
I have one table where multiple records inserted for each group of product. Now, I want to extract (SELECT) only the last entries. For more, see the screenshot. The yellow highlighted records should be return with select query.
The HAVING MAX and HAVING MIN clause for the ANY_VALUE function is now in preview
HAVING MAX and HAVING MIN were just introduced for some aggregate functions - https://cloud.google.com/bigquery/docs/release-notes#February_06_2023
with them query can be very simple - consider below approach
select any_value(t having max datetime).*
from your_table t
group by t.id, t.product
if applied to sample data in your question - output is
You might consider below as well
SELECT *
FROM sample_table
QUALIFY DateTime = MAX(DateTime) OVER (PARTITION BY ID, Product);
If you're more familiar with an aggregate function than a window function, below might be an another option.
SELECT ARRAY_AGG(t ORDER BY DateTime DESC LIMIT 1)[SAFE_OFFSET(0)].*
FROM sample_table t
GROUP BY t.ID, t.Product
Query results
You can use window function to do partition based on key and selecting required based on defining order by field.
For Example:
select * from (
select *,
rank() over (partition by product, order by DateTime Desc) as rank
from `project.dataset.table`)
where rank = 1
You can use this query to select last record of each group:
Select Top(1) * from Tablename group by ID order by DateTime Desc
A column consists of dates only. I need to extract the latest 2 dates from the table. What is the best way to do it?
Example: Values in a table having date column as 01-01-2021, 01-02-2021, 01-03-2021
I would need 01-02-2021,01-03-2021 as my output.
You can use rank() (or row_number() if there are no dupicliates):
select t.*
from t
qualify rank() over (order by datecol desc) <= 2;
Note: If you want distinct values, you could also use:
select distinct datecol
from t
order by datecol desc
limit 2;
You can order the column and then select the top X elements that you require
Select Top 2 col_Date
from MyTable
Order by col_Date DESC
The returned result would be a column
If you need to have a single string, there is a function that can help : STRING_SPLIT
I have a table with the following fields:
Risk|Date|Value
---------------
A|2019-04-23|3
A|2019-04-23|5
A|2019-06-12|4
A|2019-06-12|1
B|2019-05-22|7
B|2019-05-22|5
B|2019-03-13|4
C|2019-01-03|3
I would like to get all the lines that accomplish: its date value is the maximum along all the date values of that specific risk. The output would be:
Risk|Date|Value
---------------
A|2019-06-12|4
A|2019-06-12|1
B|2019-05-22|7
B|2019-05-22|5
C|2019-01-03|3
For the risk A, 2019-06-12 is the max date. Thus, all the lines with that date are sent to the output.
For the risk B, 2019-05-22 is the max date. Thus, all the lines with that date are sent to the output.
For the risk C, 2019-01-03 is the max date. Thus, all the lines with that date are sent to the output.
Any suggestion?
Thank you so much!!
use corelated subquery
select t1.* from tbale t1
where t1.date=(select max(t2.date) from table t2 where t1.risk=t2.riks)
A simple way filters in the where clause:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.risk = t.risk);
Use analytical/windowing function to achieve this. Syntax may vary from database to database, but in Hive, it looks like this:
SELECT
x.risk, x.date, x.value
FROM (
SELECT
risk, date, value,
DENSE_RANK() OVER(PARTITION BY risk ORDER BY date DESC) AS risk_rank
FROM
table_name
) x
WHERE x.risk_rank = 1;
select Risk, date, value
from
(select *, dense_rank() over(partition by risk order by date desc) as K
from Max_date
) as T
where K=1
id value
2 20
1 30
3 15
5 25
I have this table and want to get max value and id. When i use select id,max(value) i've got 2,30 but the right answer is 1,30. I really need to get your attention.
Thank you very much
select id, value from `table` order by value desc limit 1
You can try using subquery
select * from tablename
where value in (select max(value) from tablename)
SELECT *
FROM `tablename`
WHERE value=(SELECT
MAX(value) as value
FROM tablename)
You can check this query. Giving result according your requirement.
SELECT top 1 id,max(value) FROM table
GROUP BY id
ORDER BY max(value) desc
Use subquery
select *
from t
where value = (select max(value) from table)
From the #Thorsten comments i noted that 1st query will return all ties of max value.
Or you can use order by with limit if it is mysql
select *
from table
order by value desc
limit 1
And 2nd query will return only the single row of highest value
I have a column with various dates as timestamps!
"01/17/2014 08:25:13"
"01/17/2014 08:15:11"
"01/17/2014 09:55:12"
"01/17/2014 08:45:01"
...
...
how do I do a query to find the earliest date?
Either:
select min(stamp) from tbl
Or:
select stamp from tbl order by stamp asc limit 1
The first can also be used as a window function, if you need it on an entire set without grouping.
If you need the date in the stamp, cast it:
select min(stamp::date) from tbl
Or:
select stamp::date from tbl order by stamp asc limit 1