Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
heyy...


I want a query, to find the first and last records in a given table but I want the result by some condition..

here is my table data :

CSS
No.       starttime  Endtime
-------   ---------  --------
0500000001  06:52   06:59
0500000001  07:00   07:27
0500000001  07:28   08:09
0500000001  08:14   08:40
0500000001  08:42   10:00
0500000001  10:01   11:29
0500000001  11:31   12:41
0500000001  12:43   13:28
0500000144  10:59   11:03
0500000144  11:03   11:04
0500000144  11:05   11:07
0500000144  11:08   11:37
0500000144  11:37   09:47
0500000144  09:47   09:51
0500000144  09:52   09:53
0500000144  09:54   10:46
0500000144  10:47   11:12
0500000144  11:13   11:16
0500000144  11:17   13:02
0500000144  13:02   09:29
0500000144  13:29   09:29



here I want My result as like this

No.          starttime     Endtime
  ------        ----------   --------
  0500000001    06:52         13:28
  0500000144    10:59         09:29


ie. first record and last record of No.

Any idea how to write query in mysql...?
Posted
Updated 11-Oct-22 1:23am

Do you Google[^] it?
Try in this format..
To get the first record:
SQL
select col1,col2,col3 from tab1 order by col1 asc limit 1;

To get the last record:
SQL
select col1,col2,col3 from tab1 order by col1 desc limit 1;
 
Share this answer
 
v4
Try like this
SQL
DECLARE @Vehicle TABLE (
VehicleNo NVARCHAR(50),Datetime1 Datetime,Datetime2 Datetime);
INSERT INTO @Vehicle (VehicleNo,Datetime1,Datetime2)
VALUES('0500000001','06:52','06:59')
INSERT INTO @Vehicle (VehicleNo,Datetime1,Datetime2)
VALUES('0500000001','07:00','07:27')
INSERT INTO @Vehicle (VehicleNo,Datetime1,Datetime2)
VALUES('0500000144','10:59','11:03')

SELECT top 1 * FROM @Vehicle WHERE VehicleNo=(SELECT MIN(VehicleNo) FROM @Vehicle)
UNION
SELECT top 1 * FROM @Vehicle WHERE VehicleNo=(SELECT MAX(VehicleNo) FROM @Vehicle)
 
Share this answer
 
Comments
aravindnass 14-Aug-13 3:35am    
I want mysql query..TOP keyword not working...
 
Share this answer
 
v2
To get First and Last record from SQL Query try like this.

SQL
SELECT *
FROM table1
WHERE id IN (
SELECT TOP 1 MIN(id) ids
FROM table1
UNION ALL
SELECT TOP 1 MAX(id) ids
FROM table1)


In your case
SQL
SELECT *
FROM yourTableName
WHERE id No(
SELECT TOP 1 MIN(No) ids
FROM yourTableName
UNION ALL
SELECT TOP 1 MAX(No) ids
FROM yourTableName
)
 
Share this answer
 
Comments
aravindnass 14-Aug-13 3:35am    
I want mysql query..TOP keyword not working...
syed shanu 14-Aug-13 3:43am    
In case of MYSQL You need to limit.
select * from table where id= (select id from tab1 order by col1 asc limit 1;)
or id=(select id from tab1 order by col1 desc limit 1; )


Check this links
http://stackoverflow.com/questions/2735395/how-to-find-first-and-last-record-from-mysql-table
http://stackoverflow.com/questions/1143487/how-to-get-the-first-and-last-record-of-db

http://stackoverflow.com/questions/14334805/mysql-getting-first-and-last-record-of-grouping

Mean time try to use google in google you can get all your answers.
Try this code. Replace the table and column name as per your design.

SQL
select No, MIN(starttime),MAX(endtime) from table1
group by No
 
Share this answer
 
To get the first 10 records:
select col1,col2,col3 from tab1 order by col1 asc limit 10;

To get the last 10 records:
select col1,col2,col3 from tab1 order by col1 asc limit 10;
 
Share this answer
 
Comments
CHill60 28-Nov-18 7:19am    
No different from Solution 5 posted 5 years ago and the OP only wanted the first and last single record not first 10 and last 10.
If you are going to post solutions to questions that have already been answered, especially if one or more solutions have been accepted, then make sure you are bringing something new to the thread and not just repeating what someone else has already posted
Try this

content of the table

SELECT TOP [Number]
      ,[StratTime]
      ,[EndTime]
  FROM [TestingData].[dbo].[Table_1]


Number	StratTime	EndTime
500000001	06:52:00.0000	06:59:00.0000
500000001	08:00:00.0000	08:45:00.0000
500000001	07:00:00.0000	07:45:00.0000



Result

SELECT [Number]
      ,Min(StratTime)
      ,Max(EndTime)
  FROM [TestingData].[dbo].[Table_1]
  Group by Number


Number	(No column name)	(No column name)
500000001	06:52:00.0000	08:45:00.0000
 
Share this answer
 
Comments
Richard Deeming 29-Nov-18 7:35am    
As already mentioned in solution #4.
CHill60 29-Nov-18 7:56am    
And the same problem as Solution 4 - you did not select the records. Read the question carefully!
SELECT * FROM(SELECT E.*,ROW_NUMBER() OVER(ORDER BY ROWNUM ASC) AS RS FROM EMP_INFO E) WHERE RS=1
UNION ALL
SELECT * FROM(SELECT E.*,ROW_NUMBER() OVER(ORDER BY ROWNUM DESC) AS RS FROM EMP_INFO E) WHERE RS=1;
 
Share this answer
 
Comments
Richard MacCutchan 20-Aug-20 4:42am    
Already well answered seven years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900