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)
 Help in SQL query

Author  Topic 

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-18 : 19:17:32
Hello All,

I have following tables:-

CREATE TABLE #EMPLOYEE(EMPID INT,NAME VARCHAR(10))

INSERT INTO #EMPLOYEE
SELECT 1,'PETER'
UNION ALL
SELECT 2,'JOHN'
UNION ALL
SELECT 3,'NINA'
UNION ALL
SELECT 4,'JOSH'
UNION ALL
SELECT 5,'CHRIS'
UNION ALL
SELECT 6,'NATHAN'
UNION ALL
SELECT 7,'RAJ'
UNION ALL
SELECT 8,'ALICE'


CREATE TABLE #JOB(JOBID INT,EMPID INT,JOBLEVEL VARCHAR(1))
INSERT INTO #JOB
SELECT 1,1,'A'
UNION ALL
SELECT 1,2,'B'
UNION ALL
SELECT 1,3,'B'
UNION ALL
SELECT 2,4,'A'
UNION ALL
SELECT 2,5,'B'
UNION ALL
SELECT 2,6,'B'
UNION ALL
SELECT 3,7,'B'
UNION ALL
SELECT 3,8,'B'

DROP TABLE #EMPLOYEE
DROP TABLE #JOB


Notice:
1. Job 3 dont have anybody on level 'A' in table #JOB.
2. it means anybody from level 'B' is performing duties of level 'A'.
3. Many people are working on each job at level 'B'.

If thre is nobody at level 'A'
I want to sort people at level 'B' alphabetically and show first in sorted list under 'LEVELA' column and
others under 'LEVELB' column

Results will be:

JOB LEVELA LEVELB
1 PETER JOHN,NINA
2 JOSH CHRIS, NATHAN
3 ALICE RAJ

Thanks for your help


mk_garg

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-08-18 : 19:29:59
Have a look at the following article:

http://www.sqlteam.com/item.asp?ItemID=11021
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-18 : 21:19:36
Hi,
I already checked that.
I does not solve my requirement.

Thanks

mk_garg
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-18 : 21:34:51
Allright!

Here are new structure
1. #JOB Table splitted into 2 tables according to job level.
2. Removed #EMPLOYEE as i need names in result.

worked bit on solution given on above link.
But i have not achieved desired results.

Please help me!



CREATE TABLE #JOBA(JOBID INT,EMPNAME varchar(10))
INSERT INTO #JOBA
SELECT 1,'PETER'
UNION ALL
SELECT 2,'JOSH'

CREATE TABLE #JOBB(JOBID INT,EMPNAME varchar(10))
INSERT INTO #JOBB
SELECT 1,'JOHN'
UNION ALL
SELECT 1,'NINA'
UNION ALL
SELECT 2,'CHRIS'
UNION ALL
SELECT 2,'NATHAN'
UNION ALL
SELECT 3,'RAJ'
UNION ALL
SELECT 3,'ALICE'

select * from #JOBA
select * from #JOBB

create table #workingtable (
JOBID int,
EMPNAME varchar(10),
list varchar(8000))

insert into #workingtable (JOBID,EMPNAME)
select JOBID,EMPNAME
from #JOBB
order by JOBID,EMPNAME

declare
@list varchar(8000),
@lasti int

select
@list = '',
@lasti = -1

--here is the meat of the work
update
#workingtable
set
@list = list = case
when @lasti <> JOBID then EMPNAME
else @list + ', ' + EMPNAME
end,
@lasti = JOBID

--return a sample from the final rowset
select JOBID, max(list) as list
from
#workingtable
group by
JOBID
order by
JOBID
go



DROP TABLE #JOBA
DROP TABLE #JOBB
DROP TABLE #workingtable

/* results i need is here
JOB LEVELA LEVELB
1 PETER JOHN,NINA
2 JOSH CHRIS, NATHAN
3 ALICE RAJ
*/

Thanks


mk_garg
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-18 : 22:17:13
Try this out:


CREATE TABLE #EMPLOYEE(EMPID INT,NAME VARCHAR(10))

