|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-06-08 : 08:02:50
|
| Declan writes "Please run sample below.What I am trying to achieve is a single row as the result. I show a "workaround" at the bottom. Is ther an easier/better way?Thanks,Declan WardAwaitingCheck AwaitingResults==============||=================AA BBIs this possible?drop table #tempcreate table #temp( jobnumber char(10), StatusID int, Initials char(5))GOinsert into #temp values ('04G00020', 1, 'AA')insert into #temp values ('04G00020', 2, '')insert into #temp values ('04G00020', 3, '')insert into #temp values ('04G00020', 4, 'BB')insert into #temp values ('04G00020', 5, '')insert into #temp values ('04G00020', 6, '')insert into #temp values ('04G00021', 1, '')insert into #temp values ('04G00021', 2, '')insert into #temp values ('04G00021', 3, '')insert into #temp values ('04G00021', 4, '')insert into #temp values ('04G00021', 5, '')insert into #temp values ('04G00021', 6, '')GOselect * from #tempwhere (JobNumber = '04g00020')AND (StatusID = 1 OR StatusID = 4)GOSelect (case StatusID WHEN 1 then Initials end) As AwaitingCheck,(case StatusID WHEN 4 then Initials end) As AwaitingResultsFROM #tempwhere(JobNumber = '04g00020')AND (StatusID = 1 OR StatusID = 4)GOThis is my workaround: Is there a better way???====================================--Workaround !!!create table #temp( jobnumber char(10), StatusID int, Initials char(5))GOinsert into #temp values ('04G00020', 1, 'AA')insert into #temp values ('04G00020', 2, '')insert into #temp values ('04G00020', 3, '')insert into #temp values ('04G00020', 4, 'BB')insert into #temp values ('04G00020', 5, '')insert into #temp values ('04G00020', 6, '')insert into #temp values ('04G00021', 1, '')insert into #temp values ('04G00021', 2, '')insert into #temp values ('04G00021', 3, '')insert into #temp values ('04G00021', 4, '')insert into #temp values ('04G00021', 5, '')insert into #temp values ('04G00021', 6, '')GODECLARE check_cursor CURSOR FOR SELECT Initials FROM #temp WHERE (JobNumber = '04g00020')AND (StatusID = 1) OPEN check_cursor DECLARE @CheckInitials char(5) FETCH NEXT FROM check_cursor INTO @CheckInitialsDECLARE result_cursor CURSOR FOR SELECT Initials FROM #temp WHERE (JobNumber = '04g00020')AND (StatusID = 4) OPEN result_cursor DECLARE @ResultInitials char(5) FETCH NEXT FROM result_cursor INTO @ResultInitialscreate table #temp2( AwaitingCheck char(5), AwaitingResult char(5))insert into #temp2 values (@CheckInitials, @ResultInitials)CLOSE check_cursorCLOSE result_cursorDEALLOCATE check_cursorDEALLOCATE result_cursorSelect * from #temp2GOdrop table #tempdrop table #temp2GO" |
|