473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Basic Question on Joins

Hi All,

I have a 2 tables namely Employee and reports. An employee will/can have multiple reports associated with him for a particular month. I need to fetch the the employee details and the last (or latest) report details for that month.

If for e.g Employee table has 100 entries, i will have (1000 * 12 month * 30 reports/month) 360,000 entries in the reports table. So my query should return only 1000 records (for each employee the latest report for that month). How can i do this without using cursor?

thanks,
Dilip
Sep 17 '07 #1
3 888
azimmer
200 Expert 100+
Hi All,

I have a 2 tables namely Employee and reports. An employee will/can have multiple reports associated with him for a particular month. I need to fetch the the employee details and the last (or latest) report details for that month.

If for e.g Employee table has 100 entries, i will have (1000 * 12 month * 30 reports/month) 360,000 entries in the reports table. So my query should return only 1000 records (for each employee the latest report for that month). How can i do this without using cursor?

thanks,
Dilip
Join the tables and retrieve the records with the max. date, like this:
Expand|Select|Wrap|Line Numbers
  1. select *
  2. from employees e inner join reports r on e.empid=r.empid and r.repdate=(select max(repdate) from reports r2 where r2.empid=r.empid)
  3.  
Sep 17 '07 #2
Join the tables and retrieve the records with the max. date, like this:
Expand|Select|Wrap|Line Numbers
  1. select *
  2. from employees e inner join reports r on e.empid=r.empid and r.repdate=(select max(repdate) from reports r2 where r2.empid=r.empid)
  3.  

sorry i had reported it instead of replying...

thanks a lot for that reply...was very helpful and it only shows that i need to learn the basics . any idea where i can start from?
Sep 17 '07 #3
azimmer
200 Expert 100+
sorry i had reported it instead of replying...

thanks a lot for that reply...was very helpful and it only shows that i need to learn the basics . any idea where i can start from?
Not really, if you mean a certain book or URL. I suggest that you practice, look at what others do (look at the threads in this forum for example), check the docs (especially the help and not only what you need at a point but see other topics as well).

Others may be able to point you to some good sources as well.
Sep 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
3
by: nick | last post by:
Hi, I'm pretty much a database beginner and have what I think is a basic question: If I have a table which has a 'status' column and I can have say three statuses: "active", "pending",...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
2
by: Ryan McBride | last post by:
Once again at my wonderful job i've been given the task of "come teach your fellow idiot coworkers the skills you have" I write software for a company in chicago. I use visual basic on asp.net...
4
MMcCarthy
by: MMcCarthy | last post by:
To view Access queries in SQL rather than Access query design - open the query design window and change the view to SQL: Select Statement SELECT FROM ; Append Statement INSERT INTO (, , )...
11
by: walterbyrd | last post by:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax and non-Ajax solutions abound. With Python, finding such library, or apps. seems to be much more difficult to find. I...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.