我有一个包含报告项目系列的表格,如下所示:
Reporting Name Starting Number Ending Number
AW 965845 -
GRT 6700 6705
QEF 2011 -
我希望在Access中输出下面的表格,但如果需要,我可以从SQL转换为Access。
Output:
Combined Ref
AW 965845
GRT 6700
GRT 6701
GRT 6702
GRT 6703
GRT 6704
GRT 6705
QEF 2011
你能帮帮我吗?
答案 0 :(得分:1)
我唯一知道如何在Access中执行此操作的方法是使用计数表。该表将包含您需要的所有可能数字,1到?
create table tallynumbers (nbrs INTEGER)
您可以通过Excel轻松加载它。
然后你就可以这样加入:
select ReportingName, nbr
from reporting_items as ri, tallynumbers as tn
where tn.nbr between ri.[Starting Number] and iif(IsNull(ri.[Ending Number]),ri.[Starting Number],ri.[Ending Number])
<强>更新强> 如果你可以在SQL Server 2005+中完成所有这些,那么这是另一个选择
这是报告表
create table reports (ReportingName varchar(10), StartingNumber int, EndingNumber int)
现在用样本值填充
insert into reports
values ('AW', 965845, null),
('GRT', 6700, 6705),
('QEF', 2011, null)
现在让递归CTE将其转换为您想要的格式
;with RecurReports
as
(
select ReportingName, StartingNumber as nbr,
coalesce(EndingNumber,startingnumber) as enbr
from reports
union all select rr.ReportingName, nbr +1, enbr
from reports r
inner join RecurReports rr
on r.ReportingName=rr.ReportingName
where nbr < enbr
)
select ReportingName, nbr as Number from RecurReports
这是SQL小提琴:http://www.sqlfiddle.com/#!6/91137/1