January 30, 2009 at 1:22 am
Good day to all,
Im having a hard time doing this T-SQL, can anyone help me with this one,, please...:(
Im doing this for 3 hours.
This is the given data:
UsersDateTime Sections Title
Client1 2009-01-30 08:00 AM I A
Client2 2009-01-30 09:00 AM I B
Client3 2009-01-30 09:30 AM I B
This is the required output:
Data Section Title TotalRows
Client3 2009-01-30 09:30 AM I B 3
I must get the latest and count the number of rows.
Thanks,
Morris
January 30, 2009 at 3:55 am
this is the little code and the situation like yours I didn't have yet and sure that the others can have better idea how to do that but try and just to see if you retrieve the correct results!
SELECT TOP 1 *, ROW_NUMBER() OVER (ORDER BY DATECOLUMN) AS COUNT_ROW FROM YOUR_TABLE
ORDER BY DATECOLUMN DESC
hope it helps!
January 30, 2009 at 4:40 am
Just to be sure that this code above is just to find you solution here, as you can see I didn't count nothing just generating the number in order by date with Row_number() function and selecting the last record!
:hehe::hehe:
January 30, 2009 at 4:59 am
Try this...
DROP TABLE #Temp
CREATE TABLE #Temp (Users VARCHAR(10), [DateTime] DATETIME, Sections VARCHAR(1), Title VARCHAR(1))
INSERT INTO #Temp (Users, [DateTime], Sections, Title)
SELECT 'Client1', '2009-01-30 08:00 AM', 'I', 'A' UNION ALL
SELECT 'Client2', '2009-01-30 09:00 AM', 'I', 'B' UNION ALL
SELECT 'Client3', '2009-01-30 09:30 AM', 'I', 'B'
SELECT TOP 1 *, [Total Rows] = (SELECT COUNT(*) FROM #Temp)
FROM #Temp
ORDER BY [DateTime] DESC
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 30, 2009 at 5:16 am
Chris Morris (1/30/2009)
Try this...
DROP TABLE #Temp
CREATE TABLE #Temp (Users VARCHAR(10), [DateTime] DATETIME, Sections VARCHAR(1), Title VARCHAR(1))
INSERT INTO #Temp (Users, [DateTime], Sections, Title)
SELECT 'Client1', '2009-01-30 08:00 AM', 'I', 'A' UNION ALL
SELECT 'Client2', '2009-01-30 09:00 AM', 'I', 'B' UNION ALL
SELECT 'Client3', '2009-01-30 09:30 AM', 'I', 'B'
SELECT TOP 1 *, [Total Rows] = (SELECT COUNT(*) FROM #Temp)
FROM #Temp
ORDER BY [DateTime] DESC
Cheers
ChrisM
Yea this is correct thnx Chris Morris!
Sorry I didn't check that we are in SQL Server 2000 forum!
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply