| Author |
Topic |
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 06:54:03
|
| hi all, i'm trying to find a workaround for a script i wrote that uses cursor in sql server 2000 (cursor is a bit slow).i have a table full of transactions some of which have the same tid. the rows with more that one tid represent the same transaction but allocated to diferent categories. i need to extract all transactions with only one tid and also the first row for transactions with multiple splits. eg; tid,name, category 1224, smith, cat11224, smith, cat21223, jones, cat4given the table above, i need the following results1224, smith, cat11223, jones, cat4hope thats clear. here is the script i wrote. it does work but its little slow. DECLARE @mytid intDECLARE @mycat nvarchar (50)DECLARE @myemail nvarchar (50)DECLARE @myamount float DECLARE @mygfdate smalldatetime DECLARE @pkey intDECLARE @mytidcompare intDECLARE @mycatcompare nvarchar (50)DECLARE @myemailcompare nvarchar (50)DECLARE @myamountcompare float DECLARE @mygfdatecompare smalldatetime DECLARE @pkeycompare int--DEALLOCATE mycursorDECLARE mycursor CURSOR dynamic FOR SELECT tid, category, email, amount, gfdate, pkey FROM [gift aid].[dbo].splitgiftlist OPEN mycursor FETCH firstfrom mycursorINTO @mytid, @mycat, @myemail, @myamount, @mygfdate, @pkey INSERT INTO [gift aid].dbo.splitsinglefirst(tid, category, email, amount, gfdate, pkey) VALUES (@mytid, @mycat, @myemail, @myamount, @mygfdate, @pkey)print 'tid first'print @mytidWHILE @@fetch_status = 0 BEGIN FETCH myCURSOR INTO @mytid,@mycat, @myemail, @myamount, @mygfdate, @pkey print '@mycat second' print @mycat --FETCH PRIOR --FROM mycursor --INTO @mytidcompare,@mycatcompare, @myemailcompare, @myamountcompare, @mygfdatecompare print '@mycat compare' print @mycat --print 'compare' --print @mytidcompare print 'tid 2nd' print @mytid --if @mytidcompare <> @mytid IF @mytid not in (select tid from [gift aid].dbo.splitsinglefirst) begin INSERT INTO [gift aid].dbo.splitsinglefirst(tid, category, email, amount, gfdate, pkey) VALUES (@mytid, @mycat, @myemail, @myamount, @mygfdate, @pkey) end --fetch next from mycursorEND CLOSE mycursor DEALLOCATE mycursorps: please bear in mind this is my first sql script. regardss |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 07:05:27
|
| See if this worksselect tid,name, category from yourtable where category in (select max(category) from yourtable group by tid)MadhivananFailing to plan is Planning to fail |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 07:13:57
|
| this returns all rows in the source table. the subquery isolates the records i need but won't give me the columns that i need. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 07:21:16
|
| Then you need to give more sample dataPost some 20 sample dataMadhivananFailing to plan is Planning to fail |
 |
|
|
kapilarya
Yak Posting Veteran
86 Posts |
Posted - 2005-08-08 : 07:26:06
|
| try this select IDENTITY(int,1,1) as abc, tid,name, category, email, amount, gfdate, pkey into #temp from splitgiftlistselect tid,name, category, email, amount, gfdate, pkey from #temp t2where abc in (select min(t1.abc) from #temp t1 group by t1.tid and t1.tid = t2.tid)Kapil Arya |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 07:38:49
|
| If category is not duplicated for tid, then this should give the correct resultselect tid,name,max(category) from yourtable group by tid,nameMadhivananFailing to plan is Planning to fail |
 |
