我有一个带有句点列的表格,该列每月需要分成多行
表中有很多这样的记录:
ID, PERIOD, AMOUNT
1, 201812-201902, 300.00
2, 201804-201903, 1150.00
3, 201901-201902, 200.00
我需要以下查询结果:
1, 12-2018, 100.00
1, 01-2019, 100.00
1, 02-2019, 100.00
2, 04-2018, 95.83
2, 05-2018, 95.83
2, 06-2018, 95.83
2, 07-2018, 95.83
2, 08-2018, 95.83
2, 09-2018, 95.83
2, 10-2018, 95.83
2, 11-2018, 95.83
2, 12-2018, 95.83
2, 01-2019, 95.83
2, 02-2019, 95.83
2, 03-2019, 95.83
3, 01-2019, 100.00
3, 02-2019, 100.00
如何在MS-Access SQL中实现这一目标?
答案 0 :(得分:1)
您可以使用一系列查询(我手头有)来创建它:
将其另存为十:
SELECT DISTINCT Abs([id] Mod 10) AS N
FROM MSysObjects;
将其另存为 MonthDateRange :
PARAMETERS
[DateStart] DateTime, [DateEnd] DateTime;
SELECT
[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000 AS Id, [DateStart] AS DateStart, [DateEnd] AS DateEnd, DateAdd("m",[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000,[DateStart]) AS DateMonth
FROM
Ten AS Ten_0, Ten AS Ten_1, Ten AS Ten_2, Ten AS Ten_3, Ten AS Ten_4, Ten AS Ten_5, Ten AS Ten_6
WHERE
(((DateAdd("m",[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000,[DateStart]))<=DateAdd("m",DateDiff("m",[DateStart],DateAdd("d",-1,[DateEnd])),[DateStart])) AND ((Ten_0.N)<=DateDiff("m",[DateStart],[DateEnd])\1) AND ((Ten_1.N)<=DateDiff("m",[DateStart],[DateEnd])\10) AND ((Ten_2.N)<=DateDiff("m",[DateStart],[DateEnd])\100) AND ((Ten_3.N)<=DateDiff("m",[DateStart],[DateEnd])\1000) AND ((Ten_4.N)<=DateDiff("m",[DateStart],[DateEnd])\10000) AND ((Ten_5.N)<=DateDiff("m",[DateStart],[DateEnd])\100000) AND ((Ten_6.N)<=DateDiff("m",[DateStart],[DateEnd])\1000000));
然后使用表期间
建立最终查询:SELECT
Periods.Id, MonthsDateRange.DateMonth AS Period, Periods.Amount
FROM
Periods, MonthsDateRange
WHERE
(((MonthsDateRange.DateMonth) Between CDate(Format(Left([Period],6),"@@@@\/@@")) And CDate(Format(Right([Period],6),"@@@@\/@@"))))
ORDER BY
Periods.Id, MonthsDateRange.DateMonth;
现在,将其称为传递的起始日期和结束日期超出了您的时间范围,例如2000-01-01和2020-01-01,则输出将是:
要在期间中分配金额,请包含一个子查询:
SELECT
Periods.Id, MonthsDateRange.DateMonth AS Period,
[Amount]/(Select Count(*) From MonthsDateRange As T Where T.DateMonth Between CDate(Format(Left([Period],6),"@@@@/@@")) And CDate(Format(Right([Period],6),"@@@@/@@"))) AS AmountPeriod
FROM
Periods, MonthsDateRange
WHERE
(((MonthsDateRange.DateMonth) Between CDate(Format(Left([Period],6),"@@@@\/@@")) And CDate(Format(Right([Period],6),"@@@@\/@@"))))
ORDER BY
Periods.Id, MonthsDateRange.DateMonth;
获得此输出: