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 に答える