Вопрос

I have a table "match" below

id      league home  away  start_time               match_score
----------------------------------------------------------------  
164247; 952;   1584; 1582; "2013-05-15 18:30:00+03" "{2,1}";
164248; 952;   1585; 881;  "2012-11-03 15:30:00+02" "{2,0}";
164249; 952;   1585; 882;  "2012-11-03 15:30:00+02" "{2,0}";
164250; 952;   1585; 1175; "2012-11-16 18:30:00+02" "{1,0}";
164251; 952;   1585; 1176; "2012-11-16 18:30:00+02" "{1,0}";
164252; 952;   1585; 1274; "2012-10-20 18:30:00+03" "{3,1}";
164253; 952;   1585; 1275; "2012-10-20 18:30:00+03" "{3,1}";
164254; 952;   1585; 1290; "2012-07-28 20:30:00+03" "{0,2}";
164255; 952;   1585; 1290; "2013-04-06 18:30:00+03" "{1,2}";
164256; 952;   1585; 1291; "2012-07-28 20:30:00+03" "{0,2}";
----------------------------------------------------------------

How can I calculate in stored procedure when team (1585) won his latest home match before date ('2013-04-06')?

Expeted result: It's id=164253 from shown table above.

Это было полезно?

Решение

EDIT: Re-reading the question it sounds like you want the whole row, not just the ID.

To get the full row, you can use a CTE;

WITH cte AS (
  SELECT *, ROW_NUMBER() OVER (ORDER BY start_time,id DESC) rn
  FROM match 
  WHERE home = 1585
    AND start_time < '2013-04-06'
    AND match_score[1] > match_score[2]
)
SELECT "id", "league", "home", "away", "start_time", "match_score"
FROM cte WHERE rn=1;

An SQLfiddle to test with.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top