|
|
kapilarya
Yak Posting Veteran
86 Posts |
Posted - 2005-08-08 : 07:41:56
|
| Madhivanan he wants the first record of the same tid and not the max categorytid,name, category 1224, smith, cat11224, smith, cat21223, jones, cat4given the table above, i need the following results1224, smith, cat11223, jones, cat4Kapil Arya |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2005-08-08 : 08:26:03
|
| There is no "first record". there has to be another defining criteria to be able to order the records. Perhaps by Category?select tid,name,max(category) from yourtable group by tid,name order by category desc*need more coffee*SELECT * FROM Users WHERE CLUE > 0(0 row(s) affected) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 08:47:55
|
| >>he wants the first record of the same tid and not the max categoryYes. In that case min will give the answerMadhivananFailing to plan is Planning to fail |
 |
|
|
kapilarya
Yak Posting Veteran
86 Posts |
Posted - 2005-08-08 : 08:50:21
|
| simbeeosis "I need to extract all transactions with only one tid and also the first row for transactions with multiple splits."this line means that he wants the first row of the distinct tid's and not the max categoryKapil Arya |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 08:52:46
|
WellThats why I asked the questioner to post some more data to have his exact requirement MadhivananFailing to plan is Planning to fail |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 09:21:25
|
| kapil, the query you provided gave an error. it doesn't like the and in the group by clause. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 09:22:46
|
| Did you try the query I posted?MadhivananFailing to plan is Planning to fail |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 09:27:24
|
| here is some real data. 22188,emer,76kdc@i214c24.net,30,21/11/2004,1269528016,gen,76kdc@i214c24.net,30,09/04/2004,1322527740,gen,76kdc@i214c24.net,50,21/03/2004,1320723393,emer,76kdc@i214c24.net,100,19/12/2003,1273823393,gen,76kdc@i214c24.net,10,19/12/2003,12738this is what i should get. 22188,emer,76kdc@i214c24.net,30,21/11/2004,1269528016,gen,76kdc@i214c24.net,30,09/04/2004,1322527740,gen,76kdc@i214c24.net,50,21/03/2004,1320723393,emer,76kdc@i214c24.net,100,19/12/2003,12738please note, the final column is the table's premiary key and so is unique for all rows. regards |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2005-08-08 : 09:27:53
|
quote: simbeeosis "I need to extract all transactions with only one tid and also the first row for transactions with multiple splits."this line means that he wants the first row of the distinct tid's and not the max category
[Matrix] There IS NO FIRST ROW [/Matrix]SQL SERVER does not give a hoot about row numbers. You must supply an ORDER BY clause to get a meaningful "First Row"Help us help YOU!Read this blog entry for more details: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx*need more coffee*SELECT * FROM Users WHERE CLUE > 0(0 row(s) affected) |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 09:30:29
|
| madhivan,yes i tried it. it returns too many records when i include other columns in the order by clause |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 09:33:18
|
| donatwork,this is why i used cursor. i could think of no other way of being able to deal with the dataset row by row. i thought of derived tables but could think of no way of coding what i needed. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 09:43:15
|
| Post your sample data with column namesMadhivananFailing to plan is Planning to fail |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 09:45:01
|
| tid,category,email,gfdate,pkey22188,emer,76kdc@i214c24.net,30,21/11/2004,1269528016,gen,76kdc@i214c24.net,30,09/04/2004,1322527740,gen,76kdc@i214c24.net,50,21/03/2004,1320723393,emer,76kdc@i214c24.net,100,19/12/2003,1273823393,gen,76kdc@i214c24.net,10,19/12/2003,12738 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-08 : 09:51:18
|
| Try thisSelect Distinct tid,category,(Select top 1 email from yourtable where tid=T.tid),gfdate,pkey from yourtable TMadhivananFailing to plan is Planning to fail |
 |
|
|
simbeeosis
Starting Member
10 Posts |
Posted - 2005-08-08 : 09:54:57
|
| Select Distinct tid,category,(Select top 1 email from yourtable where tid=T.tid),gfdate,pkey from yourtable Tthis returns all rows from the table |
 |
|
|
Next Page
|
|
|