Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Ranking in SQL

$
0
0

I have group of riders that log miles ridden on the website. Miles are saved and summed in a mileagelog table. Currently they are ordered by total miles by the year. New requirement is to have an indicator that show if a rider's progress in relation to others. I can calculate their current position using this code.

declare @year int
set @year = 2014



declare @ridepost table
(
  riderid int
, [currentspot] int
, [priorspot] int
,  movement int
, [year] int
)

;with numberedrows 
as
(SELECT  m.riderid, sum(m.distancemiles) as [distance], 0 as [prior],@year as [year],
 'N' as [movement], ROW_NUMBER() OVER (ORDER BY sum(m.distancemiles)desc) AS position 
FROM    mileagelog m
WHERE  m.ridetypeid <> 7
and DATEPART(YYYY,ridedate) = @year
group by m.riderid
)

insert into BDUC.riderspot
select 	riderid
,      position
,		[prior]
,       movement
,       [year]
 from numberedrows

This orders all of the riders for the year. The problem I have is how to show changes.  The table riderspot has the rider and year. ON INSERT the position column is set to their current position the prior set to 0 and the movement flag set to 'N' (new). If a rider inserts a new mileage the prior column is set ti the previous position and the position should be set to the new position. If the new position passes one or more riders that were ahead of him his movement is set to 'U' (up) and any riders he passed are set to 'D' (down). The ruder may also delete a ride in which case he should be flagged as 'D' (down) and those that passed him = 'U'. If he adds miles but does not pass anyone his movement = 'S' (static). And of course if he ties it is set to 'T' as is the rider he ties with. Simple eh? Any Ideas?

This is my attempt at assigning positions...

DECLARE @riderid int
select @riderid = 670
DECLARE @year int
select @year = 2014
declare @current int
declare @prior int
declare @movement char(1)
declare @rider2 int

declare @riderspot  table
(
    rider int
 ,  [current] int
 ,	[prior] int
 ,  movement char(1)
 ,  [year] int
)

-- if there is a rw for the rider/year...
IF exists (select * from BDUC.riderspot where riderid = @riderid and curyear =  @year )

	--Update records
	
	SELECT @prior = (SELECT  currentspot from BDUC.riderspot WHERE  riderid = @riderid and curyear =  @year)
	select @prior
	
	DECLARE @mileagerank TABLE
(   distance  float
  , riderid  int
  , rowcounter int
  )


                                          
;with currentrows 
as
(SELECT riderid, sum(distancemiles) as [distance],  ROW_NUMBER() OVER (ORDER BY sum(distancemiles)desc) AS RowNumber
FROM    mileagelog 
WHERE DATEPART(YYYY,ridedate) = @year
AND ridetypeid <> 7
group by riderid)
insert into @mileagerank select nr.[distance], nr.riderid, nr.RowNumber from currentrows nr
                                                  JOIn riders r
                                                  on  r.riderid = nr.riderid

SELECT @current = (select rowcounter from @mileagerank where riderid = @riderid )
SELECT @current

select @movement =  case when @current < @prior then 'U'
                  WHEN @current > @prior then 'D'
                  WHEN @current = @prior then 'S'
                  ELSE 'N'
                  END
                  SELECT @movement
                  UPDATE BDUC.riderspot
                  SET currentspot = @current
                  ,   priorspot = @prior
                  , movement  = @movement
                  WHERE riderid = @riderid
                  and   curyear = @year


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>