Select the most recent date from the table [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a purchase order table that has a list of order numbers and associated order lines with different dates. I want to a query to fetch the orders with orderlines that has the most recent date.
I want the fetch to result in something like below

Here is a way to do this using row_number
select * from (
select t.*
,row_number() over(partition by ordernumber,orderlinenumber order by date desc) as rnk
from <your_table> t
)x
where x.rnk=1

Related

Tracking Employee Changes - SQL query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 hours ago.
Improve this question
I have this table of employees that gets updated at the end of each month. I know how to detect changes (2 rows; one of old and one of new) but I need to summarize the changes as showing in the second table using sql query.
With JobHistory AS(
Select
*,
LEAD([DepartmentName]) over (Partition by [EmployeeNumber] order by [ASofDate]) as NewDepartmentName,
LAG([DepartmentName]) over (Partition by [EmployeeNumber] order by [ASofDate]) as OldDepartmentName
FROM [EmployeeDirectory]
)
Select *
FROM JobHistory
WHERE (
(NewDepartmentName <> [DepartmentName])
OR
( OldDepartmentName <> [DepartmentName])
)
ORDER by AsOfDate, EmployeeNumber
[Table]
Required Results

How can we display the last 7 Records from table in Oracle 11G SQL only? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How can I write this query without TOP and Limit only use the SQL
You just sort the data one way, grab the records you want, then sort it the other way:
SELECT SOME_FIELD
FROM (SELECT st.*
FROM SOME_TABLE st
ORDER BY SOME_FIELD DESC)
WHERE ROWNUM <= 7
ORDER BY SOME_FIELD ASC
dbfiddle here

To select max date and time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data with column createdate like '24/04/2019 14:52:38',24/04/2019 14:52:37,24/04/2019 14:52:35,24/03/2019 14:52:38 etc.
how to get data based on max date and time in SQL query.
select
top(1) *
from
xx
order by
createdate desc
something like this? (works if createdate column is of date/datetime/timestamp type)
select
*
from
<table_name>
where
createdate = (select
max(createdate)
from
<table_name>)

SQL: How do I list all of customer ID that had their first 2 orders exactly in the sequence of: first order being hat, second order being sunglasses? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am new to SQL and I am trying to figure out the query:
How do I list all of customer ID that had their first 2 orders exactly in the sequence of: first order being hat, second order being sunglasses?
Thanks!
Well, in BigQuery you can do something like this:
select o.customerId
from orders o
group by o.customerId
having array_agg(o.product order by o.order_date asc limit 2) = array['hat', 'sunglasses'];
You don't specify what the data looks like, but this should give you the idea of an approach.
you try like this
SELECT CustomerID, Product
FROM Customers
WHERE Productin ('Hat', 'sunglasses')
order by Product Asc

getting one to one result from one to many relationship [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
my situation is that i have one to many relation ,like order and orderdetails ,i need to get order which has single order details.
How about:
select *
from order
where order_number in
(select order_number
from order_details
group by order_number
having count(*) = 1)
SELECT O1.order_number
FROM Orders AS O1
WHERE 1 = (
SELECT COUNT(*)
FROM OrderDetails AS D1
WHERE O1.order_number = D1.order_number
);