【发布时间】:2015-06-10 15:30:32
【问题描述】:
我有两张桌子Cal_date 和RPT_Invoice_Shipped。
表cal_data 有列
month_no
start_date
end_date
表RPT_Invoice_Shipped 有列
Day_No
Date
Div_code
Total_Invoiced
Shipped_Value
Line_Shipped
Unit_Shipped
Transaction_Date
我正在使用下面的插入语句在RPT_Invoice_Shipped 表中插入数据。
insert into [Global_Report_Staging].[dbo].[RPT_Invoice_Shipped]
(Day_No, Date, Div_code, Total_Invoiced, Transaction_Date)
select , CONVERT(DATE,Getdate()) as Date, LTRIM(RTRIM(div_Code)),
sum(tot_Net_Amt) as Total_Invoiced, (dateadd(day, -1, convert(date, getdate())))
from [Global_Report_Staging].[dbo].[STG_Shipped_Invoiced]
WHERE CONVERT(DATE,Created_date )=CONVERT(DATE,Getdate())
group by div_code
在RPT_Invoice_Shipped 表的Day_No 列中插入时,
我必须使用公式Transaction_Date-start_date+1,其中Transaction_Date 来自STG_Shipped_Invoiced,start_date 来自Cal_date 表。
我使用的是datepart (mm, Transaction_Date),所以它给出了month_no,而这个month_no我们可以加入Cal_date表中的month_no并从Cal_date表中获取start_date,这样我们就可以使用@ 987654343@ 为公式Transaction_Date-start_date+1。
但是我很难在上面的查询中安排这个。
您能指导我如何实现这一目标吗?
提前致谢
【问题讨论】:
-
cal_data在给定月份是否有一行?
标签: sql sql-server sql-server-2008 tsql