累积决策Oracle SQL

时间:2016-04-03 19:57:45

标签: sql oracle11g

美好的一天,

我有三列:Game_Date,Decision,Record

每一行(Game_Date

2 个答案:

答案 0 :(得分:2)

您可以使用窗口(analytic Function)来获取每个日期/季节的总数。

NOW TESTED!:

With GAME as
(Select to_date('15-MAR-15','DD-MON-YY') as Game_date, 'W' as decision, '2014-2015' as Season from dual UNION ALL
Select  to_date('19-MAR-15','DD-MON-YY') as Game_date, 'L' as decision, '2014-2015' as Season  from dual UNION ALL
Select  to_date('22-MAR-15','DD-MON-YY') as Game_date, 'L' as decision, '2014-2015' as Season  from dual UNION ALL
Select  to_date('24-MAR-15','DD-MON-YY') as Game_date, 'O' as decision, '2014-2015' as Season  from dual UNION ALL
Select  to_date('28-MAR-15','DD-MON-YY') as Game_date, 'W' as decision, '2014-2015' as Season  from dual UNION ALL
Select  to_date('01-APR-15','DD-MON-YY') as Game_date, 'W' as decision, '2014-2015' as Season  from dual )

--YOU ONLY NEED TO RUN THIS PART!!!    
SELECT Game_date AS "Date", Decision,
   SUM(CASE WHEN Decision = 'W' THEN 1 else 0 END) Over (partition by Season order by game_Date) || '-' || 
   SUM(CASE WHEN Decision = 'L' THEN 1 else 0 END) Over (partition by Season order by game_Date) || '-' || 
   SUM (CASE WHEN Decision = 'O' THEN 1 else 0 END) Over (partition by Season order by game_Date) AS "RECORD"
FROM Game
WHERE Season = '2014-2015' 
  --AND (Team_Visitor_ID = 1 OR Team_Home_ID = 1)

ORDER BY Game_Date desc

enter image description here

答案 1 :(得分:1)

试试这个:

select TO_CHAR(Game_Date, 'DD MON ''YY') AS "DATE",
SUM(W) Over (order by Game_Date) || '-' ||
SUM(L) Over (order by Game_Date) || '-' || 
SUM(O) Over (order by Game_Date) AS "RECORD"
from (
 SELECT Game_Date,
 SUM (CASE WHEN Decision like 'W' THEN 1 END) as W, 
 SUM (CASE WHEN Decision like 'L' THEN 1 END) as L, 
 SUM (CASE WHEN Decision like 'O' THEN 1 END) as O
 FROM Game
 WHERE Season = '2014-2015' AND (Team_Visitor_ID = 1 OR Team_Home_ID = 1)
 GROUP BY Game_Date)
ORDER BY Game_Date DESC;