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 |
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2002-01-24 : 21:21:12
|
| hi is it possible to present (concatnat) the row data in a coloum..ie.i have query return this dataOwner ID DEVICE ID Talkgroup881374 000100249453050 61881374 000100249453050 62881405 000101778765080 1881405 000101778765080 2it should return like thisOwner ID DEVICE ID Talkgroup881374 000100249453050 61,62881405 000101778765080 1,2several rows like this to gothanks======================================Ask to your self before u ask someone |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-01-25 : 03:56:31
|
| Don't think it's possible in a single query - well maybe with a function.select OwnerID, DEVICEID, Talkgroup = convert(varchar(1000),min(Talkgroup)), LastTalkgroup = min(Talkgroup)into #a from tblgroup by OwnerID, DEVICEIDwhile @@rowcount > 0update #aset Talkgroup = Talkgroup + ',' + convert(varchar(1000),min(Talkgroup)), LastTalkgroup = min(Talkgroup)from tblwhere #a.OwnerID = tbl.OwnerIDand #a.DEVICEID = tbl.DEVICEIDand #a.Talkgroup < tbl.Talkgroupand exists(select *from tblwhere #a.OwnerID = tbl.OwnerIDand #a.DEVICEID = tbl.DEVICEIDand #a.Talkgroup < tbl.Talkgroup)select OwnerID, DEVICEID, Talkgroup from #a==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2002-01-29 : 00:58:54
|
| thanks i have a stored procedure to do this but it take hell lot of timeand i am using curors... how well u function perform does it be faster than cursors....thanks======================================Ask to your self before u ask someone |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-01-29 : 16:14:58
|
| This seems to work. HTH Louis.----------------------------------------------------------------create table #me (ownerID int, deviceID varchar(100), talkgroup int, notes varchar(100), counter int)insert into #me select ownerID=881374, deviceID=000100249453050, talkgroup=61, notes=null, counter=nullunionselect ownerID=881374, deviceID=000100249453050, talkgroup=62, notes=null, counter=nullunionselect ownerID=881405, deviceID=000101778765080, talkgroup=1, notes=null, counter=nullunionselect ownerID=881405, deviceID=000101778765080, talkgroup=2, notes=null, counter=nullunionselect ownerID=123456, deviceID=000111111111111, talkgroup=3, notes=null, counter=nullunionselect ownerID=123456, deviceID=000111111111111, talkgroup=4, notes=null, counter=nulldeclare @text varchar(1000)declare @ownerID intdeclare @deviceID varchar(100)declare @counter intwhile exists( select * from #me where notes is null )begin select @ownerID=ownerID, @deviceID=deviceID from #me where notes is null select @text = null,@counter=0 update #me set @text = notes = isnull(cast(talkgroup as varchar),'') +','+ isnull(@text,'') ,@counter = counter = @counter + 1 where ownerID=@ownerID and deviceID=@deviceIDendselect a.ownerID, a.deviceId, notes=left(notes,len(notes)-1) from #me as a join (select ownerid,deviceid,counter = max(counter) from #me group by ownerID, deviceID) as b on a.ownerid=b.ownerid and a.deviceid=b.deviceid and a.counter=b.counter |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2002-02-05 : 02:59:08
|
| thanks.... i have a procedure which does the same.... almostbut i am using cursors.... is there a diffrence between cursor and while exists( select * from #me where notes is null )begin......end======================================Ask to your self before u ask someone |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-02-05 : 03:18:57
|
| The difference is that the cursor will process only one row per loop. Lou's solution can process many rows per loop, and with larger rowsets will be much faster than a cursor solution. You will not see much difference if you're processing < 5,000 rows, but any more than 10,000 should show a marked improvement. |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2002-02-05 : 04:53:02
|
| can we replace the cursors when we do a batch process...while exists( select * from #me where notes is null )begin........enddoes mean .. loop for all the rows where notes is nullthanks======================================Ask to your self before u ask someone |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-02-05 : 17:31:30
|
quote: can we replace the cursors when we do a batch process...does mean .. loop for all the rows where notes is null
Yes, you can do that, but we'll need your code before we can suggest a solution.The only thing a cursor does, THE ONLY THING, is move from one row to the next. It's a row positioning device. Period. It does not UPDATE or DELETE anything; you still must use those SQL commands to perform those actions, they are simply limited to WHERE CURRENT OF CURSOR.If you take the SELECT statement with which you define the cursor you're using, utilize the same JOIN and WHERE clauses, and include them in an UPDATE statement, you'll do the same thing the cursor procedure does, but you'll affect the ENTIRE set, with one statement in one operation, instead of several statements over thousands or more rows.Do yourself a favor: look at all of your code, and wherever you see a CURSOR declaration in that code, put it on your shit list, and vow that you will fix it to use set-based operations instead. You will not believe the improvement in performance that you will see. |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2002-02-05 : 20:39:33
|
thanks... i dont want to use cursors so i did not write the code....i was looking for alternative......the purpose of this code is to recommend the best sutiable rateplan based on the rate plan....sorry i cant give the code right now i dont have...using a query i get this result... AccountID | FULL_USAGE_QTY | plan1 | raterank | Free_Min | Rate51874 346 Saver 0 260 0.1751874 250 Saver 1 260 0.0851874 43 Saver 2 260 0.0531812 110 PlanX 0 0 0.2131812 50 PlanX 1 0 0.1331812 75 PlanX 2 0 0.07for every account i need to take out the free minin order 1,0,2and the remaining min should be charged ... a big base so i dont want to use cursors.....and i have to repeat it for the remaining rate plans hope some one will come up with a solution....thanks.... ======================================Ask to your self before u ask someone |
 |
|
|
|
|
|
|
|