473,396 Members | 1,866 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,396 software developers and data experts.

BrainDamage

5
hi,
I have two tables as given created in SQL srever 2005

Employees
-----------------
>intEid (primary key)
>intLeaveCredit

LeaveApplications
--------------------------
>intSlno (primary key)
>intEid (Foreign key)
>strTypeOfLeave
...
>dteApplicationDate (datetime)
>bolSanctioned (boolean)

now, i need to be able to get max date of each intEid in LeaveApplications with bolSanctioned as false. I need all the fields in the LeaveApplications Table and intLeaveCredit from the Employees table, to be in the resulting joined table.

this is what I have been doing...

Expand|Select|Wrap|Line Numbers
  1. select e.intEid, e.intLeaveCredit, l.strTypeOfLeave, l.dteFrom, l.dteTo, l.strReason, l.dteApplicationDate, l.bolSanctioned
  2.     from Employees e
  3.     join LeaveApplications l
  4.     on e.intEid = l.intEid
  5.     where l.dteTo = (select max(dteTo) from LeaveApplications where   bolSanctioned = 'false' )
  6.  
Mar 30 '07 #1
8 1293
Mammu
5
Sorry about the title folks... did that in frustation.. and thank you in advance for any help.
Mar 30 '07 #2
iburyak
1,017 Expert 512MB
I don't see a problem in your query.
Do you get incorrect result?

You can do the same differently if you wish but you did OK.


[PHP]select Top 1 e.intEid, e.intLeaveCredit, l.strTypeOfLeave, l.dteFrom, l.dteTo, l.strReason, l.dteApplicationDate, l.bolSanctioned
from Employees e
join LeaveApplications l
on e.intEid = l.intEid
where bolSanctioned = 'false'
ORDERR BY l.dteTo desc[/PHP]
Mar 30 '07 #3
Mammu
5
I don't get any records. I've made only a couple of test entries in the necessary fields for each table... it gave me a result when i reteived records from only the Leave records table. but what i need is a joined table with all the fields from the LeaveApplications table and the LeaveCredit field from the Employee table.

thank you for replying
Mar 30 '07 #4
iburyak
1,017 Expert 512MB
It is not a query problem.

Execute following query. Did you get any records? If not it means you don't have matching intEid in both tables.


[PHP]select *
from Employees e
join LeaveApplications l
on e.intEid = l.intEid[/PHP]

If you received result above try to execute query below. If you don't have a result here it means even thou you have matching records none of them is False.

[PHP]Select *
from Employees e
join LeaveApplications l
on e.intEid = l.intEid
where bolSanctioned = 'false'[/PHP]
Mar 30 '07 #5
Mammu
5
Sorry for the delay in replying... I was not working. You are correct about the entries I made. I had no record with bolSanctioned as false. I cannot believe I wasted so much time on this... and I would have wasted more time if it weren't for you. Thank you so much.

But I still have a problem. I'm only getting one record with the max date i the whole table. what i needed was the last date for each employee listed in the table. any suggestions? and Thank you once again for helping me...
Apr 2 '07 #6
iburyak
1,017 Expert 512MB
Try this:


[PHP]select e.intEid, e.intLeaveCredit, l.strTypeOfLeave, l.dteFrom,
l.dteTo, l.strReason, l.dteApplicationDate, l.bolSanctioned
from Employees e
join LeaveApplications l on e.intEid = l.intEid
where l.dteTo = (select max(dteTo) from LeaveApplications where bolSanctioned = 'false' and intEid = e.intEid )
and bolSanctioned = 'false' [/PHP]
Apr 2 '07 #7
Mammu
5
thank you iburyak for your help. I finally managed to find a solution without using any join.

this is my solution. Thank you all the same.

Expand|Select|Wrap|Line Numbers
  1. select e.intEid , e.intLeaveCredit, l.strTypeOfLeave , l.dteFrom , l.dteTo , l.strReason , l.dteApplicationDate 
  2. from LeaveApplications l, Employees e 
  3. where e.intEid = l.intEid
  4. and dteTo = (SELECT max(dteApplicationDate) from LeaveApplications l1 WHERE l1.intEid = l.intEid and l.bolSanctioned = 'False')
Apr 4 '07 #8
iburyak
1,017 Expert 512MB
thank you iburyak for your help. I finally managed to find a solution without using any join.

this is my solution. Thank you all the same.

Expand|Select|Wrap|Line Numbers
  1. select e.intEid , e.intLeaveCredit, l.strTypeOfLeave , l.dteFrom , l.dteTo , l.strReason , l.dteApplicationDate 
  2. from LeaveApplications l, Employees e 
  3. where e.intEid = l.intEid
  4. and dteTo = (SELECT max(dteApplicationDate) from LeaveApplications l1 WHERE l1.intEid = l.intEid and l.bolSanctioned = 'False')


HA HA HA..... :)

Whatever you show is a join just written old way that is not recommended to use in SQL syntax.... LOL

Old syntax for join:

[PHP]from LeaveApplications l, Employees e
where e.intEid = l.intEid[/PHP]

New syntax for join:

[PHP]from Employees e
join LeaveApplications l on e.intEid = l.intEid[/PHP]

Good Luck.
Apr 4 '07 #9

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

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
17
by: Joe | last post by:
I have a situation where the wrong constructor is being called. I have defined 2 constructors with different parameter types that are defined as follows... class __declspec(dllexport)CColumn :...
9
by: sb | last post by:
If there is at least one user-defined constructor, no constructors are implicitly declared. struct my_vec : public vector<int> { double foo; my_vec(size_t n) : vector<int>(2*n) {} // oops, no...
1
by: peter pilsl | last post by:
Is there a way to reset the xlog on postgres 7.1 ? Due to complete & intensive braindamage of sysadmin (=me) the whole pg_xlog-folder has been emptied on a old 7.1-installation. pg_resetxlog is...
4
by: David Veeneman | last post by:
I'm using the new BindingList generic class in a collection class. For those not familiar with BindingLists, they are similar to generic ArrayLists, but they implement the IBindingList interface,...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
2
by: BRENDAMAGE | last post by:
can anyone hlp on this... how to trap empty string(value) before it saves to database... this one id like to happen... lastname firstname middlename
21
by: J de Boyne Pollard | last post by:
HSfgets() is standard in C file I/O. HS> HSThe only issue you need to pay attention too, is RAW HS(binary) vs COOK mode. It will relate the EOL (end HSof line) definitions of MS-DOS (<CRL><LR>)...
74
by: copx | last post by:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that properly written C++ outperforms C code. I will just copy his first example here, which is supposed to demonstrate how C++...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.