我有以下查询:
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
答案 0 :(得分: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;
答案 1 :(得分: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