I have 2 tables:
COURSE
------
Id
Name
TEST
------
Id
CourseId (FK to `COURSE.ID`)
DATETIME
NUMBERS
Suppose COURSE
table with ID
1,2 (only 2 columns) and TEST
table with 8 numbers of data having different DATETIME
and CourseId
of 1 (3 columns) and 2 (6 columns).
I want to find the minimum DATETIME,CourseID
and Name
by joining these 2 tables. The below query is giving a 2 output:
(SELECT min([DATETIME]) as DATETIME ,[TEST].CourseID,Name
FROM [dbo].[TEST]
left JOIN [dbo].[COURSE]
ON [dbo].[TEST].CourseID=[COURSE].ID GROUP BY CourseID,Name)
I want a single column output i.e. a single output column (minimum datetime along with Name and ID)..HOW can i achieve??
With 2 courses you are always going to get 2 rows when joining like this. It will give you the minimum date value for each course. The first way you can get a single row is to use
TOP 1
in your query, which will simply give you the course with the earliest test date. The other way is to use aWHERE
clause to filter it by a single course.Please run this sample code with some variations of what you can do, notes included in comments:
In my understanding, you want to return the minimum date from the entire table with the course details of that day. Please try the below script