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.
| 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 #EMPLOYEESELECT 1,'PETER'UNION ALLSELECT 2,'JOHN'UNION ALLSELECT 3,'NINA'UNION ALLSELECT 4,'JOSH'UNION ALLSELECT 5,'CHRIS'UNION ALLSELECT 6,'NATHAN'UNION ALLSELECT 7,'RAJ'UNION ALLSELECT 8,'ALICE'CREATE TABLE #JOB(JOBID INT,EMPID INT,JOBLEVEL VARCHAR(1))INSERT INTO #JOBSELECT 1,1,'A'UNION ALLSELECT 1,2,'B'UNION ALLSELECT 1,3,'B'UNION ALLSELECT 2,4,'A'UNION ALLSELECT 2,5,'B'UNION ALLSELECT 2,6,'B'UNION ALLSELECT 3,7,'B'UNION ALLSELECT 3,8,'B'DROP TABLE #EMPLOYEEDROP TABLE #JOBNotice: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 andothers under 'LEVELB' columnResults will be:JOB LEVELA LEVELB1 PETER JOHN,NINA2 JOSH CHRIS, NATHAN3 ALICE RAJThanks for your helpmk_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 |
 |
|
|
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.Thanksmk_garg |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-08-18 : 21:34:51
|
| Allright!Here are new structure1. #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 #JOBASELECT 1,'PETER'UNION ALLSELECT 2,'JOSH' CREATE TABLE #JOBB(JOBID INT,EMPNAME varchar(10))INSERT INTO #JOBBSELECT 1,'JOHN' UNION ALLSELECT 1,'NINA'UNION ALLSELECT 2,'CHRIS'UNION ALLSELECT 2,'NATHAN'UNION ALLSELECT 3,'RAJ'UNION ALLSELECT 3,'ALICE'select * from #JOBAselect * from #JOBBcreate table #workingtable ( JOBID int, EMPNAME varchar(10), list varchar(8000))insert into #workingtable (JOBID,EMPNAME)select JOBID,EMPNAMEfrom #JOBBorder by JOBID,EMPNAMEdeclare @list varchar(8000), @lasti intselect @list = '', @lasti = -1--here is the meat of the workupdate #workingtableset @list = list = case when @lasti <> JOBID then EMPNAME else @list + ', ' + EMPNAME end, @lasti = JOBID--return a sample from the final rowsetselect JOBID, max(list) as listfrom #workingtablegroup by JOBIDorder by JOBIDgoDROP TABLE #JOBADROP TABLE #JOBBDROP TABLE #workingtable/* results i need is hereJOB LEVELA LEVELB1 PETER JOHN,NINA2 JOSH CHRIS, NATHAN3 ALICE RAJ*/Thanksmk_garg |
 |
|
|
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 #EMPLOYEESELECT 1,'PETER'UNION ALLSELECT 2,'JOHN'UNION ALLSELECT 3,'NINA'UNION ALLSELECT 4,'JOSH'UNION ALLSELECT 5,'CHRIS'UNION ALLSELECT 6,'NATHAN'UNION ALLSELECT 7,'RAJ'UNION ALLSELECT 8,'ALICE'CREATE TABLE #JOB(JOBID INT,EMPID INT,JOBLEVEL VARCHAR(1), processed bit default(0))INSERT INTO #JOBSELECT 1,1,'A',0UNION ALLSELECT 1,2,'B',0UNION ALLSELECT 1,3,'B',0UNION ALLSELECT 2,4,'A',0UNION ALLSELECT 2,5,'B',0UNION ALLSELECT 2,6,'B',0UNION ALLSELECT 3,7,'B',0UNION ALLSELECT 3,8,'B',0Select * from #employeeSelect * from #jobCreate 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 = 0while 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 = 0EndSelect * From #finalDROP TABLE #EMPLOYEEDROP TABLE #JOBDrop Table #finalCorey |
 |
|
|
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,NINA2 JOSH CHRIS,NATHAN3 ALICE,RAJ---------------------------But For job 3 A is blank so it should beJOBID A B---------------------------1 PETER JOHN,NINA2 JOSH CHRIS,NATHAN3 ALICE RAJ---------------------------Thanksmk_garg |
 |
|
|
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 BThanksmk_garg |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-18 : 23:16:44
|
add this right after the end of the loop...EndUpdate #finalSet 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 endFrom #finalWhere A = ''Select * From #final...Corey |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-08-18 : 23:19:49
|
| Perfect!Thanks Coreymk_garg |
 |
|
|
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.ehorni worked on link provided by you.some how it is not generating CSV string.I posted that as another topichttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=38817mk_garg |
 |
|
|
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)ASBEGINDECLARE @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.NAMERETURN Replace(@Emplist,' ,',' ')ENDSELECT 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 ) xJOBID A B ----------- ---------- -------------1 PETER JOHN,NINA2 JOSH CHRIS,NATHAN3 ALICE RAJOr if you don't need to seperate names: SELECT JOBID, dbo.f_emps(JOBID) ABFROM JOBGROUP BY JOBIDJOBID AB ----------- ----------------1 PETER JOHN,NINA2 JOSH CHRIS,NATHAN3 ALICE RAJ--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-08-19 : 02:21:34
|
| Bad LuckRunning SQL Server 7.0Can use functionsThanksmk_garg |
 |
|
|
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)) NAMEINTO #workingtableFROM EMPLOYEE, JOBWHERE JOB.EMPID = EMPLOYEE.EMPIDORDER BY JOB.JOBID DESC, JOB.JOBLEVEL, EMPLOYEE.NAMEDECLARE @jobid intDECLARE @name varchar(255)UPDATE #workingtableSET @Name = NAME = Case When @jobid = Jobid Then @name + ',' + NAME Else NAME End,@jobid = jobidSELECT 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 ) xDROP TABLE #workingtableJOBID A B ----------- ---------- ------------1 PETER JOHN,NINA2 JOSH CHRIS,NATHAN3 ALICE RAJ--Ken"Knowledge is a process of piling up facts; wisdom lies in their simplification." |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-08-19 : 03:07:48
|
| Thanks Ken!mk_garg |
 |
|
|
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 |
 |
|
|
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.NAMEquote: 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." |
 |
|
|
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 |
 |
|
|
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 complimentquote: ...because Corey is usually right on the money...
Glad we could help! Corey |
 |
|
|
|
|
|
|
|