In my case, I try to use this code for pa
boardList = DjangoBoard.objects.raw('SELECT Z.* FROM(SELECT X.*, ceil( rownum / %s ) as page FROM ( SELECT ID,SUBJECT,NAME, CREATED_DATE, MAIL,MEMO,HITS \
FROM web_DJANGOBOARD ORDER BY ID DESC ) X ) Z WHERE page = %s', [rowsPerPage, current_page])
But, It's Oracle's code, Not for mysql. So, rownum has error. How can I do?
Related
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;
I am trying to figure out where I am going wrong here. This has worked for me in the past, but that may because I was using MariaDB and now this is mySQL 8. I am trying to pagination with rank. Is this SQL no longer valid on mySQL 8? Is there a better way to do this?
SELECT
*
FROM
(
SELECT
(#row_number:=#row_number + 1) as 'rank',
a.*
FROM
(
SELECT
deals.id
FROM
deals
) AS a
ORDER BY
utc_created DESC
) AS x,
( SELECT #row := 0 ) AS r
WHERE
1 = 1
AND rank >= 1
AND rank < 51
ORDER BY
rank ASC
rank is a reserved word, and i think your table r is in the wrong position
SELECT
*
FROM
(
SELECT
#rownum := #rownum + 1 'rank',
a.*
FROM
(
SELECT
foo.id
FROM
foo
) AS a ,
( SELECT #rownum := 0 ) AS r
ORDER BY
utc_created DESC
) AS x
WHERE
1 = 1
AND `rank` >= 1
AND `rank` < 51
ORDER BY
`rank` ASC
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)
I am converting my sql queries to mysql queries, but all the queries which have WITH clauses are failing.
Here is the query:
WITH CTE AS (
SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume,
ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice, ind.NVolume AS IndVolume,
rownum = ROW_NUMBER() OVER (PARTITION BY sto.NStockName ORDER BY sto.NDate)
FROM equitystockoptions_nse AS sto
JOIN equitystockoptions_indices AS ind ON sto.NDate = ind.NDate
WHERE ind.NStockName='NIFTY' AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) >= DATE_FORMAT(v_fromdate,'%m/%d/%Y') AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) <= DATE_FORMAT(v_todate,'%m/%d/%Y')
)
SELECT curr.NDate,curr.StkName,curr.StkClosingPrice, curr.StkVolume, curr.IndName, curr.IndClosingPrice, curr.IndVolume,
LOG(curr.StkClosingPrice / prev.StkClosingPrice) AS StkReturnOnLog, LOG(curr.IndClosingPrice / prev.IndClosingPrice) AS IndReturnOnLog
FROM CTE curr
INNER JOIN CTE PREV ON prev.rownum = curr.rownum - 1 AND curr.StkName = prev.StkName
ORDER BY StkName, NDate;
Any suggestion on this conversion is appreciated.
WITH lets assign a name to an inline view (a subquery used in the FROM clause). As I know MySQL doesn't support WITH, so you can just replace all "CTE" references with the actual subquery:
SELECT curr.NDate,curr.StkName,curr.StkClosingPrice, curr.StkVolume,
curr.IndName, curr.IndClosingPrice, curr.IndVolume,
LOG(curr.StkClosingPrice / prev.StkClosingPrice) AS StkReturnOnLog,
LOG(curr.IndClosingPrice / prev.IndClosingPrice) AS IndReturnOnLog
FROM (
SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume,
ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice, ind.NVolume AS IndVolume,
rownum = ROW_NUMBER() OVER (PARTITION BY sto.NStockName ORDER BY sto.NDate)
FROM equitystockoptions_nse AS sto
JOIN equitystockoptions_indices AS ind ON sto.NDate = ind.NDate
WHERE ind.NStockName='NIFTY' AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) >= DATE_FORMAT(v_fromdate,'%m/%d/%Y') AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) <= DATE_FORMAT(v_todate,'%m/%d/%Y')
) curr
INNER JOIN (
SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume,
ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice, ind.NVolume AS IndVolume,
rownum = ROW_NUMBER() OVER (PARTITION BY sto.NStockName ORDER BY sto.NDate)
FROM equitystockoptions_nse AS sto
JOIN equitystockoptions_indices AS ind ON sto.NDate = ind.NDate
WHERE ind.NStockName='NIFTY' AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) >= DATE_FORMAT(v_fromdate,'%m/%d/%Y') AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) <= DATE_FORMAT(v_todate,'%m/%d/%Y')
) PREV ON prev.rownum = curr.rownum - 1 AND curr.StkName = prev.StkName
ORDER BY StkName, NDate;
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