Is it possible in sql server to get the lines from row 10 to row 20 for example ?
I mean, I'm getting more than 1800 rows by querying the database and then i would like to display them 10 by 10.
Use a CTE to add a column that represents row number of each row and then filter by that column.
;WITH MyCTE AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY ID) RowNum
FROM Table
)
SELECT *
FROM MyCTE
WHERE RowNum BETWEEN 10 AND 20
Or in SQL 2012+, use OFFSET\FETCH
....
ORDER BY ...
OFFSET 10 ROWS
FETCH NEXT 10 ROWS;
See http://msdn.microsoft.com/en-us/library/ms188385%28v=SQL.110%29.aspx
Example:
WITH ResultSetWithRowNumber AS
(
SELECT ROW_NUMBER() OVER(ORDER BY MyField1) AS RowNumber, MyField1, MyField2
FROM MyTable
WHERE MyField2 = SomeValue
)
SELECT RowNumber, MyField1, MyField2
FROM ResultSetWithRowNumber
WHERE RowNumber BETWEEN 10 AND 20
MSDN
Related
I have a table like below
ID Code Age
----------------
1 4758 21
1 7842 14
1 9821 23
1 6842 9
2 8472 24
2 7558 31
2 7841 28
3 7881 38
3 8794 42
3 4871 43
For each ID, I want to select one of the rows at random like so
ID Code Age
----------------
1 7842 14
2 7841 28
3 4871 43
Is this possible in SQL Server?
select top 1 with ties id,code,age
from
table
order by row_number() over (partition by id order by rand())
Update: as per this Return rows in random order, you have to use NEWId,since RAND() is fixed for the duration of the SELECT on MS SQL Server.
select top 1 with ties id,code,age
from
table
order by row_number() over (partition by id order by NEWID())
Use Newid() in order by clause of Row_number()
SELECT [ID], [Code], [Age]
FROM (SELECT *,
Row_number()
OVER(
PARTITION BY ID
ORDER BY Newid()) RNO
FROM #Table1)A
WHERE RNO = 1
with cte as
(
select *,rank() over ( partition by id order by Newid()) as rn from #c
)
select id,code,age from cte where rn=1
To select different sets each time, use checksum(newid()) in the order by clause.
Query
;with cte as(
select *, rn = row_number() over(
partition by ID
order by abs(checksum(newid())) % 15
)
from [your_table_name]
)
select * from cte
where rn = 1;
I have table like this:
id_Seq_No emp_name Current_Property_value
-----------------------------------------------
1 John 100
2 Peter 200
3 Pollard 50
4 John 500
I want the max record value of particular employee.
For example, John has 2 records seq_no 1, 4. I want 4th seq_no Current_Property_Value in single query.
Select
max(id_Seq_No)
from
t1
where
emp_name = 'John'
To get the Current_Property_value, just order the results by id_Seq_No and get the first one:
SELECT
TOP 1 Current_Property_value
FROM
table
WHERE
emp_name = 'John'
ORDER BY
id_Seq_No DESC
this will give highest for all tied employees
select top 1 with ties
id_Seq_No,emp_name,Current_Property_value
from
table
order by
row_number() over (partition by emp_name order by Current_Property_value desc)
You can use ROW_NUMBER with CTE.
Query
;WITH CTE AS(
SELECT rn = ROW_NUMBER() OVER(
PARTITION BY emp_name
ORDER BY id_Seq_No DESC
), *
FROM your_table_name
WHERE emp_name = 'John'
)
SELECT * FROM CTE
WHERE rn = 1;
I have a table with some 100 records and Total is the one of the colum in the table.I am arranging the rows in the table in the DESC order with the following query.
Select Name,Total from tbl_one group by Toal DESC
Now my requirment is after getting this result I need to give number to each record starting from first. like
John 500 1
Sam 490 2
David 480 3 and till 100
Please suggest me the best way of doing this. I am using SQL Server 2008 R2.
Your query will not work. But this is the method used to select an enumerating column:
SELECT Name, Total, ROW_NUMBER() OVER(ORDER BY Total DESC) AS RN
FROM tbl_one
ORDER BY RN ASC
You can do this with ROW_NUMBER window function:
select Name, Total, Row_Number() Over(Order By Total DESC) as RowIndex
from tbl_one
order by RowIndex
PS: in Sql Server you call it ROW not RECORD.
You could use the row_number window function:
SELECT Name, Total, ROW_NUMBNER() OVER (ORDER BY Total)
FROM tbl_one
ORDER BY Toal DESC
SELECT
Name,
Total,
Index = ROW_NUMBER() OVER(PARTITION BY [Total] ORDER BY [Total])
FROM tbl_one
To retain the grouping
DECLARE #startnum INT=400
DECLARE #endnum INT=1400
;
WITH gen AS (
SELECT #startnum AS num
UNION ALL
SELECT num+10 FROM gen WHERE num + 1<=#endnum
)
SELECT 'Some name',num,ROW_NUMBER()OVER( ORDER BY num DESC) FROM gen order by num desc
option (maxrecursion 10000)
I'm looking for a way to fetch at least 20 rows, regardless of the date or all rows with a date greater than a set date (whichever of the 2 fetches the most rows).
For example:
SELECT *
FROM table1
WHERE the_date >= '2014-11-01'
ORDER BY the_date DESC
will give me what I want, but if this returns less than 20 rows then I want to keep going before that date until I get 20 (or until there are no more rows - whichever comes first).
I could just select all and take the ones I need programatically, but I'm trying to avoid that as that would be a major change to the actual code in many places.
Can this be done?
Note: I am using SQL Server 2008.
Here's an approach:
select *
from
(
select row_number() over (order by the_date desc) num, t.*
from table1 t
) i
where i.the_date >= '2014-11-01'
or i.num < 21
Very strange requirement but something like this should work.
DECLARE #Date date = '2014-11-01';
with MyResults as
(
SELECT *, 1 as RowNum
FROM table1
WHERE (the_date >= #Date )
UNION ALL
SELECT top 10 *, ROW_NUMBER() over(order by the_date desc)
FROM table1
order by the_date desc
)
Select *
from MyResults
where RowNum <= 20
The right answer was almost there, but the user has removed it, so here it is again:
SELECT *
FROM table1
WHERE the_date >= '2014-11-01'
union
SELECT top 20 *
FROM table1
ORDER BY the_date desc
The removed answer had union all
This answer has a problem with duplicate dates , but with the data I have that is not an issue
I have this table:
ID COLOR TYPE DATE
-------------------------------
1 blue A 2012.02.05
2 white V 2010.10.23
3 white V 2014.03.05
4 black S 2013.02.14
I'd like to select only the ID, but in case of 2nd and 3rd rows I want to select the 3rd row because of its latest DATE value.
I have tried this query but it gives back all the two rows:
SELECT
ID, MAX(DATE) OVER(PARTITION BY COLOR, TYPE)
FROM
TABLE
WHERE
...
How can I select just one column value while I group the rows by other columns, please?
;WITH CTE AS
(
SELECT * , ROW_NUMBER() OVER (PARTITION BY COLOR,[TYPE] ORDER BY [DATE] DESC) rn
FROM TableName
)
SELECT ID
,COLOR
,[TYPE]
,[DATE]
FROM CTE
WHERE rn = 1
OR
SELECT ID
,COLOR
,[TYPE]
,[DATE]
FROM
(
SELECT * , ROW_NUMBER() OVER (PARTITION BY COLOR,[TYPE] ORDER BY [DATE] DESC) rn
FROM TableName
) A
WHERE rn = 1