BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with a query

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Eric Stewart
Guest





PostPosted: Mon May 10, 2004 6:48 pm    Post subject: Help with a query Reply with quote



I have two tables. Showing only the necessary info, they look like this

rsiEmployeeMaster (T1)

AreaID varchar(20)
Empnum int
EmpName varchar(31)
MaintSite varchar(20)
Shift varchar(20)

rsiScheduleMasterEmployee (T2)

AreaID varchar(20)
Empnum int
ScheduledDay smallint
RouteSection varchar(2)

T1 has one record for each employee. T2 may have multiple records for each
employee. The query I am trying to write will have input paramaters that
provide AreaID, MaintSite, Shift, ScheduledDay, and RouteSection. I need a
query that will return a result set with employees that meet the following
criteria:

Any employee with the specified AreaID, MaintSite, Shift with no record in
T2 for ScheduledDay.
Any employee with the specified AreaID, MaintSite, Shift, ScheduledDay with
a record in T2 that meets this condition:
RouteSection is any letter between A and Z but not the same as the
input parameter supplied (RouteSection)
Also, if the employee has a record in T2 with the specified AreaID,
MaintSite, Shift, ScheduledDay with a RouteSection of '*', he should not be
included in the result set.

I hope this makes since. In case it helps, here is some more detail on what
I am trying to do. I have a shift we'll call MYSHIFT. The shift has routes
and some routes are pulled weekly and others are pulled every other week.
Each route has a route section assigned to it. If it is a weekly route the
route section is '*'. If it is a route pulled every other week, it has a
letter between A and Z assigned to it. When the schedule is opened, the user
specifies what day and routesection they want to see, Monday, Section * or
Monday, Section A for example. So it is possible for an employee to have two
Monday schedules as long as they are schedules with a lettered route
section. However, if an employee is scheduled on Monday with a route section
of '*', he can not be scheduled on any more routes. The resultset I am
looking for are those employees who are available to be scheduled.

Thanks to anyone who understands my jibberish and can help with a query.

Eric


Back to top
Del Murray
Guest





PostPosted: Mon May 10, 2004 7:27 pm    Post subject: Re: Help with a query Reply with quote



Eric,

Break it down in smaller pieces and use the "UNION" statement to do a select
of one group, then union it to the select of the next group, etc. Use the
"EXITS" statement to include (or not include) a record or condition that
'exists' (duh) that would effect that row being selected or not.


Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Mon May 10, 2004 9:02 pm    Post subject: Re: Help with a query Reply with quote



Eric Stewart wrote:
Quote:
I have two tables. Showing only the necessary info, they look like
this

rsiEmployeeMaster (T1)
AreaID varchar(20)
Empnum int
EmpName varchar(31)
MaintSite varchar(20)
Shift varchar(20)

rsiScheduleMasterEmployee (T2)
AreaID varchar(20)
Empnum int
ScheduledDay smallint
RouteSection varchar(2)

Any employee with the specified AreaID, MaintSite, Shift with no
record in T2 for ScheduledDay.

This much can be done with either a left outer join or subselect, depending
on the database the left join is often faster.

select t1.* from T1 t1
left outer join T2 t2
on t1.areaid = t2.areaid and t1.empnum=t2.empnum
where t1.areaid = :areaid and t1.maintsite =:maintsite and t1.shift = :shift
and t2.areaid is null
..
Quote:
Any employee with the specified AreaID, MaintSite, Shift,
ScheduledDay with a record in T2 that meets this condition:
RouteSection is any letter between A and Z but not the same as
the input parameter supplied (RouteSection)
Also, if the employee has a record in T2 with the specified AreaID,
MaintSite, Shift, ScheduledDay with a RouteSection of '*', he should
not be included in the result set.

This would be another select unioned with the first.

union
select t1.* T1 t1
where exists
(select 1 from T2 t2
where t1.areaid = t2.areaid and t1.empnum=t2.empnum
)
where t1.areaid = :areaid and t1.maintsite =:maintsite and t1.shift = :shift
and scheduledday = :scheduledday
and routesection between 'A' and 'Z' and routesection <> :routesection

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"True peace is not the absence of tension, but the presence of
justice." - Martin Luther King, Jr.



Back to top
Eric Stewart
Guest





PostPosted: Mon May 10, 2004 10:01 pm    Post subject: Re: Help with a query Reply with quote

Thanks Wayne. Will give it a try.

Eric

"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> wrote

Quote:
Eric Stewart wrote:
I have two tables. Showing only the necessary info, they look like
this

rsiEmployeeMaster (T1)
AreaID varchar(20)
Empnum int
EmpName varchar(31)
MaintSite varchar(20)
Shift varchar(20)

rsiScheduleMasterEmployee (T2)
AreaID varchar(20)
Empnum int
ScheduledDay smallint
RouteSection varchar(2)

Any employee with the specified AreaID, MaintSite, Shift with no
record in T2 for ScheduledDay.

This much can be done with either a left outer join or subselect,
depending
on the database the left join is often faster.

select t1.* from T1 t1
left outer join T2 t2
on t1.areaid = t2.areaid and t1.empnum=t2.empnum
where t1.areaid = :areaid and t1.maintsite =:maintsite and t1.shift =
:shift
and t2.areaid is null
.
Any employee with the specified AreaID, MaintSite, Shift,
ScheduledDay with a record in T2 that meets this condition:
RouteSection is any letter between A and Z but not the same as
the input parameter supplied (RouteSection)
Also, if the employee has a record in T2 with the specified AreaID,
MaintSite, Shift, ScheduledDay with a RouteSection of '*', he should
not be included in the result set.

This would be another select unioned with the first.

union
select t1.* T1 t1
where exists
(select 1 from T2 t2
where t1.areaid = t2.areaid and t1.empnum=t2.empnum
)
where t1.areaid = :areaid and t1.maintsite =:maintsite and t1.shift =
:shift
and scheduledday = :scheduledday
and routesection between 'A' and 'Z' and routesection <> :routesection

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"True peace is not the absence of tension, but the presence of
justice." - Martin Luther King, Jr.





Back to top
Eric Stewart
Guest





PostPosted: Mon May 10, 2004 10:01 pm    Post subject: Re: Help with a query Reply with quote

Thanks Del.

Eric

"Del Murray" <Del.Murray (AT) N_S_CreditHawk (DOT) Net> wrote

Quote:
Eric,

Break it down in smaller pieces and use the "UNION" statement to do a
select
of one group, then union it to the select of the next group, etc. Use the
"EXITS" statement to include (or not include) a record or condition that
'exists' (duh) that would effect that row being selected or not.





Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.