I wrote a query to create a table with one column
CREATE TABLE ORDER(
Order_Date DATE NOT NULL
)
I inserted two values into the column
--Order_Date--
2018-05-21
2018-01-21
How to check if the first row is greater or less than the second row using IF statement?
SQL tables represent unordered sets. There is no "first" row or "second" row in a table.
You can do what you want if you include an identity column:
CREATE TABLE ORDERS (
OrderId IDENTITY(1, 1) PRIMARY KEY,
Order_Date DATE NOT NULL
);
Now, you can see if OrderId
and OrderDate
are in the same order. Something like:
select (case when max(case when seqnum = 1 then order_date end) >
max(case when seqnum = 2 then order_date end)
then 'greater than'
when max(case when seqnum = 1 then order_date end) =
max(case when seqnum = 2 then order_date end)
then 'equal'
when max(case when seqnum = 1 then order_date end) <
max(case when seqnum = 2 then order_date end)
then 'less than'
end) as comparison
from (select o.*, row_number() over (order by OrderId) as seqnum
from orders o
) o;
Notice that I also renamed the table to Orders
. Order
is a really bad name for a table because it is a SQL keyword and a reserved word as well.