I am translating a very large CTE Teradata query and got stuck at this following portion that is its own subquery, which is being cross joined into a much large subquery.
How can I translate this query into Bigquery?
(select row_number() over (order by tablename) subsequent_month from dbc.tables qualify row_number() over (order by tablename) <= 24)
Thoughts guys?
Below is for BigQuery Standard SQL
#standardSQL
SELECT subsequent_month FROM (
SELECT ROW_NUMBER() OVER (ORDER BY tablename) subsequent_month
FROM dbc.tables
) WHERE subsequent_month <= 24
You need a subquery for the qualify:
from (select row_number() over (order by tablename) as subsequent_month
from dbc.tables
) t
where subsequent_month < 24;
In Teradata, qualify is a "where" clause that works on window functions. It is analogous to having which works on aggregation functions.
This simply returns 24 rows with consecutive numbers from 1 to 24.
So translate it to a similar query against a any table in BigQuery or use an existing numbers table.
Related
Can anyone help me to translate Teradata SQL QUALIFY ROW_NUMBER() OVER into Presto:
SELECT *
FROM table1
QUALIFY ROW_NUMBER() OVER(ORDER BY id DESC) > 5000000
AND ROW_NUMBER() OVER(ORDER BY id DESC) <= 10000000;
Or provide some suggestions how to extract large datasets by row filtering.
As far as I understand there is no direct analog for QUALIFY clause in PrestoSQL/Trino. You can just use window function in the WHERE clause. Something like this:
SELECT *
FROM table1
WHERE ROW_NUMBER() OVER(ORDER BY id DESC) BETWEEN 5000001
AND 10000000;
I am attempting to sample 20% of a table in impala. I have heard somewhere that the built in impala sampling function has issues.
Is there a way to pass in a subquery to the impala limit function to sample n percent of the entire table.
I have something like this:
select
* from
table_a
order by rand()
limit
(
select
round( (count(distinct ids)) *.2,0)
from table_a)
)
The sub query gives me 20% of all records
I'm not sure if Impala has specific sampling logic (some databases do). But you can use window functions:
select a.*
from (select a.*,
row_number() over (order by rand()) as seqnum,
count(*) over () as cnt
from table_a
) a
where seqnum <= cnt * 0.2;
Does anyone know if it is possible to do any arithmetic operation on a result derived using GBQ window functions?
For example, can I increase row_number by 100 (some number) using pseudocode like this:
SELECT 100 + ROW_NUMBER() OVER (PARTITION BY X ORDER BY x_id DESC) increased_row_num
FROM Table1
...
You will need to use subquery for that
SELECT 100 + row_num AS increased_row_num FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY X ORDER BY x_id DESC) AS row_num
FROM Table1
)
but I'we hoped that there is another solution
With BigQuery Standard SQL expected functionality works now as is
#standardSQL
SELECT 100 + ROW_NUMBER() OVER (PARTITION BY X ORDER BY x_id DESC) increased_row_num
FROM Table1
See Enabling Standard SQL and Migrating from legacy SQL
I am using Oracle database and I have a table that have 1.9 billion rows of records. I want to get the rows of records ranging from 100,000,001 to 200,000,000. Can someone help me on this? Thank you in advance.
generally speaking you want a pagination query , which would be of the format:
select t.*
from (select t.*, rownum rn
from (select t.yourfields
from yourtab t
order by t.something)
where rownum <= end_rownum
) t
where rn >= offset;
or
select *
from (select t.yourfields, row_number() over (order by t.something) rn
from yourtab t)
where rn between start_rownum and end_rownum;
if you mean those ranges are rownum then here is simple the query
select * from (select e.*,rownum test from hr.employees e) where test>5 and test <50;
I have a SQL query that does some ranking, like this:
SELECT RANK() OVER(PARTITION BY XXX ORDER BY yyy,zzz,oooo) as ranking, *
FROM SomeTable
WHERE ranking = 1 --> this is not possible
I want to use that ranking in a WHERE condition at the end.
Now I nest this query in another query and do filtering on the ranking there, but is there no easier or faster way to filter on such values from the SELECT statement?
Use a CTE (Common Table Expression) - sort of an "inline" view just for the next statement:
;WITH MyCTE AS
(
SELECT
RANK() OVER(PARTITION BY XXX ORDER BY yyy,zzz,oooo) as ranking,
*
FROM SomeTable
)
SELECT *
FROM MyCTE
WHERE ranking = 1 --> this is now possible!
Sorry for the former posting, i forgot : windowing functions can only be used in select or order by clauses.
You'll have to use a sub query:
SELECT * FROM
(
SELECT RANK() OVER(PARTITION BY XXX ORDER BY yyy,zzz,oooo) as ranking, *
FROM SomeTable
) t
WHERE ranking = 1
OR A CTE.
select * from (
select RANK() OVER(PARTITION BY name ORDER BY id) as ranking, *
from PostTypes
) A
where A.ranking = 1
https://data.stackexchange.com/stackoverflow/query/edit/59515