我编写了以下查询,但收到多部分标识符未绑定(bind)错误,因为我试图使用来自子选择外部的第一个内部联接的值来过滤子查询。
SELECT runners.id, wins
FROM dbHorseRacing.dbo.historic_runners as runners
inner join dbHorseRacing.dbo.historic_races as races on races.race_id = runners.race_id
inner join (
select ru.runner_id, count(*) as wins
FROM dbHorseRacing.dbo.historic_runners as ru
inner join dbHorseRacing.dbo.historic_races as ra on ra.race_id = ru.race_id
where ru.runner_id = runners.runner_id
and ra.meeting_date < races.meeting_date
and ru.finish_position = 1
group by ru.runner_id
) w on w.runner_id = runners.runner_id
以下两行导致了问题:
where ru.runner_id = runners.runner_id
and ra.meeting_date < races.meeting_date
我尝试以其他方式编写此查询,但没有成功,我看到其他人使用嵌套选择,引用嵌套选择外部的标识符...
作为一项原则,我试图做的事情总体上是错误的吗?如果是这样,还有其他方法可以实现吗?
我已尝试寻找答案,非常感谢任何帮助!
劳拉
最佳答案
试试这个:
SELECT runners.id, (
select count(*)
FROM dbHorseRacing.dbo.historic_races
where historic_races.runner_id = runners.runner_id
and historic_races.meeting_date < races.meeting_date
and ru.finish_position = 1
) wins
FROM dbHorseRacing.dbo.historic_runners as runners
inner join dbHorseRacing.dbo.historic_races as races on races.race_id = runners.race_id
关于sql - 嵌套选择 - 多部分标识符未绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937097/