INSERT INTO #EMPLOYEE
SELECT 1,'PETER'
UNION ALL
SELECT 2,'JOHN'
UNION ALL
SELECT 3,'NINA'
UNION ALL
SELECT 4,'JOSH'
UNION ALL
SELECT 5,'CHRIS'
UNION ALL
SELECT 6,'NATHAN'
UNION ALL
SELECT 7,'RAJ'
UNION ALL
SELECT 8,'ALICE'


CREATE TABLE #JOB(JOBID INT,EMPID INT,JOBLEVEL VARCHAR(1), processed bit default(0))
INSERT INTO #JOB
SELECT 1,1,'A',0
UNION ALL
SELECT 1,2,'B',0
UNION ALL
SELECT 1,3,'B',0
UNION ALL
SELECT 2,4,'A',0
UNION ALL
SELECT 2,5,'B',0
UNION ALL
SELECT 2,6,'B',0
UNION ALL
SELECT 3,7,'B',0
UNION ALL
SELECT 3,8,'B',0


Select * from #employee
Select * from #job

Create Table #final (jobId int, A varchar(1000), B varchar(1000))

Insert Into #final
Select
jobId,
A = (Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'A'),
B = (Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'B')
From (Select Distinct JobId From #job) as A


Update #job
Set
processed = 1
From #job
Inner Join
(
Select
jobId,
JobLevel = 'A',
name = (Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'A')
From (Select Distinct JobId From #job) as A
Union
Select
jobId,
JobLevel = 'B',
name = (Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'B')
From (Select Distinct JobId From #job) as A
) as Z
On #job.jobId = z.Jobid
and #job.empId = (Select empId From #employee Where name = z.name)
and #job.jobLevel = z.joblevel
and #job.processed = 0

while exists(Select 1 From #job where processed=0)
Begin
Update #final
Set
A = isnull(A,'') + isnull(','+(Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'A'),''),
B = isnull(B,'') + isnull(','+(Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'B'),'')
From #final as A


Update #job
Set
processed = 1
From #job
Inner Join
(
Select
jobId,
JobLevel = 'A',
name = (Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'A')
From (Select Distinct JobId From #job) as A
Union
Select
jobId,
JobLevel = 'B',
name = (Select min(name) From #employee as z Inner Join #job as y On z.empId = y.empId Where jobId = A.jobId and processed = 0 and JobLevel = 'B')
From (Select Distinct JobId From #job) as A
) as Z
On #job.jobId = z.Jobid
and #job.empId = (Select empId From #employee Where name = z.name)
and #job.jobLevel = z.joblevel
and #job.processed = 0
End

Select * From #final

DROP TABLE #EMPLOYEE
DROP TABLE #JOB
Drop Table #final


Corey
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-18 : 22:48:47
Corey,
Your solution is very close to my requirements.

Result from you Query:-
JOBID A B
---------------------------
1 PETER JOHN,NINA
2 JOSH CHRIS,NATHAN
3 ALICE,RAJ
---------------------------

But For job 3 A is blank so it should be
JOBID A B
---------------------------
1 PETER JOHN,NINA
2 JOSH CHRIS,NATHAN
3 ALICE RAJ
---------------------------

Thanks


mk_garg
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-18 : 22:52:12
I mean ALICE should be under column A and RAJ under Column B
Thanks


mk_garg
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-18 : 23:16:44
add this right after the end of the loop


...
End

Update #final
Set
A = case when charindex(',',B)>0 then left(B,charindex(',',B)-1) else B end,
B = case when charindex(',',B)>0 then right(B,len(B) - charindex(',',B)) else null end
From #final
Where A = ''

Select * From #final
...


Corey
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-18 : 23:19:49
Perfect!
Thanks Corey

mk_garg
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-19 : 01:57:34
Corey when i am running it on my database. it is very slow.
I guess due to sub queries.
Any better alternative.


ehorn
i worked on link provided by you.
some how it is not generating CSV string.

I posted that as another topic
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=38817



mk_garg
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-08-19 : 02:13:32
I suspect I missed something in this question because Corey is usually right on the money, but how about this?


CREATE FUNCTION f_emps (@JOBID int) RETURNS Varchar(2000)
AS
BEGIN
DECLARE @Emplist varchar(2000)

SELECT @Emplist = CASE WHEN @Emplist IS NULL THEN NAME + ' ' ELSE @Emplist + ',' + NAME End
FROM EMPLOYEE, JOB
WHERE @JOBID = JOB.JOBID
AND JOB.EMPID = EMPLOYEE.EMPID
ORDER BY JOB.JOBLEVEL, EMPLOYEE.NAME

RETURN Replace(@Emplist,' ,',' ')
END

SELECT JOBID, Cast (Substring(AB,1,Charindex(' ',AB)) AS Varchar(10)) A, Rtrim(Substring(AB,Charindex(' ',AB) + 1 ,255)) B
FROM (
SELECT JOBID, dbo.f_emps(JOBID) AB
FROM JOB
GROUP BY JOBID
) x

JOBID A B
----------- ---------- -------------
1 PETER JOHN,NINA
2 JOSH CHRIS,NATHAN
3 ALICE RAJ

Or if you don't need to seperate names:

SELECT JOBID, dbo.f_emps(JOBID) AB
FROM JOB
GROUP BY JOBID

JOBID AB
----------- ----------------
1 PETER JOHN,NINA
2 JOSH CHRIS,NATHAN
3 ALICE RAJ


--Ken
"Knowledge is a process of piling up facts; wisdom lies in their simplification."
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-19 : 02:21:34
Bad Luck

Running SQL Server 7.0
Can use functions

Thanks

mk_garg
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-08-19 : 02:35:33
Then you can do it with a temp table. This is probably close to what you tried to do already.

SELECT JOBID, Cast (NAME AS VARCHAR(255)) NAME
INTO #workingtable
FROM EMPLOYEE, JOB
WHERE JOB.EMPID = EMPLOYEE.EMPID
ORDER BY JOB.JOBID DESC, JOB.JOBLEVEL, EMPLOYEE.NAME


DECLARE @jobid int
DECLARE @name varchar(255)

UPDATE #workingtable
SET @Name = NAME = Case When @jobid = Jobid Then @name + ',' + NAME Else NAME End,
@jobid = jobid

SELECT JOBID, Cast (Substring(AB,1,Charindex(',',AB) - 1) AS Varchar(10)) A, Rtrim(Substring(AB,Charindex(',',AB) + 1 ,255)) B
FROM (
select Jobid, Max(name) AB
from #workingtable
group by jobid
) x


DROP TABLE #workingtable

JOBID A B
----------- ---------- ------------
1 PETER JOHN,NINA
2 JOSH CHRIS,NATHAN
3 ALICE RAJ


--Ken
"Knowledge is a process of piling up facts; wisdom lies in their simplification."
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-19 : 03:07:48
Thanks Ken!

mk_garg
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-19 : 09:05:02
Ken - Thanks for the vote of confidence! I may be wrong, but it seems that your query is very specific to the example data.

For example:
- what would happen if the data was not listed in the correct order (ie 'B' before 'A')
- what would happen if more than one entry was listed for level 'A'
- what would happen if there is only 1 person list for 'B' and none for 'A'

mk_garg - Sorry it was slow, I could probably fix it, but if you are satisfied then I won't bother...


Corey
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-08-19 : 12:47:31
quote:
Originally posted by Seventhnight
- what would happen if the data was not listed in the correct order (ie 'B' before 'A')
for 'A'


I don't think that would matter because #workingtable is created with ORDER BY JOB.JOBID DESC, JOB.JOBLEVEL, EMPLOYEE.NAME
quote:
Originally posted by Seventhnight
- what would happen if more than one entry was listed for level 'A'
for 'A'


It would take the first one alphabetically as colum A, the others would be in the colum B list.
quote:
Originally posted by Seventhnight
- what would happen if there is only 1 person list for 'B' and none for 'A'


There would be only 1 name for column A and none for B but what else can you do?
At least that's how I think it would work without trying it

--Ken
"Knowledge is a process of piling up facts; wisdom lies in their simplification."
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-08-19 : 18:13:22
quote:
Originally posted by Seventhnight

Ken - Thanks for the vote of confidence!
Corey


Its not like that. Ken's solution is working. but i could not implement on my project. I used code send by you.

I am thankful to both of you.



mk_garg
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-19 : 23:54:50
Well I'm glad you got it working, but I was merely thanking ken for the compliment
quote:
...because Corey is usually right on the money...


Glad we could help!


Corey
Go to Top of Page
   

- Advertisement -