What will be sql query for the fifth youngest employee?
Below query is wrong? please help
SELECT EmpID, EmpName, EMPDOB,
ORDER BY EMPDOB DESC
WHERE ROWNUMBER = 5
FROM dbo.EMP
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY EMPDOB ASC) AS rownumber,
EmpID, EmpName, EMPDOB
FROM EMPLOYEE
) AS foo
WHERE rownumber = 5
The following query will solve your problem:
SELECT TOP 1
T.EmpID
, T.EmpName
, T.EMPDOB
FROM (SELECT TOP 5 * FROM dbo.EMP ORDER BY EMPDOB DESC) AS T
Note: This solution assumes that EMPDOB is in a date or integer based format.
Related
I need a query to display employee names, dept no and highest salary in dept no wise
Example
ENAME SAL dept no
KING 5000 10
FORD 3000 20
SCOTT 3000 20
BLAKE 2850 30
As the commenter said, it's helpful when you provide more detail about what you've tried.
Nonetheless, I think you're looking for something like this:
;with cte as (
select ename, sal, [dept no]
, row_number() over (partition by [dept no] order by sal desc, ename) rn
from your_table
)
select *
from cte
where rn = 1
Note that your example data shows the same SAL of 3000 for [dept no] 20. To attempt to break ties, I added the ename to the order by statement.
This query displays correctly
select ENAME, SAL, [deptno]
from (
select *
, DENSE_RANK() over(partition by [deptno] order by sal desc) as Highest_sal
from Employee
) a
where a.Highest_sal = 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;
this is driving me crazy! does anyone know how to write some SQL that will return the MIN and MAX dates from groups of sequential numbers? please see screen shots below.
This is the SQL I used:
SELECT
num
, empid
, orderdate
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY orderdate) AS Num
, empid
, orderdate
FROM TSQL.Sales.Orders)T1
WHERE empid = 4
This is what it returns:
What I would like to do is get the Min and Max dates for each set of sequential numbers based on the num column. For example: the first set would be num 3, 4, 5 & 6. so the Min date is 2006-07-08 and the Max date is 2006-07-10
See example of results needed below
Any help with this would be much appreciated, thank you in advance
Update
I have now changed the SQL to do what I needed: example as follows:
Select
empid
, Island
, MIN(orderdate) as 'From'
, Max(orderdate) as 'To'
From
(select
empid
, num
, num - ROW_NUMBER() OVER (ORDER BY num, orderdate) as Island
, orderdate
from
(Select
ROW_NUMBER() OVER (ORDER BY orderdate) as Num
, empid
, orderdate
from TSQL.Sales.Orders)T1
where empid = 4
)T2
group By
empid
, Island
Result
Thank you so much for your help on this, I have been trying this for ages
Regards
Jason
This should do it:
;with dateSequences(num, empId, orderDate) as
(
select ROW_NUMBER() over (order by orderdate) as num
, empId
, orderdate
from yourTable
),
dateGroups(groupNum, empId, orderDate, num) as
(
select currD.num, currD.empid, currD.orderDate, currD.num
from dateSequences currD
left join dateSequences prevD on prevD.num = currD.num - 1 and prevD.empid = currD.empId
where prevD.num is null
union all
select dg.groupNum, d.empId, d.orderDate, d.num
from dateSequences d
inner join dateGroups dg on dg.num + 1 = d.num and d.empId = dg.empId
)
select empId, min(orderDate) as MinDate, max(orderDate) as MaxDate
from dateGroups
where empId = 4
group by empId, groupNum
Basically it first makes a CTE to get the row numbers for each row in date order. Then it makes a recursive CTE that first finds all the groups with no previous sequential entries then adds all subsequent entries to the same group. Finally it takes the records with all the group numbers assigned and groups them by their group number and gets the min and max dates.
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
This is kind of like this questions:
T-SQL: How to use MIN
But I want it to return the lowest 4 values per group.
Thank you
Use a CTE with ROW_NUMBER:
WITH CTE AS(
SELECT T.*, RN=ROW_NUMBER()OVER(PARTITION BY Col1 Order By Col2 ASC)
FROM dbo.TableName T
)
SELECT * FROM CTE WHERE RN <= 4
Ranking Functions
SELECT
ID, SomeVal
FROM (
SELECT ID, SomeVal, row_number() over(PARTITION BY id ORDER BY SomeVal ASC) rn
FROM [TableName]
) T
WHERE rn <= 4
This gives you the lowest first 4 SomeVal values per ID.