0

我想创建一个临时表来存储这个查询,以便我可以更新另一个表。

SELECT SaginawUtilityData.ServiceFrom, SaginawUtilityData.ServiceThru, 

BillingMonth = dbo.fn_MonthWithMostDaysInRange(ServiceFrom, ServiceThru),

SaginawUtilityData.Usage, SaginawUtilityData.UtilityCharges

FROM SaginawUtilityData

JOIN tblMEP_CustomerAccounts
ON SaginawUtilityData.AccountNumber = tblMEP_CustomerAccounts.AccountNumber

JOIN tblMEP_Customers
ON tblMEP_CustomerAccounts.CustomerID = tblMEP_Customers.ID

JOIN tblMEP_UtilityCompanies
ON tblMEP_UtilityCompanies.ID = tblMEP_CustomerAccounts.UtilityCompanyID

JOIN tblMEP_Meters
ON tblMEP_CustomerAccounts.ID = tblMEP_Meters.CustomerAccountID
WHERE tblMEP_Customers.ID = 43

如果您可以只进行更新,那就太好了:这是另一个表,我想从上面的表中插入到这个表中的列:

SELECT tblMEP_MonthlyDATA.CycleStartDate, tblMEP_MonthlyDATA.CycleEndDate, 

tblMEP_MonthlyDATA.BillingMonth, tblMEP_MonthlyDATA.Consumption,

tblMEP_MonthlyDATA.Charge

FROM tblMEP_MonthlyData

谢谢。

4

1 回答 1

1

我不确定我是否真的理解您的要求,但我认为您在问如何将顶部查询中的数据直接放入底部查询中显示的表列中,因此:

INSERT tblMEP_MonthlyDATA (
    CycleStartDate, CycleEndDate, BillingMonth, Consumption, Charge)
SELECT 
    SaginawUtilityData.ServiceFrom, 
    SaginawUtilityData.ServiceThru, 
    dbo.fn_MonthWithMostDaysInRange(ServiceFrom, ServiceThru),
    SaginawUtilityData.Usage, 
    SaginawUtilityData.UtilityCharges
FROM SaginawUtilityData
    JOIN tblMEP_CustomerAccounts ON SaginawUtilityData.AccountNumber = 
                                           tblMEP_CustomerAccounts.AccountNumber
    JOIN tblMEP_Customers ON tblMEP_CustomerAccounts.CustomerID = tblMEP_Customers.ID
    JOIN tblMEP_UtilityCompanies ON tblMEP_UtilityCompanies.ID = 
                                           tblMEP_CustomerAccounts.UtilityCompanyID
    JOIN tblMEP_Meters ON tblMEP_CustomerAccounts.ID = tblMEP_Meters.CustomerAccountID
WHERE 
    tblMEP_Customers.ID = 43 --or other set of conditions

希望这会有所帮助(或者至少与您想知道的内容模糊相关)

于 2013-07-03T22:36:50.497 回答