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)
Related
So i have a table named "log" with the following columns,
id, endpoint ,response ,group
SAMPLE DATA.
1. endpoint1 ,{"last_name":"data here"} ,1234
2. endpoint2 ,{"first_name":"data here"} ,1234
3. endpoint3 ,{"dob":"12-21-2301"} ,1234
what I want to achieve is to write a query that can generate a record grouped by the "
group" column and the final output should be something like this.
{"last_name","data here","first_name":"data here","dob":"12-21-2301"}
for each record with each key been a column.
Thank you
WITH RECURSIVE
cte1 AS ( SELECT response,
`group`,
ROW_NUMBER() OVER (PARTITION BY `group`) rn
FROM log ),
cte2 AS ( SELECT response,
`group`,
rn
FROM cte1
WHERE rn = 1
UNION ALL
SELECT JSON_MERGE_PRESERVE(cte1.response, cte2.response),
cte1.`group`,
cte1.rn
FROM cte2
JOIN cte1 USING (`group`)
WHERE cte2.rn + 1 = cte1.rn )
SELECT DISTINCT
FIRST_VALUE(response) OVER (PARTITION BY `group` ORDER BY rn DESC) responses,
`group`
FROM cte2;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=913b1923d7d5dbc7e42baeefb6e6ec86
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;
Query stopped working (getting the user's position in the leaderboard):
SELECT
`rank`, `uid`, `battleWinScore`
FROM
(SELECT
#rank:=#rank+1 AS `rank`, `uid`, `battleWinScore`
FROM
`rating`, (SELECT #rank := 0) r
ORDER BY `battleWinScore` DESC
) t
WHERE uid = 572;
In the rating table, we need to get the user's position by field battleWinScore.
I am absolutely not good at mysql. Help =)
If you are running MySQL 8.0, just use row_number() (or rank(), if you want to consistently allow ties).
select *
from (
select uid, battleWinScore, rank() over(order by battleWinScore desc) rn
from rating
) r
where uid = 572
In earlier versions, I would recommend a correlated subquery rather than user variables:
select uid, battleWinScore
(select count(*) + 1 from rating r1 where r1.battleWinScore > r.battleWinScore) as rn
from rating r
where uid = 572
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;
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