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 |
|
mkool
Starting Member
25 Posts |
Posted - 2008-02-29 : 12:50:25
|
| i m new in sql...and i have this procedure..which have cursor inside..1. i want to get all distinct date into #tempt table.2. In the loop for each distinct date fetch all the date into another #temp3. get max date from that #temp and use that date to get the data from original tablebut i m getting 0 rows else all 8000 rows..which is wrong..can anyone help me plz...create procedure procdate1(@name varchar(50))asbeginSET NOCOUNT ONDECLARE @MaxDate datetimeDECLARE @Date datetimeselect id, title, dated,CONVERT(CHAR(10), dated,101) as date,CONVERT(CHAR(8), dated,114) as time from generalwhere name = @name AND dated = @MaxDateDECLARE CUR1 CURSOR FOR SELECT @DATE FROM #temptOPEN CUR1FETCH NEXT FROM CUR1 INTO @DateWHILE @@FETCH_STATUS = 0BEGINSELECT dated INTO #Date1 FROM general WHERE CONVERT(CHAR(10),dated,101) = @DateSELECT @MaxDate = MAX(dated) FROM #date1--DROP TABLE #Date1FETCH NEXT FROM CUR1 INTO @DatecontinueCLOSE CUR1DEALLOCATE CUR1end-- DROP TABLE #temptend |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-02-29 : 13:11:45
|
| This sounds like it can be done easily without using a cursor. Please provide a sample of your data and the expected results. |
 |
|
|
mkool
Starting Member
25 Posts |
Posted - 2008-02-29 : 13:20:41
|
| i have so many columns in general table..from which i want only id,title,dated columnsjust simple select id,title,dated from general..i m getting more than 8000 rows i want max id for each maxdate. |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-02-29 : 13:36:12
|
If Im understanding you correctly the following select statement should provide you with your desired results:SELECT g1.id, g1.title, g1.datedFROM general g1 JOIN (SELECT title, max(dated) AS maxdated FROM general GROUP BY title) g2 ON g1.title = g2.title AND g1.dated = g2.maxdated |
 |
|
|
|
|
|
|
|