Equivalent of MSSQL with statement in MYSQL - mysql

is there any equivalent to the following statement in mysql ?
With Tmp1 as (
Select Distinct EmpID, TypeID
From tb_deductionBalance
), Tmp2 as (
Select *,
row_number() OVER ( order by empID /* no employeeID in Tmp1 */) as RowNum
From Tmp1
)
Select * From Tmp2
Where RowNum Between #Start and #End
I have to migrate an mssql database to mysql and there are plenty of such statements which would take much more time to recreate in mysql if it cannot be translated.
Thanks

SELECT DISTINCT EmpID, TypeID
FROM tb_deductionBalance
ORDER BY empID
LIMIT YourStartNumber - 1, YourEndNumber - YourStartNumber
Unfortunately, you can't use variables in LIMIT, so you'll need to use actual integers there. For instance, if you want results from the rows 3 to 10, you'll need to use:
SELECT DISTINCT EmpID, TypeID
FROM tb_deductionBalance
ORDER BY empID
LIMIT 2, 7

Related

order by with union in SQL is not working

Is it possible to order when the data comes from many select and union it together? Such as
In this statement, the vouchers data is not showing in the same sequence as I saved on the database, I also tried it with "ORDER BY v_payments.payment_id ASC" but won't be worked
( SELECT order_id as id, order_date as date, ... , time FROM orders WHERE client_code = '$searchId' AND order_status = 1 AND order_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
UNION
( SELECT vouchers.voucher_id as id, vouchers.payment_date as date, v_payments.account_name as name, ac_balance as oldBalance, v_payments.debit as debitAmount, v_payments.description as descriptions,
vouchers.v_no as v_no, vouchers.v_type as v_type, v_payments.credit as creditAmount, time, zero as tax, zero as freightAmount FROM vouchers INNER JOIN v_payments
ON vouchers.voucher_id = v_payments.voucher_id WHERE v_payments.client_code = '$searchId' AND voucher_status = 1 AND vouchers.payment_date BETWEEN '$start_date' AND '$end_date' ORDER BY v_payments.payment_id ASC , time )
UNION
( SELECT return_id as id, return_date as date, ... , time FROM w_return WHERE client_code = '$searchId' AND w_return_status = 1 AND return_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
Wrap the sub-select queries in the union within a SELECT
SELECT id, name
FROM
(
SELECT id, name FROM fruits
UNION
SELECT id, name FROM vegetables
)
foods
ORDER BY name
If you want the order to only apply to one of the sub-selects, use parentheses as you are doing.
Note that depending on your DB, the syntax may differ here. And if that's the case, you may get better help by specifying what DB server (MySQL, SQL Server, etc.) you are using and any error messages that result.
You need to put the ORDER BY at the end of the statement i.e. you are ordering the final resultset after union-ing the 3 intermediate resultsets
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. See link below:
ORDER BY and LIMIT in Unions
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;

RANK query using PARTITION BY fails in mysql

I tried below query to partition by but which fails with below query, the inner query works
select issueid, task_type, assignee, timeoriginalestimate, CREATED,
dense_rank() over ( partition by issueid order by CREATED desc ) as rank
from(
--- Complex query with p.pname, i.issuenum, cg.issueid, it.pname task_type, i.assignee, i.timeoriginalestimate, cg.CREATED, columns which works fine
)
Exception:
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 '( partition by issueid order by CREATED desc ) as rank
from(
SELECT p.pna' at line 3
Update:
SELECT VERSION(); -- 5.6.27
Although your MySQL version do not support Window function, I am letting this is not the issue. Guess you have a higher version and window function is supported.
Now, in your query you have defined the Alias of a column name to "Rank" which is a reserved keyword for your database and you can not use that as column name.
Hope this below hints will help you-
select
issueid,
task_type,
assignee,
timeoriginalestimate,
CREATED,
dense_rank() over ( partition by issueid order by CREATED desc ) as rn -- change the alias name
from(
-- Your subquery
) A -- Also need to give a Alias name to your sub query
Finally, if you have lower version check this LINK for help to get an idea of creating Row_number or Ranking for MySQL older versions.
In addition, this following sample query will really help you finding solution for different type row_number in mysql-
SET #simple_row_number := 0;
SET #id_wise_row_number := 0;
SET #dense_rank_per_id := 0;
SET #prev := 0;
SELECT *,
#simple_row_number := #simple_row_number + 1 AS simple_row_number,
#id_wise_row_number := IF(issueid > #prev, #id_wise_row_number + 1, #id_wise_row_number) AS id_wise_row_number,
#dense_rank_per_id :=IF(issueid > #prev,1, #dense_rank_per_id + 1) AS dense_rank_per_id,
#prev := A.issueid Prev_IssueId
FROM (
SELECT 1 issueid, '20200601' CREATED UNION ALL
SELECT 1 issueid, '20200401' CREATED UNION ALL
SELECT 1 issueid, '20200501' CREATED UNION ALL
SELECT 1 issueid, '20200201' CREATED UNION ALL
SELECT 1 issueid, '20200301' CREATED UNION ALL
SELECT 2 issueid, '20200301' CREATED UNION ALL
SELECT 2 issueid, '20200201' CREATED UNION ALL
SELECT 2 issueid, '20200401' CREATED
) A
ORDER BY issueid, CREATED DESC

Turn MySQL query to SQL Server

I have this query in MySQL:
Select *
From Customer
Order By ID DESC Limit 1,1
How to use this query for SQL server ?
For SQL Server 2005 and up:
;WITH cte AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY ID DESC) AS RowNumber
FROM Customer
)
SELECT *
FROM cte
WHERE RowNumber = 2
MSSQL, use this query to fetch Nth record
SELECT * FROM (
SELECT
*, ROW_NUMBER() OVER(ORDER BY userID) AS ROW
FROM tblUser
) AS TMP
WHERE ROW = n
In SQL Server 2012 or higher, you can also use Offset and Fetch:
Select *
From Customer
Order By ID Desc
Offset 1 Rows
Fetch Next 1 Rows Only

How Can I set result set row count using sql query

I wanted to set the number rows returned per query say 5. How can I set this in the sql query.
Can any one please help me
Highly dependent on what RDBMS you are using.
For Oracle
SELECT * FROM the_table WHERE ROWNUM < 6
(since 12c there is another option, too).
For Postgresql
SELECT * FROM the_table LIMIT 5
According to the MySQL manual you can do this by adding LIMIT statement to your query:
SELECT * FROM tbl_name
LIMIT offset, row_numbers
or
SELECT * FROM tbl_name
LIMIT row_numbers OFFSET offset
offset option is very useful in case of pagination.
SELECT TOP 5 *
FROM dbo.MyTable
As someone suggest zou can use:
select top X from table_name
where X is the numer of rows that you want
or you can use row_number
With cte AS
( SELECT *,
ROW_NUMBER() OVER (order by table_column) as RowNumber
FROM table_name)
select *
from cte
Where RowNumber <= 5
or even:
With cte AS
( SELECT *,
ROW_NUMBER() OVER (order by table_column) as RowNumber
FROM table_name)
select *
from cte
Where RowNumber between 5 and 10

how to select rows of a table in 3th multiply order in sql like 3,6,9 etc

My table consists of 1024 rows and two columns.
I want to select and display rows like 3,4,6,etc..
Is it possible in sql query.
If possible what is the code for that..
In a relational database there is no concept of the "3rd" or "6th" row unless you can define a sort order.
Assuming you do have a column by which you can order the result in order to define a row as "3rd", or "4th", the following will work with Postgres:
select *
from (
select col1, col2, col3,
row_number() over (order by your_sort_column) as rn
from your_table
) t
where rn in (3,4,6);
Where your_sort_column is the one that defines the order or rows. This could be an incremental id or a timestamp column that stores the time of insert or update of the row.
In MySQL it can be accomplished as follows:
SELECT * from
(SELECT (#row := #row + 1) num, tab.* FROM your_table, (SELECT #row := 0) r
ORDER by your_sort_column) t
WHERE (num % 3) = 0
We have used a MODULO operator to get row which is at multiple of 3.
See it working at SQL Fiddle: http://www.sqlfiddle.com/#!2/192b0/4
In PostgreSQL it can be accomplished as follows:
select *
from (
select *, row_number() over (order by your_sort_column) as rn
from your_table
) t
where (rn % 3)=0 ;
http://www.sqlfiddle.com/#!15/cc649/5