Conversion teradata to mysql aurora without analytical function - mysql

I have mysql aurora 5.6 version so can't use analytical function. I am
not getting any fruitful comment from anywhere. Please help to fix the
query
Teradata query:
select * from xyz qualify row_number() over (partition by source_job_id, run_date, source_job_id, source_parent_job_id order by source_job_id, run_date, source_job_id, source_parent_job_id)=1
MYSQL Query ( Partially wrong as I don't know how to use multiple partition column)
SELECT T.*, RowNum
FROM(
SELECT T.*,
IF(#last_source_job_id = source_job_id, #I:=#I+1, #I:=1) RowNum,
#last_source_job_id := source_job_id
FROM xyz T,(SELECT #last_source_job_id:=NULL, #I:=0)I
ORDER BY source_job_id, run_date, source_job_id, source_parent_job_id
)T where RowNum=1;
**
Updated Query
**
SELECT T.*, RowNum
FROM(
SELECT T.*,
IF(#last_source_job_id = source_job_id and #last_run_date=run_date and #last_source_parent_job_id=source_parent_job_id, #I:=#I+1, #I:=1) RowNum,
#last_source_job_id := source_job_id and #last_run_date:=run_date and #last_source_parent_job_id:=source_parent_job_id
FROM xyz T
ORDER BY source_job_id, run_date, source_job_id, source_parent_job_id
)T where RowNum=1;

Related

MySQL get 2% of the record

I am trying to get 2% of the random sample record.
SELECT * FROM Orders
ORDER BY RAND()
LIMIT (SELECT CEIL(0.02 * (SELECT COUNT(*) FROM Orders)));
This one gives a syntax error due to line 3. Is there anything I am doing wrong?
Or is there a better way to get n % of records?
If you are using MySQL 8+, then ROW_NUMBER() provides one option:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY RAND()) rn,
COUNT(*) OVER () cnt
FROM Orders
)
SELECT *
FROM cte
WHERE 1.0*rn / cnt <= 0.02;
On MySQL 5.7 and earlier, we can simulate row number:
SELECT *
FROM
(
SELECT *, (#rn := #rn + 1) AS rn
FROM Orders, (SELECT #rn := 0) AS x
ORDER BY RAND()
) t
CROSS JOIN (SELECT COUNT(*) AS cnt FROM Orders) o
WHERE 1.0*rn / cnt <= 0.02;

mysql sql error when using the row_number

This query is throwing error, it seems correct but i am getting an error
WITH Rows AS (SELECT *,
ROW_NUMBER() OVER (ORDER BY userid asc) as [Row]
FROM users
WHERE 1=1
)
SELECT * FROM Rows
WHERE Row >= 1 and Row <= 10
error i am getting while running the above statement is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Rows AS (SELECT *, ROW_NUMBER() OVER (ORDER BY userid asc)
FROM users
' at line 1
Your error is on Rows because MySQL does not recognize the CTE.
Then, MySQL also does not recognize [, so you want to use a more reasonable name. In MySQL 8+, that would be something like:
WITH Rows AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY userid asc) as seqnum
FROM users
)
SELECT *
FROM Rows
WHERE seqnum <= 10;
You don't need to compare to 1. That is definitional.
Of course, this, in turn, is overkill. The CTE is unnecessary:
SELECT *,
ROW_NUMBER() OVER (ORDER BY userid asc) as seqnum
FROM users
ORDER BY userid
LIMIT 10;
But this will still fail, because MySQL versions started recognizing CTEs and window functions in the same version.
So, you can just use variables:
SELECT u.*, (#rn := #rn + 1) as seqnum
FROM users u CROSS JOIN
(SELECT #rn := 0) params
ORDER BY userid
LIMIT 10;

MySQL behaves differently on 3 servers, same version

I've got this query running on two different fiddle sites, both set to use MySQL 5.6:
SELECT name, rank, position FROM(
SELECT name, position,
#rank:= IF(#prev = name, #rank + 1, 1) AS rank,
#prev:= name
FROM (SELECT * FROM drivers
LEFT JOIN results on drivers.id = results.driver_id
JOIN (SELECT #rank := 1) AS init
ORDER BY name, results.position ASC) AS temp
) AS derived WHERE rank <= 3 ORDER BY name, rank
It's supposed to give the top 3 finishing positions of each driver. The query works on fiddle #1, but not on fiddle #2 or the production server, although all three of them are running on MySQL 5.6.
Is there a setting that I'm missing?
Fiddle #1 - working
Fiddle #2 - not working
You can use the following solution:
SELECT name, rank, position FROM (
SELECT name, position,
#rank:= IF(#prev = name, #rank + 1, 1) AS rank,
#prev:= name
FROM (
SELECT *
FROM drivers LEFT JOIN results ON drivers.id = results.driver_id
JOIN (SELECT #rank := 1) AS init_rank
JOIN (SELECT #prev := '') AS init_prev
ORDER BY name, results.position ASC
) AS temp
) AS derived
WHERE rank <= 3
ORDER BY name, rank
I added the initialization for the #prev variable on a JOIN too.
different demos:
demo on db-fiddle.com
demo on sqlfiddle.com
demo on dbfiddle.uk
Since MySQL 8.0 you can use the built-in RANK window function. So you don't need the #prev or #rank variables:
SELECT name, `rank`, position FROM (
SELECT name, position, RANK() OVER (PARTITION BY name ORDER BY name, position) AS `rank`
FROM drivers LEFT JOIN results ON drivers.id = results.driver_id
) AS derived
WHERE `rank` <= 3
ORDER BY name, `rank`
demo on dbfiddle.uk

missing parenthesis error in Mysql server

I am using the following code in MySQL server to get entries between 2 to 5 from table 'new_table' but I am receiving 'missing parenthesis' error, I have checked every aspect.
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY salary ASC) AS row number
FROM pact
) as temp table name
WHERE rownumber IN (2,5)
if your using a MySql-Server there is nothing like ROW_NUMBER()
but you could try something like this (UNTESTET):
SELECT * FROM (
SELECT
#num := if(#type = type, #num + 1, 1) as rownumber
FROM pact
ORDER BY salary ASC
) as temp_table_name
WHERE rownumber IN (2,5)
You can't use space in alias
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY salary ASC) AS rownumber
FROM pact
) as temp_table_name
WHERE rownumber IN (2,5)

Translate MySQL to Postgres

I am looking for help converting my MySQL query to work in PostgreSQL. This is a query on the model of RoR application.
Any help is appreciated.
SELECT * FROM(
SELECT #row := #row + 1 AS rownum, id, device_id, name, quarterly
FROM (SELECT #row :=0) r, recurrent_tests
WHERE device_id = "+self.id.to_s+" AND quarterly = 1
ORDER BY name ASC
) ranked
WHERE (rownum-1) % 4 = "+(i-1).to_s)
select *
from (
select row_number() over (order by name asc) as rownum,
id,
device_id,
name,
quarterly
from recurrent_tests
) t
where rownum - 1 % 4 = ...
For more details on window functions (the over (...) clause) please see the manual:
http://www.postgresql.org/docs/current/static/tutorial-window.html