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 |
|
chrispy
Posting Yak Master
107 Posts |
Posted - 2007-07-09 : 18:15:42
|
The #tempa that the below refers to is as suchLocID intLocname varchar (100)JobID intand could contain:LocID LocName JobID------ ------- -----44 Dallas 9944 Dallas 10055 Miami 9955 Miami 10055 Miami 101 I am creating pagation from the #tempa and need to also get the number of job openings for each LocID, so the example above would return LocID LocName JobID Openings------ ------- ----- --------44 Dallas 99 244 Dallas 100 255 Miami 99 355 Miami 100 355 Miami 101 3 Here is SELECT statement I would like to bring this into :Select #tempa.*, WordRadius = ( SELECT dbo.udf_Num_ToWords(MileRadius) ), MoreRecords = ( SELECT COUNT(*) FROM #tempa WHERE RecCount >= @LastRec ), TotalRecords= ( SELECT COUNT(*) FROM #tempa ) FROM #tempa WHERE RecCount > @FirstRec AND RecCount < @LastRec I already have two table scans on the #tempa and would like to get this and reduce the table scans.Any steps in the right direction would be appreciated!!! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-09 : 18:16:53
|
| So why not create an index on your table?Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
chrispy
Posting Yak Master
107 Posts |
Posted - 2007-07-09 : 18:22:39
|
| Tara,I am selecting into this #tempa, would it be useful to create an index after the selection? Or would it be better off to allow the #temp to be scanned? I imagine there is a breaking point as to when and when not to create the index, but that is beyond my knowledge. If I could guess the #tempa would have about 10,000 rows at the most, but the average would most likely be around 1,0000.but I still need to get the additional count in there.Thanks for the help. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-09 : 18:32:34
|
| You typically should create the temp table first with the indexes on it, then insert the rows into it.If you are trying to avoid a table scan on a temp table, then you need to add an index. Without an index, SQL Server scans.To see which way works best for you, you'll need to test it. We can't answer that for you.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
chrispy
Posting Yak Master
107 Posts |
Posted - 2007-07-09 : 18:42:06
|
| Tara,Thanks for the advice.I will try indexing the #tempa both ways and see where it gets me. I am still stuck on getting the count for the number of jobs per location as I mentioned above. Still need help on that.Thanks again! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-09 : 18:58:08
|
| How is Openings calculated?Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
chrispy
Posting Yak Master
107 Posts |
Posted - 2007-07-09 : 19:13:58
|
| Tara,I guess I am not being clear. 'openings' is what I am trying to calculate. If 'dallas' appears two times in the table then I need 'openings' to be 2, |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-09 : 19:25:22
|
| SELECT LocName, COUNT(*) AS OpeningsFROM #tempaGROUP BY LocNameIf you could show us the query where you are inserting the data into the temp table, then we can add the above to it as well. It doesn't look like there's a point in adding the Openings field to the select statement you posted as that doesn't show your expected result set anyway.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-09 : 19:34:06
|
Or perhaps this is what you want:SELECT t.*, d.Openings, WordRadius = (SELECT dbo.udf_Num_ToWords(MileRadius)), MoreRecords = (SELECT COUNT(*) FROM #tempa WHERE RecCount >= @LastRec), TotalRecords = (SELECT COUNT(*) FROM #tempa) FROM #tempa tINNER JOIN( SELECT LocName, COUNT(*) AS Openings FROM #tempa GROUP BY LocName) dON t.LocName = d.LocNameWHERE RecCount > @FirstRec AND RecCount < @LastRec You should consider doing the MoreRecords and TotalRecords outside of this query though. Just stuff their values into a variable and output those in the stored procedure.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
chrispy
Posting Yak Master
107 Posts |
Posted - 2007-07-09 : 20:01:06
|
Tara,BINGO! The second one was what I was looking for. Thanks! I knew how to get the count by the GROUP BY like you mentioned first, just could not work it like you had in the second code sample. Thanks for the tip on MoreRecords and the TotalRecords. This sproc started out working fine, but I was dumping it into 4 #temps, just trying to refine and and thanks to the help here I am!!!!Given what this sproc is doing I am very happy with it now. Although I think I need to test different ways on the #tempa index. CREATE PROC [dbo].[SP_search_radi_Job] @CustID varchar (50), @Mode int = 0, @Zip char(8) , @Miles int, @OrderBy int = 1, @SortAscDesc char (4) = 'asc', @RecsPerPage int = 250, @Page int = 1 ASSET NOCOUNT ONDECLARE @HighLatitude float , @LowLatitude float, @HighLongitude float, @LowLongitude floatDECLARE @StartLatitude float, @StartLongitude float, @LatitudeRange float, @LongitudeRange floatDECLARE @sql VARCHAR(64)DECLARE @FirstRec int ,@LastRec int, @MoreRecords int, @TotalRecords intSELECT @StartLatitude = a.lat, @StartLongitude = a.LonFROM places aWHERE a.zip = @Zip Set @LongitudeRange = @Miles / (((Cos(@StartLatitude * pi() / 180) * 6076.0) / 5280.0) * 60) Set @LatitudeRange = @Miles / 69.0454545454545454545454545454 --55 Set @LowLatitude = @StartLatitude - @LatitudeRange Set @HighLatitude = @StartLatitude + @LatitudeRange Set @LowLongitude = @StartLongitude - @LongitudeRange Set @HighLongitude = @StartLongitude + @LongitudeRangeSELECT @FirstRec = (@Page - 1) * @RecsPerPageSELECT @LastRec = (@Page * @RecsPerPage + 1)SELECT a.*, RecCount = identity(int,1,1)INTO #tempaFROM ( SELECT e.LocationName, e.LocationID, e.Address, e.City, e.State, b.Jobtitle, b.Summary, b.JobID, MileRadius = ROUND(dbo.F_GREAT_CIRCLE_DISTANCE(@StartLatitude,@StartLongitude,lat,lon),0) FROM dbo.JobDetail AS b INNER JOIN dbo.JobsActive AS c INNER JOIN dbo.Places AS d INNER JOIN dbo.LocationDetail AS e ON d.ZIP = e.Zip ON c.LocationID = e.LocationID ON b.JobID = c.JobID WHERE (b.CustID = @CustID) AND (b.ActiveFlag = 1) AND (Lat <= @HighLatitude AND Lat >= @LowLatitude) AND (Lon <= @HighLongitude AND Lon >= @LowLongitude) -- MileRadius < (@Miles * 1.05) -- Drops bogus resul that are larger then 5% of the orginal radius ) a CREATE UNIQUE CLUSTERED INDEX IX_1 ON #tempa (RecCount, LocationID, JobID)SET @MoreRecords = (SELECT COUNT(*) FROM #tempa WHERE RecCount >= @LastRec)SET @TotalRecords = (SELECT COUNT(*) FROM #tempa)SELECT t.*, d.Openings, WordRadius = (SELECT dbo.udf_Num_ToWords(MileRadius)), MoreRecords = @MoreRecords, TotalRecords = @TotalRecords FROM #tempa t INNER JOIN ( SELECT LocationID, COUNT(*) AS Openings FROM #tempa GROUP BY LocationID ) d ON t.LocationID = d.LocationIDWHERE RecCount > @FirstRec AND RecCount < @LastRecORDER BY MileRadius , LocationName |
 |
|
|
|
|
|
|
|