I have table containing data for users, I need to get only last 2 users after first user ex
id-username
1-john
2-fredrek
3-sara
4-sarah
I need to get fredrek, sara - how do I do this in SQL Server ??
I know to do that with MySQL I use LIMIT1,2 but with SQL Server I can't
To avoid 2 x TOP you can use ROW_NUMBER (note: you can't use ROW_NUMBER output directly in the WHERE clause of a single statement )
;WITH cRN AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY id) AS rn,
username
FROM
mytable
)
SELECT username
FROM cRN
WHERE rn BETWEEN 2 AND 3
You can use TOP, restricting in the WEHRE clause, not to take the first register
SELECT TOP 2 *
FROM MyTable
WHERE Id > 1
With SQL Server, you can use the TOP command.
You have to make a dual query:
select * from ( select top 10 * from (
select top 20 * from table_name order by 1 asc
) as tb_name order by 1 desc
) as tb_name_last
order by whathever;
You can use TOP in T-SQL:
SELECT TOP 2 FROM table_name
Related
I have a table in SQL Server, in this table I have a column chq_no_start with some duplicate cheque no, but the account id is different according those cheque no.
Now I want to show only one cheque no from those duplicate cheque no, no matter if one account id is missing.
I show my output:
Try this...
Select
distinct chq_no_start,
(select top 1 alt_acc_id from Table_name where chq_no_start= tn.chq_no_start order by alt_acc_id) Account_ID
From
Table_name tn
Order by
chq_no_start
Try using the DISTINCT keyword before the column name.
You could try the following:
with cte as (
select alt_acc_id, issue_date, no_of_chq, chq_no_start,
row_number() over (partition by chq_no_start order by alt_acc_id) RowNum
from your_table
where your_column = 'your_value')
select * from cte
where RowNum = 1
I am using SQL Server 2008. With help of TOP clause in SQL Server I have run following query to display the first 1000 rows from my table Department
Select TOP 1000 *
from Department;
Now I need to display the next 1000 data from the same table. Can anybody help me to find a solution for the this?
You can use the ROW_NUMBER function to get the number of the row, and then take the numbers from X to Y. Below query is assuming that you want the results ordered by ID:
WITH OrderedDepartments AS
(
SELECT ID, Name,
ROW_NUMBER() OVER (ORDER BY ID) AS RowNumber
FROM Department
)
SELECT ID, Name
FROM OrderedDepartments
WHERE RowNumber BETWEEN 1001 AND 2000;
you can try something like:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY DEPARTMENT_NAME) AS rownum
FROM DEPARTMENT
) AS t
WHERE t.rownum BETWEEN 1001 AND 2001
Best way is use "FETCH (Transact-SQL)", more info and sample available in following link,
FETCH (Transact-SQL)
I have a table with the following example format:
ID Name
1 NULL
1 NULL
2 HELLO
3 NULL
3 BYE
My goal is to remove repeated lines with same IDS, but with restrictions.
According to the example, I need to remove a row with ID-1, and the row with ID-3 and with no value (NULL).
I would stick with the table:
ID Name
1 NULL
2 HELLO
3 BYE
How can I do this in sql server? thank you
To just select the data, you can use a simple CTE (common table expression);
WITH cte AS (
SELECT id, name,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY name DESC) rn
FROM myTable
)
SELECT id,name FROM cte WHERE rn=1;
An SQLfiddle to test with.
If you mean to delete the duplicates from the table and not just select the data without updating anything, you could use the same CTE;
WITH cte AS (
SELECT id, name,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY name DESC) rn
FROM myTable
)
DELETE FROM cte WHERE rn<>1;
Another SQLfiddle to test with, and remember to always back up your data before running destructive SQL statements from random people on the Internet.
How to covert mysql query into mssql query?
SELECT name FROM user LIMIT 5, 10
I have known that mssql don't support 'limit'...
But I have to use limit!
How to covert mysql query into SQL Server query?
Try out this
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY name) as row FROM sys.databases
) a WHERE a.row > 5 and a.row <= 10
You can achieve your concept.
select * from
(select name , ROW_NUMBER() over(order by name) rn from user ) a
where rn > 5 and rn<= 15
There is no way to translate this, but there is a bit of workaround here on SO.
Check this out.
Is is possible to do a variation of select top n rows to select top n rows starting at a row other than 0.
My (mobile) app has limited resources and no server side caching available. The maximum rows returned is 100. I get the first 100 by select top 100. I would then like the user to be then able to request rows 101-200 and so on. The database data is static and the the re-query time negligible.
Platform SQL Server 2008
Here's an article which demonstrates such queries using the ROW_NUMBER function.
;With CTETable AS
(
SELECT ROW_NUMBER() OVER (ORDER BY Column_Name DESC) AS ROW_NUM, * FROM TABLENAME WHERE <CONDITION>
)
SELECT Column_List FROM CTETable WHERE ROWN_NUM BETWEEN <StartNum> AND <EndNum>
Use your [startNum] and [EndNum] to be any series you want maybe 123 - 147 ! This will work well !