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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Problem with query

Author  Topic 

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-10 : 07:10:14
I have a location and one location having person with their designation.
List of table which i use to get data is
1 Location
2 Person
3 LocationPerson
4 LocationPersonDesignation
5 Designation
Link like this

LocationPerson contain the ID of location that means one location having multiple person and loactionPersonDesignation contain ID of locatiionPerson and Designation.

This means that one location have more than one person with same designation.
Suppose designation is Manager, Asst. Manager

What I need to do is to show my result like this

Location--------------------Manager---------Asst. Manager
1001-------------------john--------------------Mark
1001---------------------Joe-------------------Dev
1002---------------------ASC--------------------ADD

Please help me

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-10 : 18:49:40
Can you post your table structure and sample data ?

-----------------
'KH'

Go to Top of Page

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-11 : 00:48:22
table structure
Location
-----LocationID
-----LocationName
Person
-----PersonID
-----Name
Designation
-----DesignationID
-----Description
LocationPerson
-----LocationPersonID
-----LocationID
-----PersonID
LocationPersonDesignation
-----LocationPersonDesignationID
-----LocationPersonID
-----DesignationID

Sample Data
Location
LocationID------LocationName
1----------1001
2----------1002
Person
PersonID--------Name
1---------ABC
2---------XYZ
3---------PQR
Designation
DesignationID--------Description
1---------Manager
2---------Asst.Manager
LocationPerson
LocationPersonID------LocationID--------PersonID
1---------------------1------------------1
2---------------------1------------------2
3---------------------1-------------------3

LocationPersonDesgination

LocationPersonDesgination---LocationPersonID------DesignationID
1--------------------1-------------------------------1
2--------------------2--------------------------------1
3--------------------3----------------------------2
thanks for ur respose plz help me is urgent

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-11 : 01:18:21
Try this . . .
create table #Location
( LocationID int,
LocationName varchar(30)
)

create table #Person
(
PersonID int,
Name varchar(30)
)

create table #Designation
(
DesignationID int,
Description varchar(30)
)

create table #LocationPerson
(
LocationPersonID int,
LocationID int,
PersonID int
)

create table #LocationPersonDesignation
(
LocationPersonDesignationID int,
LocationPersonID int,
DesignationID int
)

insert into #Location
select 1, '1001' union all
select 2, '1002'

insert into #Person
select 1, 'ABC' union all
select 2, 'XYZ' union all
select 3, 'PQR'

insert into #Designation
select 1, 'Manager' union all
select 2, 'Asst.Manager'

insert into #LocationPerson
select 1, 1, 1 union all
select 2, 2, 2 union all
select 3, 1, 3

insert into #LocationPersonDesignation
select 1, 1, 1 union all
select 2, 2, 1 union all
select 3, 3, 2

select l.LocationName,
max(case when lpd.DesignationID = 1 then p.Name else '' end) as 'Manager',
max(case when lpd.DesignationID = 2 then p.Name else '' end) as 'Asst. Manager'
from #LocationPersonDesignation lpd inner join #LocationPerson lp
on lpd.LocationPersonID = lp.LocationPersonID
inner join #Location l
on lp.LocationID = l.LocationID
inner join #Person p
on lp.PersonID = p.PersonID
group by l.LocationName

drop table #Location
drop table #Person
drop table #Designation
drop table #LocationPerson
drop table #LocationPersonDesignation

In future, try to post your tables structure & data like this. It is easier for us to test the codes

-----------------
'KH'

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.
Go to Top of Page

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-11 : 08:08:16
thanks and i will give a sample data in future
I face one problem
If one location contain more then one persons with both designation then data is coming like this

1001---ABC-----NULL
1001---NULL----PQR
1001---XYZ------NULL

i want somthing like this
1001---ABC-----PQR
1001---XYZ----NULL
would it possible
Plz help me
Go to Top of Page

sanjay_jadam
Yak Posting Veteran

53 Posts

Posted - 2006-01-17 : 09:04:10
Please help me for finding above result
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-17 : 19:51:11
Can't think of any solution for this. Maybe someone else can help

-----------------
'KH'

Go to Top of Page
   

- Advertisement -