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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Counting rows within a portion of query

Author  Topic 

pixelwiz
Starting Member

25 Posts

Posted - 2010-12-07 : 16:07:10
Hi All,

I hope this question makes sense.

I have a fairly massive query (below), but the gist of it, it has to calculate groups numbers and player order for a field.

I've ran into a problem, when the field does not evenly divide into 3, the player order gets thrown off.

so if I had something like this for group numbers, how can I get the numbers on the right calculated
1 1
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3

Right now the results I am getting are wrong
1 1
2 2
2 3
2 1
3 2
3 3
3 1
4 2
4 3
4 1


DECLARE
@nr_ttime datetime,
@interval tinyint,
@starters_time float

SET @nr_ttime = '8:00 AM'
SET @interval = 10
SET @starters_time = 9999

SELECT 123 AS nr_course_id,
(CEILING((Row_Number() OVER
(ORDER BY nr_group_number, score, r1_ttime, r1_start_hole, r1_player_order, course_priority)) % 3.001) )
player_order,

DateAdd(minute, (@interval * nr_group_number - @interval +
CASE
WHEN (nr_group_number) / @starters_time > 1
THEN (FLOOR ((nr_group_number - 1) / @starters_time) * @interval)
ELSE 0
END
),@nr_ttime)

nr_ttime,
1 nr_start_hole,
(SELECT id FROM flights WHERE event_id = 5008 AND flight_number = 1) nr_flight_id,
e.* FROM (


SELECT
CEILING(
MAX(b.group_order) OVER (PARTITION BY b.event_id) --total groups
+ 1 - b.group_order)
nr_group_number, b.*, c.r1_shots
FROM(
SELECT a.*, g.ttime r1_ttime, g.start_hole r1_start_hole, gp.player_order r1_player_order, f.flight_number, ec.course_priority,
CEILING (rank() OVER (ORDER BY a.score, g.ttime, gp.player_order, g.start_hole, ec.course_priority) / 3.0) group_order,
rank() OVER (ORDER BY a.score) position
FROM(
SELECT p.id player_id, p.first_name, p.last_name, p.city, p.state, p.country, sum(ph.shots-h.par) score, h.event_id
FROM players p
INNER JOIN player_holes ph
ON p.id = ph.player_id
INNER JOIN holes h
ON h.id = ph.hole_id
WHERE h.event_id = 5008
AND ph.round < 2
GROUP BY p.id, p.first_name, p.last_name, p.city, p.state, p.country, h.event_id) a
INNER JOIN group_players gp
ON gp.player_id = a.player_id
INNER JOIN groups g
ON gp.group_id = g.id
AND g.event_id = a.event_id
AND g.round = 1
INNER JOIN flights f
ON f.id = g.flight_id
INNER JOIN event_courses ec
ON ec.course_id = g.course_id
AND ec.event_id = g.event_id
INNER JOIN event_players ep -- Make sure all the players with scores haven't DQ or WD or other status change
ON ep.player_id = a.player_id
AND ep.event_id = 5008
AND ep.status_id IS NULL
) b
INNER JOIN (
SELECT ph.player_id, SUM(ph.shots) r1_shots
FROM player_holes ph
INNER JOIN holes h
ON h.id = ph.hole_id
AND ph.round = 1
AND h.event_id = 5008
GROUP BY ph.player_id
) c
ON c.player_id = b.player_id
WHERE b.position <= (SELECT ISNULL(cut,9999) FROM events WHERE id = 5008)
GROUP BY b.player_id, b.first_name, b.last_name, b.city, b.state, b.country, b.score, b.event_id, b.r1_ttime, b.r1_start_hole, b.r1_player_order,
b.flight_number, b.course_priority, b.group_order, b.position, c.r1_shots
) e
GROUP BY nr_group_number, player_id, first_name, last_name, city, state, country, score, event_id, r1_ttime, r1_start_hole, r1_player_order,
flight_number, course_priority, group_order, position, r1_shots
ORDER BY nr_group_number

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2010-12-07 : 23:02:23
Please show sample data of what you are starting with, and what you want to end with. I am not following your data, and your query is too large where I would prefer not to go though it.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

pixelwiz
Starting Member

25 Posts

Posted - 2010-12-08 : 08:21:32
Ok, let me try to give a little better sample data.

Say I have an output kinda like this, and I need to calculate the player order within each group. So in the example below, there is only one player in group #1, so his player order is 1. In group #2, there are 3 players, and their player order should go from 1 to 3.

Group #1 Player_A (CalculatedColumn = 1)
Group #2 Player_B (CalculatedColumn = 1)
Group #2 Player_C (CalculatedColumn = 2)
Group #2 Player_D (CalculatedColumn = 3)
Group #3 Player_E (CalculatedColumn = 1)
Group #3 Player_F (CalculatedColumn = 2)
Group #3 Player_G (CalculatedColumn = 3)
Group #4 Player_H (CalculatedColumn = 1)
Group #4 Player_I (CalculatedColumn = 2)
Group #4 Player_J (CalculatedColumn = 3)
Go to Top of Page

pixelwiz
Starting Member

25 Posts

Posted - 2010-12-08 : 09:11:25
I figured it out!

So in case someone else is looking the key is this:

(Row_Number() OVER
(PARTITION BY nr_group_number ORDER BY nr_group_number, score, r1_ttime, r1_start_hole, r1_player_order, course_priority))
player_order

Adding in PARTITION BY nr_group_number did the trick.
Go to Top of Page
   

- Advertisement -