SQL自连接多次
我有一个存储周条目的数据库表。
Id Value WeekId
1 1.0000 1
2 2.0000 1
同一周最多可以有三个条目。
所以我认为使用自连接可以解决这个
SELECT w1.Value, w2.Value, w3.Value
FROM [List].[dbo].[testWeekEntries] as w1
LEFT OUTER JOIN [List].[dbo].[testWeekEntries] as w2 ON w1.WeekId = w2.weekId
LEFT OUTER JOIN [List].[dbo].[testWeekEntries] as w3 ON w2.WeekId = w3.WeekId
WHERE w1.Id < w2.Id AND w2.Id < w3.Id
问题:它可以在最大数量的条目下正常工作,但是它不会拉回具有一两个条目的行。
是否有不同类型的连接可以用来拉回只有一个或两个条目的行,或者是否有不同的方法来解决这个问题?
I have a single database table that stores week entries.
Id Value WeekId
1 1.0000 1
2 2.0000 1
There can be up to three entries with the same week.
So I figured using a self join would solve this
SELECT w1.Value, w2.Value, w3.Value
FROM [List].[dbo].[testWeekEntries] as w1
LEFT OUTER JOIN [List].[dbo].[testWeekEntries] as w2 ON w1.WeekId = w2.weekId
LEFT OUTER JOIN [List].[dbo].[testWeekEntries] as w3 ON w2.WeekId = w3.WeekId
WHERE w1.Id < w2.Id AND w2.Id < w3.Id
The problem: It worls fine with the maximum number of entries however it doesn't pull back a row with one or two entries.
Is there a different type of join I can use to pull back a row with only one or two entries or a different way of approaching this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些条目不会返回,因为当连接表返回 NULL 值时,您的
WHERE
子句会显式过滤掉它们。此解决方案向每条记录添加一个连续的行号,每周重新开始为 1。 中使用此序列号
这允许您在 PIVOT 语句SQL 2000 语句
SQL 2008 语句
These entries are not returning because your
WHERE
clause explicitly filters them out when the joined tables returnNULL
values.This solution adds a sequential rownumber to each record, restarting to 1 for each week. This allows you to use this sequential number in a PIVOT statement
SQL 2000 Statement
SQL 2008 Statement
您需要在 where 子句中添加
w2.Id is null
或w3.id is null
的可能性,所以类似
You will need to add in your where clause the possibility that
w2.Id is null
orw3.id is null
So something like
您还可以使用
PIVOT
You can also use
PIVOT