我试图根据DRIVERPOSITION
计算队友击败队友的次数,但我一直得到invalid select-list in subselect
我猜这是因为我使用了子选择查询中的b
和a
表?
示例数据
RACEID CONSTRUCTORID DRIVERID DRIVERPOSITION
970 4 826 3
970 4 807 7
960 4 826 4
960 4 807 7
970 3 820 10
970 3 810 12
960 3 820 13
960 3 810 11
期望的结果
RACEID CONSTRUCTORID DRIVERID WINS
970 4 826 2
970 4 807 0
960 3 820 1
960 3 810 1
到目前为止我尝试了什么
SELECT
(
SELECT COUNT(
CASE
WHEN b.DRIVERPOSITION > a.DRIVERPOSITION THEN 1
ELSE 0 END
)
FROM QUALIFYING b
WHERE RACEYEAR = to_char(NOW(), 'YYYY')
AND a.CONSTRUCTORID = b.CONSTRUCTORID
AND a.RACEID = b.RACEID
AND a.DRIVERID != b.DRIVERID
)
FROM QUALIFYING a
INNER JOIN RACES
ON a.RACEID = RACES.RACEID
INNER JOIN DRIVERS
ON a.DRIVERID = DRIVERS.DRIVERID
INNER JOIN CONSTRUCTORS
ON a.CONSTRUCTORID = CONSTRUCTORS.CONSTRUCTORID
WHERE RACEYEAR = to_char(NOW(), 'YYYY');
答案 0 :(得分:1)
我认为这可以满足您的需求:
select raceid, constructorid, driverid,
sum(case when seqnum = 1 then 1 else 0 end) as numwins
from (select d.*,
row_number() over (partition by raceid, constructorid order by driverposition) as seqnum
from data d
) d
group by raceid, constructorid;
但是,我不知道这如何适合您的查询。您的示例数据引用一个表。您的查询有多个表引用。