how to get specific number of row data in SQL Server 2005 - sql

my table look like
ID Name TakenDateTime
2 critical care 19-Dec-12 11:04:45 AM
3 Pathology 26-Dec-12 5:59:28 PM
4 Radiology 05-Jan-13 7:20:56 PM
5 Cardiology 07-Jan-13 3:54:04 PM
6 Neurology 07-Jan-13 3:56:13 PM
7 Oncology 07-Jan-13 3:56:19 PM
9 Orthopedic 08-Jan-13 5:30:29 PM
10 Cardiothoracic 08-Jan-13 5:30:52 PM
from this table i just want 5th number of row data in SQL Server 2005.
is it possible if yes then please provide me possible way for that.
Any help will be appreciate...

SELECT
ID,
Name,
TakenDateTime
FROM (SELECT
ROW_NUMBER() OVER (ORDER BY Id) AS Row,
*
FROM TableName)
t
WHERE t.Row = 5
Sql Fiddle Demo

Related

How do you get the last entry for each month in SQL?

I am looking to filter very large tables to the latest entry per user per month. I'm not sure if I found the best way to do this. I know I "should" trust the SQL engine (snowflake) but there is a part of me that does not like the join on three columns.
Note that this is a very common operation on many big tables, and I want to use it in DBT views which means it will get run all the time.
To illustrate, my data is of this form:
mytable
userId
loginDate
year
month
value
1
2021-01-04
2021
1
41.1
1
2021-01-06
2021
1
411.1
1
2021-01-25
2021
1
251.1
2
2021-01-05
2021
1
4369
2
2021-02-06
2021
2
32
2
2021-02-14
2021
2
731
3
2021-01-20
2021
1
258
3
2021-02-19
2021
2
4251
3
2021-03-15
2021
3
171
And I'm trying to use SQL to get the last value (by loginDate) for each month.
I'm currently doing a groupby & a join as follows:
WITH latest_entry_by_month AS (
SELECT "userId", "year", "month", max("loginDate") AS "loginDate"
FROM mytable
)
SELECT * FROM mytable NATURAL JOIN latest_entry_by_month
The above results in my desired output:
userId
loginDate
year
month
value
1
2021-01-25
2021
1
251.1
2
2021-01-05
2021
1
4369
2
2021-02-14
2021
2
731
3
2021-01-20
2021
1
258
3
2021-02-19
2021
2
4251
3
2021-03-15
2021
3
171
But I'm not sure if it's optimal.
Any guidance on how to do this faster? Note that I am not materializing the underlying data, so it is effectively un-clustered (I'm getting it from a vendor via the Snowflake marketplace).
Using QUALIFY and windowed function(ROW_NUMBER):
SELECT *
FROM mytable
QUALIFY ROW_NUMBER() OVER(PARTITION BY userId, year, month
ORDER BY loginDate DESC) = 1

Max date among records and across tables - SQL Server

I tried max to provide in table format but it seem not good in StackOver, so attaching snapshot of the 2 tables. Apologize about the formatting.
SQL Server 2012
**MS Table**
**mId tdId name dueDate**
1 1 **forecastedDate** 1/1/2015
2 1 **hypercareDate** 11/30/2016
3 1 LOE 1 7/4/2016
4 1 LOE 2 7/4/2016
5 1 demo for yy test 10/15/2016
6 1 Implementation – testing 7/4/2016
7 1 Phased Rollout – final 7/4/2016
8 2 forecastedDate 1/7/2016
9 2 hypercareDate 11/12/2016
10 2 domain - Forte NULL
11 2 Fortis completion 1/1/2016
12 2 Certification NULL
13 2 Implementation 7/4/2016
-----------------------------------------------
**MSRevised**
**mId revisedDate**
1 1/5/2015
1 1/8/2015
3 3/25/2017
2 2/1/2016
2 12/30/2016
3 4/28/2016
4 4/28/2016
5 10/1/2016
6 7/28/2016
7 7/28/2016
8 4/28/2016
9 8/4/2016
9 5/28/2016
11 10/4/2016
11 10/5/2016
13 11/1/2016
----------------------------------------
The required output is
1. Will be passing the 'tId' number, for instance 1, lets call it tid (1)
2. Want to compare tId (1)'s all milestones (except hypercareDate) with tid(1)'s forecastedDate milestone
3. return if any of the milestone date (other than hypercareDate) is greater than the forecastedDate
The above 3 steps are simple, but I have to first compare the milestones date with its corresponding revised dates, if any, from the revised table, and pick the max date among all that needs to be compared with the forecastedDate
I managed to solve this. Posting the answer, hope it helps aomebody.
//Insert the result into temp table
INSERT INTO #mstab
SELECT [mId]
, [tId]
, [msDate]
FROM [dbo].[MS]
WHERE ([msName] NOT LIKE 'forecastedDate' AND [msName] NOT LIKE 'hypercareDate'))
// this scalar function will get max date between forecasted duedate and forecasted revised date
SELECT #maxForecastedDate = [dbo].[fnGetMaxDate] ( 'forecastedDate');
// this will get the max date from temp table and compare it with forecasatedDate/
SET #maxmilestoneDate = (SELECT MAX(maxDate)
FROM ( SELECT ms.msDueDate AS dueDate
, mr.msRevisedDate AS revDate
FROM #mstab as ms
LEFT JOIN [MSRev] as mr on ms.msId = mr.msId
) maxDate
UNPIVOT (maxDate FOR DateCols IN (dueDate, revDate))up );

