Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Write a SQL statement

Author  Topic 

Brook11234
Starting Member

5 Posts

Posted - 2009-08-18 : 13:52:17
I am trying to figure out this query. If I use the EXISTS Keyword with a JOIN in a subquery along with a CASE statment, but I am really baffled as to how to get the right query.
If I can use the inner query to write a statement.Please help me out!

Please write a SQL statement based on the information below that retrieves the history of each position grouped by organization. If the position is currently vacant, display the number of months the position has been vacant instead of the employee’s name. If the position has always been vacant, display that the position has never been filled instead of the employee’s name.

In database VPositions, there are two tables called Positions and PersonsPositions:

Positions
----------
PositionID
PositionTitle
OrganizationID

PersonPositions
----------------
PersonPositionID
PersonID
PositionID
AssignmentStartDate
AssignmentEndDate

In database VPersons, there is a table called Persons:

Persons
--------
PersonID
LastName
FirstName

In database VOrganizations, there is a table called Organizations:

Organizations
--------------
OrganizationID
OrganizationName

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-08-18 : 13:58:55
What have you tried so far?
Go to Top of Page

Brook11234
Starting Member

5 Posts

Posted - 2009-08-18 : 14:12:54
quote:
Originally posted by vijayisonly

What have you tried so far?


I try with inner query but it not working, please help me
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-08-18 : 14:13:45
Show us your query...along with some with sample data and expected output.
Go to Top of Page

Brook11234
Starting Member

5 Posts

Posted - 2009-08-18 : 14:17:57
Organization
Position
Employee Name or Vacancy
Assignment Start Date
Assignment End Date

Oklahoma City VAMC
Director
Mouse, Mickey
1/1/07
1/1/09

Oklahoma City VAMC
Director
Position vacant for 5 months



Oklahoma City VAMC
Associate Director
Position has never been filled



Muskogee VAMC
Director
Duck, Donald
1/1/08
1/1/09

Muskogee VAMC
Director
Woman, Wonder
1/2/09





Go to Top of Page

Brook11234
Starting Member

5 Posts

Posted - 2009-08-18 : 14:25:17
This a result table and columns
Organization
Position
Employee Name or Vacancy
Assignment Start Date
Assignment End Date

Here are the results belongs to each columns

Oklahoma City VAMC
Director
Mouse, Mickey
1/1/07
1/1/09

Oklahoma City VAMC
Director
Position vacant for 5 months



Oklahoma City VAMC
Associate Director
Position has never been filled



Muskogee VAMC
Director
Duck, Donald
1/1/08
1/1/09

Muskogee VAMC
Director
Woman, Wonder
1/2/09


Go to Top of Page

Brook11234
Starting Member

5 Posts

Posted - 2009-08-18 : 14:27:46
Any one can help?
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-08-18 : 15:34:50
Sample Data

declare @positions table 
(positionid int, positiontitle varchar(20),organizationid int)

insert @positions
select 1,'director',1 union all
select 2,'asst director', 1 union all
select 3,'employee',1

declare @PersonPositions table
(
PersonPositionID int,
PersonID int,
PositionID int,
AssignmentStartDate datetime,
AssignmentEndDate datetime
)
insert @PersonPositions
select 1,1,1,'2009-01-01','2009-06-01' union all
select 3,3,3,'2009-01-01','2009-12-31'

declare @Persons table
(
PersonID int,
LastName varchar(20),
FirstName varchar(20)
)

insert @Persons
select 1,'director','first' union all
select 3,'employee','first'

declare @Organizations table
(
OrganizationID int,
OrganizationName varchar(20)
)
insert @Organizations
select 1,'OrgOne'


Query

SELECT d.OrganizationName, 
a.positiontitle,
CASE
WHEN b.PersonID IS NULL
THEN 'Position has never been filled'
ELSE CASE
WHEN datediff(mm,getdate(),b.AssignmentEndDate) > 0
THEN c.FirstName + ' ' + c.LastName
ELSE 'Position vacant for ' + cast(datediff(mm,b.AssignmentEndDate,getdate()) AS CHAR(4)) + 'months '
END
END,
b.AssignmentStartDate,
b.AssignmentEndDate
FROM @positions a
LEFT JOIN @PersonPositions b
ON a.positionid = b.PositionID
LEFT JOIN @Persons c
ON b.PersonID = c.PersonID
LEFT JOIN @Organizations d
ON a.organizationid = d.OrganizationID


Result

OrganizationName     positiontitle                                                  AssignmentStartDate     AssignmentEndDate
-------------------- -------------------- ----------------------------------------- ----------------------- -----------------------
OrgOne director Position vacant for 2 months 2009-01-01 00:00:00.000 2009-06-01 00:00:00.000
OrgOne asst director Position has never been filled NULL NULL
OrgOne employee first employee 2009-01-01 00:00:00.000 2009-12-31 00:00:00.000
Go to Top of Page
   

- Advertisement -