我有一個數據集,其中包含客戶可以接收的服務類型的每月預算。該SERVICES_ID是我們的服務類型,Begin_DT和END_DT給的時間服務是良好的範圍內,COST_PER_MONTH給予每月預算金額,並NUMBER_OF_MONTHS給出的月數了該服務的預算是有益的。根據兩個日期之間的月數添加行數
下面是一個示例數據集,您會注意到有兩個客戶端每個都有兩個預算的服務。一個客戶有12個月兩種服務,另外有一個服務12個月,另一個是6
CREATE TABLE Services (
ID int,
SERVICES_ID int,
Begin_DT datetime,
End_DT datetime,
COST_PER_MONTH int,
NUMBER_OF_MONTHS int)
('10178', 9, '7/21/2011', '7/21/2012' 203.2, 12)
('10178', 10, '7/21/2011', '7/21/2012' 78.2, 12)
('13862', 12, '12/8/2011', '6/3/2012', 469.2, 6)
('13862', 10, '6/3/2011', '6/3/2012', 78.2, 12)
我需要查詢其在月增加和年變量,並增加了對行每個月該服務的預算範圍爲Begin_DT和End_DT。這就是我要找:
CREATE TABLE Services_monthly (
ID int,
SERVICES_ID int,
Begin_DT datetime,
End_DT datetime,
Month int,
Year int,
COST_PER_MONTH int,
NUMBER_OF_MONTHS int)
('10178', 9, '7/21/2011', '7/21/2012', 7, 2011, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 8, 2011, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 9, 2011, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 10, 2011, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 11, 2011, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 12, 2011, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 1, 2012, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 2, 2012, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 3, 2012, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 4, 2012, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 5, 2012, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 6, 2012, 203.2, 12),
('10178', 9, '7/21/2011', '7/21/2012', 7, 2012, 203.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 7, 2011, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 8, 2011, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 9, 2011, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 10, 2011, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 11, 2011, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 12, 2011, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 1, 2012, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 2, 2012, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 3, 2012, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 4, 2012, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 5, 2012, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 6, 2012, 78.2, 12),
('10178', 10, '7/21/2011', '7/21/2012', 7, 2012, 78.2, 12),
('13862', 12, '12/8/2011', '6/3/2012', 12, 2011, 469.2, 6),
('13862', 12, '12/8/2011', '6/3/2012', 1, 2012, 469.2, 6),
('13862', 12, '12/8/2011', '6/3/2012', 2, 2012, 469.2, 6),
('13862', 12, '12/8/2011', '6/3/2012', 3, 2012, 469.2, 6),
('13862', 12, '12/8/2011', '6/3/2012', 4, 2012, 469.2, 6),
('13862', 12, '12/8/2011', '6/3/2012', 5, 2012, 469.2, 6),
('13862', 12, '12/8/2011', '6/3/2012', 6, 2012, 469.2, 6),
('13862', 10, '6/3/2011', '6/3/2012', 6, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 7, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 8, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 9, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 10, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 11, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 12, 2011, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 1, 2012, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 2, 2012, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 3, 2012, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 4, 2012, 78.2, 12),
('13862', 10, '6/3/2011', '6/3/2012', 5, 2012, 78.2, 12)
創建日曆表。從1900年1月1日至2070年12月31日的完整日曆表格,包括元數據和索引,可讓您快速輕鬆地確定哪些日期是星期,月份,年份等的開始日期,並可讓您記錄哪些日期是銀行假期等,然後加入。 – MatBailie
@MatBailie謝謝。這將是一個很好的解決方案,但我沒有權限創建表作爲我的工作。你認爲可能有另一種方式嗎? – user2270911