2013-08-27 93 views
0

我有一個數據集,其中包含客戶可以接收的服務類型的每月預算。該SERVICES_ID是我們的服務類型,Begin_DTEND_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_DTEnd_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) 
+0

創建日曆表。從1900年1月1日至2070年12月31日的完整日曆表格,包括元數據和索引,可讓您快速輕鬆地確定哪些日期是星期,月份,年份等的開始日期,並可讓您記錄哪些日期是銀行假期等,然後加入。 – MatBailie

+0

@MatBailie謝謝。這將是一個很好的解決方案,但我沒有權限創建表作爲我的工作。你認爲可能有另一種方式嗎? – user2270911

回答

0

請試試這個:

with number_cte (number) as 
(select distinct number from master..spt_values where number between 1 and 1000) 

Select ID, SERVICES_ID,Begin_DT, End_DT,COST_PER_MONTH , NUMBER_OF_MONTHS 
from Services_monthly s cross join number_cte n 
where n.number <= s.NUMBER_OF_MONTHS 

而且,最好的解決辦法是創建一個表變量,並用數字來填充它。

+0

是否可以使用上面的查詢創建一個Month和Year變量? – user2270911

1

查詢:

SQLFIDDLEExample

SELECT ID, 
     SERVICES_ID, 
     Begin_DT, 
     End_DT, 
     MONTH(DATEADD(MONTH,n.number, Begin_DT)) AS Month, 
     YEAR(DATEADD(MONTH,n.number, Begin_DT)) AS Year, 
     COST_PER_MONTH , 
     NUMBER_OF_MONTHS 
FROM Services s, 
(SELECT DISTINCT number 
FROM master..spt_values 
WHERE number >= 0 
AND number < 100) n 
WHERE n.number <= s.NUMBER_OF_MONTHS 

你的結果是錯誤的,你需要最後一行添加到您的數據:

| 13862 |   10 |  June, 03 2011 00:00:00+0000 | June, 03 2012 00:00:00+0000 |  6 | 2012 |   78.2 |    12 | 

我覺得我的小提琴就像你想要的。

相關問題