473,789 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help needed in SQL Query for Hierarchy

I would like to seek your expert opinions on the following problem of mine:

I have a table to store employees

Here is the partial structure of the table

EMPID
EMP_POS
DATE
MGMT
BOSS_POS

The data looks like this:

EMPLID EMP_POS DATE FLAG BOSS_POS
z001 p009 01/01/2000 N p008
z001 p019 03/11/1999 N p031
z001 p029 11/30/1998 N p085

y001 p008 06/13/2001 N p007
y001 p031 08/23/2000 N p022
y001 p054 01/08/1998 N p045

x001 p007 09/23/2000 Y p006

w001 p006 03/11/1998 N p005
....
....
....

It is evident that this stores the positions that an employee is currently in
and has been. For example, employee z001 is in position p009 as that is the
most recent record and has previously been in positions p019 and p029.

This is true for all employees with the number of positions varying from 1 to
many.

I am working on a query that returns the EMPLID that has the flag set to Y for
a given input EMPLID ie. For input EMPLID = 'z001', the query should return
EMPLID = 'x001' even when 'w001' is the Boss of 'x001' so is the Boss of 'z001'
The level at which the FLAG is set is not known so the query has to traverse
till it finds the record with FLAG set or return the record from the result
after traversing the whole tree right up to the top.

In writing this query I have to take the record of the employee that has the
most recent date.

Attempted Solution
I thought of creating a view that would return me the current position of each
employee and then using 'CONNECT BY PRIOR' to traverse the hierarchy.
View
CREATE VIEW the_view AS
SELECT A.EMP_POS, A.EMPLID
FROM the_table A
WHERE A.EMP_POS <> ' '
AND A.DATE = (
SELECT MAX(C.DATE)
FROM the_table C
WHERE C.EMPLID = A.EMPLID
AND C.DATE <= SYSDATE
)

select EMPLID, EMP_POS, FLAG
from the_view
connect by prior EMP_POS = BOSS_POS
start with EMP_POS = 'z001'

But then CONNECT BY PRIOR does not accept sub-queries
Secondly, I would have to use the above statement as
an inline view to filter the result to return the
record where FLAG = 'Y'

What could be the most efficient way of doing this?
Thanking you in advance for your suggestions.
Jul 19 '05 #1
1 6549
Try:

select level
,a.emplid
,a.emp_pos
,a.flag
,a.boss_pos
from temp a
where a.date = (select max(c.date)
from temp c
where c.emplid = a.emplid)
and flag = 'Y'
connect by prior boss_pos=emp_po s
start with emplid = 'z001'

It works with the data you supplied (on a 9i database). I don't know
if this is the most efficient way (sub-selects are always pretty
slow).
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1599
by: knoak | last post by:
Hi there, I'm building a website about animals. There is a mySQL DB behind it. There are 2 tables: 1 - species 2 - animals Species could be: birds, fishes etc. Animals would be: hawk, eagle etc, and orca, whale etc. So every animal belongs to one of the species.
0
4121
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting together a query to pull out an alternative hierarchical view of my data. The database is implemented under SQL Sever 2000 and I am writing the front end using VB.Net and ADO.net. The following is the portion of my database structure that I am...
1
2105
by: joey | last post by:
I have a SQL query where I get the count of something, but what I really want is a yes/no (boolean) answer. Here is my query: SELECT COUNT(*) AS COUNT FROM hd_entries,hierarchy WHERE IFNULL (BETWEEN_TIMES (b_yr, b_day, b_msec, e_yr, e_day, e_msec, 2001, 31, 0, 2001, 31, 63720000) >0, 1) AND hd_entries.data_key=hierarchy.data_key AND hierarchy.project='CLUSTERII' AND hierarchy.mission='CLUSTER-4' AND hierarchy.experiment='PEACE';
3
3740
by: Brent Taylor via AccessMonster.com | last post by:
I am working on a MS Access database to track the sales of a 3 Tier Multi- level Marketing system. I have created a table to track a new RepID and a SponsorID. I have created a query that will show the Sponsor and all of their reps but I am not sure how to show the reps that the second tier has sponsored. Example: RepID 0011 SponsorID 0010
0
1253
by: Henry | last post by:
I have a dataset dsMain in which I have two tables The table period has columns like period_id, name, root_org_id.... The table organization has org_id, name, parent_id, hier_path, level, org_index, and period_id. There is a one to many relationship between period and organization, but the organization is actually a hierarchy. period.root_or_id represents the org_id of the root node of the organization hierarchy and that organization...
53
4758
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating records that have a '99' in them (it is similar to a CSV file). Some of my concerns are:
31
2799
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed Python programs into optimized C++, and hence, highly optimized machine language. Besides many bug fixes and optimizations, these releases add the following changes: -support for 'bisect', 'collections.deque' and 'string.maketrans'
1
249
by: shilsum | last post by:
I would like to seek your expert opinions on the following problem of mine: I have a table to store employees Here is the partial structure of the table EMPID EMP_POS DATE MGMT
3
2756
by: IAuditor | last post by:
Access 2003, XP - I have a multi-table query that is losing data. 1 table is a lookup (Hierarchy), and the other 3 are virtual duplicates (all with a one-to-many relationship with the lookup) except for some field names. The Hierarchy field is identically named in all 3 tables. The query starts with the Hierarchy number, then descriptions, etc. Next are 2 long-integer fields that are used to calculate a Difference and %ofDifference fields. Up...
0
9661
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10403
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7524
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6755
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5414
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4087
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2904
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.