我应该知道这一点 - 但我不知道,任何人都可以用语法帮助我。
假设我正在跟踪越野接力赛中的跑步者,我有一个主要的联赛表,将跑步者与比赛联系起来......还有跑步者表,告诉我他们何时/何时开始他们的腿以及在哪里/当他们结束时...还有沿路线记录的检查点。
要求每个跑步者都有一个起点和终点 - 但是,这并不总是正确输入* - 我需要一个查询来拉出在比赛中没有两种类型的参赛者的跑步者。
(*是的我正在修复数据输入方面的事情)
THX
结构:
RACE TABLE
raceID (int)
runnerID (int)
STOPS TABLE
runnerID (int)
stopTypeID (int) -- fk to stop type
when (timestamp)
sequence (smallint)
我还没有“拥有”任何东西 - 因为我被我需要的东西所吸引......
获得跑步者和比赛日志的基本问题是
SELECT *
FROM RACES R
JOIN STOPS S ON S.runnerID = R.runnerID
WHERE R.RaceID = 133
RESULTS
1 = start
2 = check point
3 = rest point
4 = end
RACEID RUNNERID STOPTYPEID
133 21 1
133 21 4
133 21 3
133 21 2
133 21 2
133 21 2
133 21 2
133 21 2
133 23 2
133 23 2
133 23 2
133 23 2
133 23 4
注意跑步者23缺少类型'1'(开始) 我想要一个缺少数据的比赛中的跑步者名单...如果我说1和4是必需的....
(感谢抱歉不发布所需的'数据')
答案 0 :(得分:1)
对你的结构略有猜测,但是:
SELECT RR.RunnerId
FROM Race AS R
INNER JOIN RaceRunners AS RR
ON <whatever>
LEFT OUTER JOIN Runners AS Starters
On Starters.ID = RR.RunnersID
AND Starters.Type = 'StartPoint' -- don't know how you're specifying this.
LEFT OUTER JOIN Runners AS Finishers
On Finishers.ID = RR.RunnersID
AND Finishers.Type = 'EndPoint' -- don't know how you're specifying this.
WHERE Starters.<Whatever> IS NULL
OR Finishers.<Whatever> IS NULL
答案 1 :(得分:0)
您可以通过这种方式获得两种停止类型(开始,结束)中每个人的停留次数,这也会为您提供一个列,显示跑步者是否有开始或结束登记。
create table #race
(
raceid int,
runnerid int
)
create table #stops
(
runnerid int,
stoptypeid int,
)
insert into #race values(133, 21)
insert into #race values(133, 23)
insert into #race values(133, 20)
insert into #race values(133, 33)
insert into #stops values(21, 1)
insert into #stops values(21, 4)
insert into #stops values(21, 3)
insert into #stops values(21, 2)
insert into #stops values(21, 2)
insert into #stops values(21, 2)
insert into #stops values(21, 2)
insert into #stops values(21, 2)
insert into #stops values(23, 2)
insert into #stops values(23, 2)
insert into #stops values(23, 2)
insert into #stops values(23, 2)
insert into #stops values(23, 4)
insert into #stops values(20, 3)
insert into #stops values(20, 1)
insert into #stops values(33, 4)
insert into #stops values(33, 1)
SELECT r.raceid
, r.runnerid
, (select COUNT(*)
FROM #stops s
WHERE S.runnerID = R.runnerID
AND s.stoptypeid IN (1, 4)) as StartEndStops
, CASE WHEN (SELECT COUNT(*) FROM #stops s WHERE S.runnerID = R.runnerID AND s.stoptypeid = 1) = 1
THEN 'Start Check-in'
ELSE ''
END as 'Start'
, CASE WHEN (SELECT COUNT(*) FROM #stops s WHERE S.runnerID = R.runnerID AND s.stoptypeid = 4) = 1
THEN 'End Check-in'
ELSE ''
END as 'End'
FROM #race R
drop table #race
drop table #stops
结果:
RaceId RunnerId StartEndStops Start End
133 21 2 Start Check-in End Check-in
133 23 1 End Check-in
133 20 1 Start Check-in
133 33 2 Start Check-in End Check-in
答案 2 :(得分:0)
我最终使用了这个 - 虽然我仍然很好奇它是否是最佳解决方案...... 我仍然将'详细'等同于'低效' - 而且我不确定这是一个好心态。 我只是想知道它是否混乱,代码比必须更优雅地说明SQL引擎会“更喜欢”......
当然 - 这是抽象的 - 在Q中运行了很多过滤 - 为了简化'概念',我把它切成了一个简单的分钟 - 谢谢
SELECT DISTINCT RaceID, RunnerID
FROM (
SELECT
raceID
,runnerID
,(
SELECT count(stops.ID)
FROM races
JOIN stops
WHERE raceid = main.raceID
AND STOPTYPEID = 1
) AS poStart
,(
SELECT count(stops.ID)
FROM races
JOIN stops
WHERE raceid = main.raceID
AND STOPTYPEID = 4
) AS poEnd
FROM races
WHERE x BETWEEN '2012-03-01 00:00:00.0' AND '2012-03-31 23:59:59.9'
ORDER BY race
) AS T1 WHERE poStart = 0 OR poEnd = 0