I was trying to select the second last row with SQL Server. So I wrote a query like this:
SELECT TOP 1 * From Cinema
WHERE CinemaID!=(SELECT TOP 1 CinemaID
FROM Cinema
ORDER BY CinemaID DESC)
ORDER BY CinemaID DESC
and it did what I need. But I want to do the same thing with only one select.
I read that the LIMIT clause in MySql does that. But I couldn't find any equivalent
of that. So I appreciate any help about finding something useful.
To get the 2nd last row in one select:
SELECT TOP 1 * From
(select Top 2 * from Cinema ORDER BY CinemaID DESC) x
ORDER BY CinemaID
It's really only "one" select because the outer select is over only 2 rows.
The best way to do this (and compatible with the ANSI SQL standard), is to use a CTE (Common Table Expression) with the ROW_NUMBER function:
;WITH OrderedCinemas AS
(
SELECT
CinemaID, CinemaName,
ROW_NUMBER() OVER(ORDER BY CinemaID DESC) AS 'RowNum'
FROM dbo.Cinema
)
SELECT
CinemaID, CinemaName
FROM OrderedCinemas
WHERE RowNum = 2
By using this construction, you can get the second highest value very easily - or the fifth hightest (WHERE RowNum = 5) or the top 3 rows (WHERE RowNum <= 3) or whatever you need - the CinemaID values are just ordered and sequentially numbered for your use.
The following doesn't work, explaination of why:
Using ranking-function derived column in where clause (SQL Server 2008)
I'll keep it here for completeness:
SELECT row_number() OVER (ORDER BY col) r, *
FROM tbl
WHERE r = 2
More info:
http://www.bidn.com/blogs/marcoadf/bidn-blog/379/ranking-functions-row_number-vs-rank-vs-dense_rank-vs-ntile
So I think the most readable way of doing it is:
SELECT * FROM (SELECT row_number() OVER (ORDER BY col) r, * FROM tbl) q
WHERE r = 2
Since this (old) question has not been tagged with a specific SQL-Server version and none of (the very good) answers uses only one SELECT clause - for the good reason that it was not possible in old verions - here is one that works only in recent, 2012+ versions:
SELECT c.*
FROM dbo.Cinema AS c
ORDER BY CinemaID DESC
OFFSET 1 ROW
FETCH FIRST 1 ROW ONLY ;
Tested at SQLFiddle
SELECT TOP 1 * FROM tbl_CompanyMaster
where Companyid >= (SELECT MAX(Companyid) - 1 FROM tbl_CompanyMaster)
select * from TABLE_NAME order by COLUMN_NAME desc limit 1,1 ;
Where COLUMN_NAME should be "primary key" or "Unique"
Two selects but a bit quicker
select top 1 * from(
SELECT TOP 2 * From Cinema
WHERE CinemaID
ORDER BY CinemaID DESC) top2
Order by CinemaID
So, in the spirit of only using one SELECT clause as stated in the OP and thoroughly abusing T-SQL in general, I proffer something I would never, ever recommend using in production that nevertheless satisfies the stated criteria:
update Cinema
set Cinema.SomeField = Cinema.SomeField
output inserted.*
from Cinema
inner join
(
select top 2 CinemaID, ROW_NUMBER() over (order by CinemaID desc) as RowNum
from Cinema
) rsRowNum on rsRowNum.CinemaID = Cinema.CinemaID
where RowNum = 2
This query will also work for SQLITE
SELECT * From
(select * from Cinema ORDER BY CinemaID DESC LIMIT 2) AS name
ORDER BY CinemaID LIMIT 1
You're only using one SELECT statement. A SELECT statement can include an arbitrary (more or less) number of subqueries--correlated subqueries, scalar subqueries, and so on, each with their own SELECT clause. But it's still just one SELECT statement.
If you want to avoid a subquery, you could select the top 2, and skip the one you don't want. That kind of programming is pretty brittle, though. You have to remember what to skip every time; sooner or later, you'll forget.
SELECT field_name FROM (SELECT TOP 2 field_name FROM table_name
ORDER BY field_name DESC)
WHERE rownum = 2;
select top 1* from(SELECT TOP 2 * From Cinema
WHERE CinemaID
ORDER BY CinemaID DESC) XYZ
ORDER BY CinemaID
where XYZ is not a keyword. It is just a word. And word can be anything.
If you need to do that, but:
the column is different than id
you need to order by some specific column
can't use SELECT on FROM clause (if you are using old versions of Hibernate, per example).
You can do:
select top 1 * from Cinema
where date < (select MAX(date) from Cinema)
order by date desc
The easiest way to get second last row from sql table is user ORDER BY CinemaID DESC and set LIMIT 1,1
TRY THIS
SELECT * from `Cinema` ORDER BY `CinemaID` DESC LIMIT 1,1
select * from
(select ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as R, * from Cinema ) T1
where (select count(*) from Cinema ) - T1.R = 1
SELECT * FROM record
WHERE record_id=(SELECT max(record_id)-1 FROM record)
Here is my code:
SELECT * From
(select * from table name ORDER BY column name DESC LIMIT 2) AS xyz
ORDER BY column name LIMIT 1;
Related
I want to use top clause in where Condition.
I want to do like this:-
SELECT column_name_1,column_name_2,column_name_n
FROM table_name
Where condition and top n.
You could use a ranking function (ROW_NUMBER()/RANK()/DENSE_RANK() depending on your requirements) and a subquery. But I have no idea why you would want to do this. It is more verbose and has no benefits that I can see - consider the following queries, which are equivalent (in terms of results, not necessarily execution plans):
DECLARE #N INT = 5;
SELECT TOP (#N) name
FROM sys.all_objects
ORDER BY object_id;
SELECT name
FROM ( SELECT object_id, name, RowNum = ROW_NUMBER() OVER(ORDER BY object_id)
FROM sys.all_objects
) AS o
WHERE RowNum <= #N;
I know which one I find easier to read
I have a data sample, and now i want to get data using TOP X combine ROW_NUMBER()
IndexNo ProductName
1 Black
2 Blue
3 Brown
4 Green
5 Red
6 White
7 Yellow
As follow in this case, i want to get the data, which after run SQL Statement, result as
IndexNo ProductName
3 Brown
4 Green
5 Red
I use this sql statement for this case, but i get this error Invalid column name 'IndexNo' , this is sql statement .
SELECT TOP 3 ROW_NUMBER() OVER(ORDER BY TEMPA.ProductName) AS IndexNo, TEMPA.ProductName
FROM (
SELECT DISTINCT ProductName FROM PRODUCTS WHERE ProductType ='Food'
) AS TEMPA
WHERE IndexNo between 3 and 5
You could use another level of subquery with parentheses.
SELECT TOP 3 * FROM
( SELECT ROW_NUMBER() OVER(ORDER BY TEMPA.ProductName) AS IndexNo, TEMPA.ProductName
FROM (
SELECT DISTINCT ProductName FROM PRODUCTS
) AS TEMPA
) as TEMPB
WHERE IndexNo between 3 and 5
DEMO
You need to wrap your ROW_NUMBER into Common Table Expression and apply between on the outer level:
with cte as (
SELECT ROW_NUMBER() OVER(ORDER BY TEMPA.ProductName) AS IndexNo, TEMPA.ProductName
FROM (
SELECT DISTINCT ProductName FROM PRODUCTS WHERE ProductType ='Food'
) AS TEMPA
) select top 3 * from cte
WHERE cte.IndexNo between 3 and 5
You need to create the ROW_NUMBER() in one scope and filter it in another scope...
SELECT
*
FROM
(
SELECT *, ROW_NUMBER() OVER (ORDER BY x) AS ix FROM example
)
indexed_example
WHERE
ix BETWEEN 3 AND 5
This is the NOT same for TOP and ORDER BY, as these are applied after the SELECT and WHERE clauses, so this would work fine...
SELECT TOP(3)
*,
ROW_NUMBER() OVER (ORDER BY id DESC) ix
FROM
example
ORDER BY
ix
This is especially useful to your case when using ORDER BY ? OFFSET ? FETCH ? instead of TOP.
SELECT
*,
ROW_NUMBER() OVER (ORDER BY id DESC) ix
FROM
example
ORDER BY
ix DESC
OFFSET 2 ROWS -- Skip 2 rows
FETCH NEXT 3 ROWS ONLY -- Fetch the 3rd, 4th and 5th rows.
In your example, you're also using DISTINCT which is applied after the SELECT values are calculated, but you could use GROUP BY instead as it is applied before the SELECT values are calculated.
SELECT
ROW_NUMBER() OVER (ORDER BY Products.ProductName) ix,
Products.ProductName
FROM
Products
WHERE
Products.ProductType = 'Food'
GROUP BY
Products.ProductName
ORDER BY
ix DESC
OFFSET 2 ROWS
FETCH NEXT 3 ROWS ONLY
All the joins in the FROM clause first (nothing to do in your case)
Apply the WHERE clause
Apply the GROUP BY clause (same effect as your DISTINCT)
Calculate the SELECT values, including the ROW_NUMBER()
Apply the ORDER BY including the OFFSET and FETCH NEXT clauses
Everything you wanted, without needing to next anything in sub-queries.
I have the following table (must shorter version than the real one),
and I want to all the rows with max _ values for each _ displayed.
How should I do this?
Table Now
Table I want to have
thanks a lot in advance!!
Using the dense_rank function and a derived table would be appropriate for this (please note I used underscores instead of spaces in the column names):
select group_type
,desk_number
,comments
from
(select *
,dense_rank() over(partition by group_type order by desk_number desc) dr
from mytable) t1
where t1.dr = 1
I made a rextester sample that you can try here
Let me know if you have any questions.
How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?
This answers your question quite well but I will convert it for your convenience <3
SELECT *
FROM table
INNER JOIN
(SELECT comments, MAX([desk number]) AS MaxDesk
FROM table
GROUP BY comments) groupedtable
ON table.[desk number]= groupedtable.[desk number]
AND table.comments= groupedtable.MaxDesk
try this :
WITH CTE
AS
(
SELECT
SeqNo = ROW_NUMBER() OVER(ORDER BY CAST(DeskNumber AS INT) DESC PARTITION BY GroupType),
GroupType,
DeskNumber,
[Comment]
FROM YourTable
)
SELECT
*
FROM CTE WHERE CTE.SeqNo = 1
There is a table which contains 50 records. I want to select first 10 records without using TOP keyword.
In SQL Server 2012+ you can use OFFSET ... FETCH
SELECT *
FROM YourTable
ORDER BY YourColumn ASC
OFFSET 0 ROWS
FETCH FIRST 10 ROWS ONLY
You can use ROW_NUMBER and Common Table Expression to query any range of data.
USE AdventureWorks2012;
GO
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (ORDER BY OrderDate) AS RowNumber
FROM Sales.SalesOrderHeader
)
SELECT SalesOrderID, OrderDate, RowNumber
FROM OrderedOrders
WHERE RowNumber <= 10 -- other conditions: RowNumber between 50 and 60
Refere ROW NUMBER Here
Although it's probably the same thing internally, you can use
set rowcount 10
and then run the query.
I guess you can try something like this:
SELECT t.Id, t.Name FROM Table t
WHERE 10 > (SELECT count(*) FROM Table t2 WHERE t.id > t2.id)
You can use ROW_NUMBER. Let's say your table contains columns ID and Name. In that case you can use such query:
SELECT t.Id, t.Name
FROM (
SELECT ID, Name,
ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber
FROM TableName
) t
WHERE RowNumber <= 10
How to first filter the result based on params then to apply where-between?
Some thing like
With Results as
(
Select colName,Title, Row_Number(Over...) as row from a table where colName=5
)
Select * from Results
where
row between #first and #last
But it does not works. I need to move my where colName=5 from with clause to outside then I got wrong data as It first get rows between #first n #last then search for colName=5.
Also I want count of Results.
Any idea?
You can use COUNT(*) OVER() to get the count of the unfiltered results
WITH cte as
(
select *,
ROW_NUMBER() over (order by name desc) AS RN,
count(*) over() AS [Count]
from master..spt_values
)
SELECT name, number,[Count]
FROM cte
WHERE RN BETWEEN 20 AND 24
Returns
name number Count
----------------------------------- ----------- -----------
VIEW 8278 2506
VIEW 8278 2506
view 2 2506
varchar 3 2506
varbinary 1 2506
This has performance implications though. You might want to just calculate the COUNT up front and cache it somewhere rather than recalculating it for every page request.
Your ROW_NUMBER syntax is incorrect. It should be this:
With Results as
(
SELECT colName, Title, ROW_NUMBER() OVER (ORDER BY ...) AS RN
FROM your_table
WHERE colName = 5
)
SELECT * FROM Results
WHERE rn BETWEEN #first AND #last
ORDER BY rn
See the documentation for more information.
I use approach very similar to Martin Smiths (currently selected answer) and at least in the tests I've made it gives better performance results.
; WITH cte as
(
select *,
ROW_NUMBER() over (order by name desc) AS RN
from master..spt_values
)
SELECT name, number, (SELECT COUNT(*) FROM cte) AS [Count]
FROM cte
WHERE RN BETWEEN 20 AND 24
Run this and his queries side by side and compare execution plans.