假设我有两张桌子:
People
Id Name FirstDate LastDate
-- -------- --------- --------
A1 Bob 1/1/2007 5/6/2007
A2 Joe 1/4/2008 9/1/2008
.. .. ... ...
PiesBaked
Name Date Value
-------- ---- -----
Bob 1/1/2007 7
Bob 1/2/2007 9
.. ... ...
Joe ... ...
如何构建一个视图,将PiesBaked子集设置为名称的第一个/最后一个日期
Result
Id Name Date Value
-- ---- ---- -----
A1 Bob 1/1/2007 7
A1 Bob 1/2/2007 9
.. .. ... ...
A1 Bob 5/6/2007 ...
.. ... ... ...
答案 0 :(得分:3)
试试这个:
SELECT a.Id, a.Name, b.Date, b.Value
FROM People a INNER JOIN PiesBaked b
ON a.Name = b.Name
AND b.Date BETWEEN a.FirstDate AND a.LastDate
答案 1 :(得分:3)
select p.id, p.name, pb.Date, pb.value
from people p
inner join piesBaked pb
on pb.name = p.name
and pb.date between p.firstDate and p.lastDate
order by p.id, pb.Date