Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First Table : Exam Table
ex1 2014-08-07 13:21:48.000
ex2 2014-08-07 14:21:48.000
ex3 2014-08-06 13:21:48.000
ex4 2014-08-05 13:21:48.000

Second Table :Lecture Table

l1 2014-08-07 11:21:48.000
l2 2014-08-07 12:21:48.000
l3 2014-08-06 11:21:48.000
l4 2014-08-05 12:21:48.000
I want to retrieve top 5 rows from both the table by calculating time and date of both simultaneously.This record corresponds to one student. I want to find out recent 5 exams or lectures attended by student. Expected Result is as follows :

ex2 2014-08-07 14:21:48.000
ex1 2014-08-07 13:21:48.000
l2 2014-08-07 12:21:48.000
l1 2014-08-07 11:21:48.000
ex3 2014-08-06 13:21:48.000

While displaying result, time and date both should be compared properly. Can anyone tell me sql query for this?
Posted
Comments
Herman<T>.Instance 12-Aug-14 4:20am    
what is the link between both tables. Any ID?

SQL
;WITH MyCte(type, date) AS
(
SELECT LectureID, StartDateTime FROM lectureTable
UNION ALL
SELECT Exam_ID, DateOfExam FROM examTable
)

SELECT TOP 5 * FROM MyCte ORDER BY 2 DESC
 
Share this answer
 
v2
Comments
m-shraddha 11-Aug-14 7:46am    
'MyCte' has fewer columns than were specified in the column list.
Mayank Vashishtha 11-Aug-14 8:02am    
This code must work. I've just checked it. Working fine. Try selecting the complete query from ; to DESC. then try and execute it.
SQL
;WITH MyCte(EType,EventDate) AS
(
SELECT 'Exam',E.DateOfExam FROM tbl_ExamCreation E
UNION ALL
SELECT 'Lecture',LS.StartDateTime FROM tbl_LectureScheduling LS
)
 
SELECT EType,EventDate FROM MyCte ORDER BY 2 DESC 




This is Perfect Soluction. As we are selecting two columns, we have to mention two column name after MyCTe. as well as after last SELECT statement.
 
Share this answer
 
v3
Comments
jaket-cp 12-Aug-14 3:59am    
top 5 is missing in the solution
Here is a way of getting the desired results without using a common table expression (CTE).
SQL
--Data setup
--create tables
declare @Exam as table (id varchar(5), theDateTime datetime);
declare @Lecture as table (id varchar(5), theDateTime datetime);

--populate tables
insert into @Exam values ('ex1','2014-08-07 13:21:48.000');
insert into @Exam values ('ex2','2014-08-07 14:21:48.000');
insert into @Exam values ('ex3','2014-08-06 13:21:48.000');
insert into @Exam values ('ex4','2014-08-05 13:21:48.000');

insert into @Lecture values ('l1','2014-08-07 11:21:48.000');
insert into @Lecture values ('l2','2014-08-07 12:21:48.000');
insert into @Lecture values ('l3','2014-08-06 11:21:48.000');
insert into @Lecture values ('l4','2014-08-05 12:21:48.000')

SQL
--query, not using CTE
select top 5
    id,
    theDateTime
from (
    select id, theDateTime from @Exam
    union all
    select id, theDateTime from @Lecture
) ExamAndLecture
order by theDateTime desc;

I am not sure which version will run faster, but I assume there will not be much difference in the speed of execution.
I have included the cte version below so you may test and compare the difference or similarity in the query structure.
SQL
--query, using CTE
with ExamAndLecture(id, theDateTime) as (
    select id, theDateTime from @Exam
    union all
    select id, theDateTime from @Lecture
)
select top 5
    id,
    theDateTime
from ExamAndLecture
order by theDateTime desc;
 
Share this answer
 
SQL
;WITH MyCte(type, date) AS
(
SELECT * FROM examTable
UNION ALL
SELECT * FROM lectureTable
)

SELECT TOP 5 * FROM MyCte ORDER BY 2 DESC
 
Share this answer
 
Comments
m-shraddha 11-Aug-14 6:31am    
That semicolon before WITH is part of the query or it was just mistyped?
Mayank Vashishtha 11-Aug-14 6:32am    
part of the query. that is the syntax of cte(common table expression).

If it solves your problem, kindly rate it.
m-shraddha 11-Aug-14 6:36am    
Msg 205, Level 16, State 1, Line 1
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
m-shraddha 11-Aug-14 6:37am    
can you please tell me what is this error indicates?
Mayank Vashishtha 11-Aug-14 6:41am    
It happens when your inner queries combined with UNION send different columns. Share your table structure with column names, I'll send you the updated query.

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