1

我有以下查询:

Select Player_Id,
Pt_Credit As Total_Points
From Player 
Where Trunc(Create_Dtime) = Trunc(Sysdate) - 90
And Trunc(Init_Dtime) >= To_Date('2012-sep-1','yyyy-mon-dd')
Order by Total_Points Desc

这给了我这个结果:

Player_ID      TOTAL_POINTS
  23452         1500
  23494         1000
  30934         500

等等。我想将这些 player_ids 结果分组为 0-300 点、301-600 点、601-900 点和大于 900 点的组......所以结果看起来像这样:

Point_Level     Count(Player_ID)
0-300            532
301-600          444
601-900          669
>900             185
4

2 回答 2

2

采用CASE ... WHEN

SELECT players.clasification, count(players.player_id)
FROM (
    SELECT ROWNUM player_id, 
        (ROWNUM + 100) points,
        CASE
            WHEN (ROWNUM + 100) BETWEEN 0 AND 300 THEN
                '0-300'
            WHEN (ROWNUM + 100) BETWEEN 301 AND 600 THEN
                '301-600'
            WHEN (ROWNUM + 100) BETWEEN 601 AND 900 THEN
                '601-900'
            ELSE
                '> 900'
            END clasification
    FROM dual 
    CONNECT BY LEVEL <= 1000
) players 
GROUP BY players.clasification;

或者在您的示例中:

SELECT 
    count(player.player_id),
    player.clasification
FROM (
    SELECT pl.*,
        CASE
            WHEN total_points BETWEEN 0 AND 300
                THEN '0-300'
            WHEN total_points BETWEEN 301 AND 600
                THEN '301-600'
            WHEN total_points BETWEEN 601 AND 900
                THEN '601-900'
            ELSE '> 900'
        END clasification
    FROM player pl
    WHERE 
            TRUNC(Create_Dtime)  = TRUNC(SYSDATE) - 90
        AND TRUNC(Init_Dtime) >= To_Date('2012-sep-1', 'yyyy-mon-dd')

)
    Player
GROUP BY player.clasification;
于 2012-11-02T23:06:34.680 回答
0
Select case 
    when Pt_Credit between 0 and 300 then '0 to 300'
    when Pt_Credit between 301 and 600 then '301 to 600'
    when Pt_Credit between 601 and 900 then '601 to 900'
    else '> 900'
end Total_Points
, count(*)
From Player 
Where Trunc(Create_Dtime) = Trunc(Sysdate) - 90
And Trunc(Init_Dtime) >= To_Date('2012-sep-1','yyyy-mon-dd')
Group by case 
    when Pt_Credit between 0 and 300 then 0 
    when Pt_Credit between 301 and 600 then 1 
    when Pt_Credit between 601 and 900 then 2
    else 3
end
Order by Total_Points Desc

或者

Select case 
    when Pt_Credit between 0 and 300 then '0 to 300'
    when Pt_Credit between 301 and 600 then '301 to 600'
    when Pt_Credit between 601 and 900 then '601 to 900'
    else '> 900'
end Total_Points
, count(*)
From Player 
Where Trunc(Create_Dtime) = Trunc(Sysdate) - 90
And Trunc(Init_Dtime) >= To_Date('2012-sep-1','yyyy-mon-dd')
Group by cast(Pt_Credit/300 as integer)
Order by Total_Points Desc
于 2012-11-02T23:01:02.373 回答