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 |
|
skillile
Posting Yak Master
208 Posts |
Posted - 2001-12-21 : 13:52:58
|
| I have a table like the one below:resid closer ----------- -------------------- 1 skillile-holland1 skillile-holland1 skillile-holland5 mschmidt-grandha11 green room12 room 1212 room 126 mlawless-hollandI want to insert it into another table with theresid or another field added to the table that increments based on the change. How can I do this without a cursor.End resultresid closer ----------- -------------------- 1 skillile-holland1 skillile-holland1 skillile-holland2 mschmidt-grandha3 green room4 room 124 room 125 mlawless-holland6 jsmith-holland6 jsmith-hollandslow down to move faster... |
|
|
izaltsman
A custom title
1139 Posts |
Posted - 2001-12-21 : 14:18:13
|
I suspect order isn't important... If that's the case, then something like this should work: SELECT 1 + (SELECT COUNT(DISTINCT closer) FROM #mytable as a WHERE a.closer < mt.closer) as resid, mt.closerFROM #mytable as mt Edited by - izaltsman on 12/21/2001 14:19:37 |
 |
|
|
skillile
Posting Yak Master
208 Posts |
Posted - 2001-12-21 : 14:42:50
|
| This is what I am trying to do now.I added your solution as (test int).declare @closings table ( clsident int primary key identity(1,1), closeid int, ordid int, resid int, cldate smalldatetime, cldateend smalldatetime, closer varchar(20), priority int, location varchar(20), propadd varchar(50), metfileid varchar(20), test int )insert @closingsselect a.closeid, a.ordid, a.resid, a.cldate, a.cldateend, b.label as closer, c.priority, d.label as location, e.propadd, e.metfileid, (SELECT 1 + (SELECT COUNT(DISTINCT closer) FROM @closings as a WHERE a.closer < mt.closer) mt.closer FROM @closings mt )from tblorder_close ainner join tblresource b on a.resid=b.residinner join tblresource_join c on a.resid=c.resid and c.oid=@oidinner join tbllocation d on a.locid=d.locidinner join tblorder e on a.ordid=e.ordidwhere a.moid=@oid AND (datepart(dd,a.cldate) = datepart(dd,@starttime) AND datepart(mm,a.cldate) = datepart(mm,@starttime) AND datepart(yyyy,a.cldate) = datepart(yyyy,@starttime) )These closers all have a unique resid I want to keep the uniquenessbut start from 1 and go to max# of users.Thanksslow down to move faster... |
 |
|
|
izaltsman
A custom title
1139 Posts |
Posted - 2001-12-21 : 15:48:23
|
Well, first of all, you were missing a comma. Secondly, you need to populate the table before you query it. Finally for some reason SQL2k does not seem to treat table variables the same as temporary tables within a subquery, so I replaced table variable with a temp table. Will this do the trick for you? create table #closings ( clsident int primary key identity(1,1), closeid int, ordid int, resid int, cldate smalldatetime, cldateend smalldatetime, closer varchar(20), priority int, location varchar(20), propadd varchar(50), metfileid varchar(20), test int )insert #closingsselect a.closeid, a.ordid, a.resid, a.cldate, a.cldateend, b.label as closer, c.priority, d.label as location, e.propadd, e.metfileid, NULLfrom tblorder_close ainner join tblresource b on a.resid=b.residinner join tblresource_join c on a.resid=c.resid and c.oid=@oidinner join tbllocation d on a.locid=d.locidinner join tblorder e on a.ordid=e.ordidwhere a.moid=@oid AND (datepart(dd,a.cldate) = datepart(dd,@starttime) AND datepart(mm,a.cldate) = datepart(mm,@starttime) AND datepart(yyyy,a.cldate) = datepart(yyyy,@starttime) )update #closingsset test = x.residFROM #closings INNER JOIN (SELECT 1 + (SELECT COUNT(DISTINCT closer) FROM #closings as a WHERE a.closer < mt.closer) as resid, mt.closer FROM #closings as mt) as x ON #closings.closer = x.closerselect * from #closings order by test Edited by - izaltsman on 12/21/2001 15:49:40Edited by - izaltsman on 12/21/2001 15:50:28 |
 |
|
|
skillile
Posting Yak Master
208 Posts |
Posted - 2001-12-21 : 16:21:08
|
| yeah,we must be thinking alike thats what I was thinking with you.But let me back up. I may be up a creek here. If you see my fields I also have priority column.My last statement in this proc is to select as many closers(columns) inpriority asc.ie./*resid closer priority----------- ----------------- --------- 1 skillile-holland 61 skillile-holland 61 skillile-holland 62 mschmidt-grandha 53 green room 54 room 12 44 room 12 35 mlawless-holland 46 jsmith-holland 26 jsmith-holland 2*/SET @x=1SET @columncount=(select count(distinct resid) from @closings)--this is my last statement to return as many select statement--as I have columns --by priority by userwhile @x <= @columcountbegin select * from @closings where resid=@x order by priority --this will not work that is where I am getting messed upendthe priority is not unique reason the closers are not always activeI am only chosing the ones with activity but the priority still needsto be asc.END RESULT: I need all my closers with a unique key and sorted by priorityslow down to move faster... |
 |
|
|
skillile
Posting Yak Master
208 Posts |
Posted - 2001-12-21 : 18:29:20
|
| Here is what I think I need. I added another tableto come up with the priority id's this will incthe resids as they came in.thank you izaltsman for all your help. I will use yoursolution with the backside to another sproc.Thanks for the time..ALTER PROCEDURE s_cal_office_matrix2(@ddate smalldatetime='12/17/2001',@oid int=36--@starttime smalldatetime output,--@endtime smalldatetime output,--@columncount int output,--@minuteview int output,--@totalsteps int output)asSET NOCOUNT ON--declare varsdeclare @closedate smalldatetimedeclare @starttime smalldatetimedeclare @endtime smalldatetimedeclare @columncount intdeclare @minuteview intdeclare @x intdeclare @closings table ( clsident int primary key identity(1,1), closeid int, ordid int, resid int, cldate smalldatetime, cldateend smalldatetime, closer varchar(20), priority int, location varchar(20), propadd varchar(50), metfileid varchar(20) )declare @priority table ( priority int identity(1,1), prior_cls int, resid int )--set vars and tableSET @closedate=@ddate --save the original date with no timeSET @starttime= convert(smalldatetime,(convert(varchar(20), @ddate, 101) + ' 6:00:00'), 101)SET @endtime= convert(smalldatetime,(convert(varchar(20), @ddate, 101) + ' 20:00:00'), 101)SET @minuteview=1 --set the minutes to 1 hour--get daily closings for that office--and place values into variable tableinsert @closingsselect a.closeid, a.ordid, a.resid, a.cldate, a.cldateend, b.label as closer, c.priority, d.label as location, e.propadd, e.metfileidfrom tblorder_close ainner join tblresource b on a.resid=b.residinner join tblresource_join c on a.resid=c.resid and c.oid=@oidinner join tbllocation d on a.locid=d.locidinner join tblorder e on a.ordid=e.ordidwhere a.moid=@oid AND (datepart(dd,a.cldate) = datepart(dd,@starttime) AND datepart(mm,a.cldate) = datepart(mm,@starttime) AND datepart(yyyy,a.cldate) = datepart(yyyy,@starttime) )--columncountSET @columncount=(select count(distinct resid) from @closings) --minuteviewif exists (select cldate from @closings where datepart(mi, cldate)=30 or datepart(mi,cldateend) = 30)SET @minuteview=30if exists (select cldate from @closings where datepart(mi, cldate)=15 or datepart(mi,cldateend) = 15)SET @minuteview=15 if exists (select cldate from @closings where datepart(mi, cldate)=45 or datepart(mi,cldateend) = 45)SET @minuteview=15--insert @priority and set the ranking order of the columninsert @priorityselect distinct priority, resid from @closingsorder by priority, resid--set inc varset @x=1--return rswhile @x <= @columncountbeginselect a.priority, b.closeid, b.ordid, b.cldate, b.cldateend, b.closer, b.location, b.metfileid from @priority ainner join @closings b on a.resid=b.residwhere a.priority=@xorder by a.priority, b.cldateset @x=@x+1end--test s_cal_office_matrix2slow down to move faster... |
 |
|
|
|
|
|
|
|