How to select top (5) rows with specified Id and latest date?

The table is this:
Id_P Id_Utente PasswordOld_ DateOld_
1 134 E0476F2E85A84FB4E68AA26A841A86FA 8/01/2015 10:30:00 PM
2 134 9C454981DE1702C7AAD3B435B51404EE 8/02/2015 10:30:00 PM
3 134 BA0D9BE25565C34CAAD3B435B51404EE 8/03/2015 10:30:00 PM
4 134 9C6C9E34FB63DC9DE68AA26A841A86FA 8/04/2015 10:30:00 PM
5 134 14BEE187F918F8817584248B8D2C9F9E 8/04/2015 10:30:00 PM
6 135 9A70F4507624037CAAD3B435B51404EE 15/01/2015 10:30:00 PM
7 135 C3DB775AE3B3BD29E68AA26A841A86FA 15/02/2015 10:30:00 PM
8 135 0BB76B9CA33D8E31AAD3B435B51404EE 15/03/2015 10:30:00 PM
9 136 9A70F4507624037CAAD3B435B51404EE 26/01/2015 10:30:00 PM
10 136 C3DB775AE3B3BD29E68AA26A841A86FA 26/02/2015 10:30:00 PM
11 136 0BB76B9CA33D8E31AAD3B435B51404EE 26/03/2015 10:30:00 PM
You can use limit or top. Try this:
SELECT TOP 5 * FROM yourTablename ORDER BY DateOld_ DESC, Id_P DESC;
SELECT LIMIT is not supported in all SQL databases. But you can use the query above for SQL Server or MSAccess and the below query for mysql specifically.
SELECT * FROM yourTablename ORDER BY DateOld_ DESC LIMIT 5;
For more info, you can visit this link.
Use a order by clause
select top 5 *
from table1
where Id_P in (1,2,3,4,5)
order by DateOld_;
LIMIT is the best way.
Use it like:
SELECT *
FROM TABLE_NAME
LIMIT 5
As your DateOld_ field is of type ntext, you cannot do an order by on that. To sort the results based on that column, you need to convert the values in to datetime format.
You can execute the following query to fetch 5 rows with id_utente = 134 and sorted in descending order as per the time:
select top 5 * from table1
where id_utente = 134
order by convert(datetime, convert(varchar, dateold_)) desc;
SQLFiddle Example

Creating Subtotals - SQL Server 2008

I have the below data set, which is reporting employee hours for each day, and each day has a week number (1 or 2) as running for two weeks.
SiteId SiteName EmployeeId FirstName SurName Date WorkedHours WorkedMinutes WeekNo
4 Test 7 Steve Stone 2014-03-01 4.5 270 1
4 Test 7 Steve Stone 2014-03-02 9 540 1
4 Test 7 Steve Stone 2014-03-03 4.5 270 1
4 Test 7 Steve Stone 2014-03-08 4.5 270 2
4 Test 7 Steve Stone 2014-03-09 9 540 2
4 Test 7 Steve Stone 2014-03-10 4.5 270 2
I would like to add a sub total of workedhours and workedminutes for each employee for each week. So the desired outcome would have a row at the end of each section of weeks for the sub total, which would give a total for week 1 and then again for week 2
If you are using SQL Server 2008 or above you should take a look at GROUPING SETS. They are much faster than Rollup. Its an awesome feature to do such tasks.
I recently implemented similar logic
select EmpId, Year, FaciLeaveType, Month, DateValue , Sum(Leaves)
FROM EmplLeave
GROUP BY
GROUPING SETS(
( EmpId,Year,FaciLeaveType,Month,DateValue )
,( EmpId,Year,FaciLeaveType,Month )
,( EmpId,Year,FaciLeaveType )
)
https://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx
Rollup was the best way forward
Here.

Access SQL - Select only the last sequence

I have a table with an ID and multiple informative columns. Sometimes however, I can have multiple data for an ID, so I added a column called "Sequence". Here is a shortened example:
ID Sequence Name Tel Date Amount
124 1 Bob 873-4356 2001-02-03 10
124 2 Bob 873-4356 2002-03-12 7
124 3 Bob 873-4351 2006-07-08 24
125 1 John 983-4568 2007-02-01 3
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
So, I would like to obtain only these lines:
124 3 Bob 873-4351 2006-07-08 24
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
Anyone could give me a hand on how I could build a SQL query to do this ?
Thanks !
You can calculate the maximum sequence using group by. Then you can use join to get only the maximum in the original data.
Assuming your table is called t:
select t.*
from t join
(select id, MAX(sequence) as maxs
from t
group by id
) tmax
on t.id = tmax.id and
t.sequence = tmax.maxs