SSMS 2008合并2个查询

时间:2014-03-25 18:32:35

标签: sql variables merge sql-server-2008

我正在尝试合并两个独立工作的查询,理想情况下优化查询过程。第一个查询返回各种合同信息,最后一个变量“@InYearMonths”确定此财务报告的总月数年。

第一个查询是:

Declare @ReportID int, @OrgId nvarchar(10), @FYStartDate DATETIME, @InYearMonths INT
Select @ReportID = '58', @OrgId = '%', @FYStartDate = '2013-04-01', @InYearMonths = '12'

SELECT  Categories.Category, Contracts.ContractName, ContractUptakeStatus.Description AS     UptakeStatus, ContractUptakeStatus.RAGCode AS UptakeRAG, 
        ContractBenefitActuals.ActualValueCR, ContractBenefitActuals.ActualValueCA, ContractSharing.SavingsStartDate, ContractBenefitOpportunity.AnnualSpend, 
        ContractBenefitOpportunity.AnnualisedOppCR, ContractBenefitOpportunity.AnnualisedOppCA, ContractBenefitOpportunity.CurrentFYCR, 
        ContractBenefitOpportunity.CurrentFYCA, ContractBenefitOpportunity.NextFYCR, ContractBenefitOpportunity.NextFYCA, Categories.CategoryColour, 
        ContractSharing.OrgID, Organisations.Organisation, ContractBenefitReporting.Year, ContractBenefitReporting.Period, ContractBenefitReporting.Version, 
        Categories.CategoryForeColour, Contracts.StartDate, Contracts.EndDate, ContractBenefitActuals.ActualSpend,
         CASE WHEN SavingsStartDate IS NOT NULL THEN CASE WHEN 
        DateDiff(MM, SavingsStartDate, CAST('01/04/' + CAST([Year] - 1 AS NVARCHAR(10)) AS datetime)) between -12 and 0 
        THEN(SELECT ISNULL(SUM(ActualValueCR),0) as InYearCRSavings
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY ContractBenefitActualsDetail.ActualDetailID
ORDER BY ContractBenefitActualsDetail.Period DESC) AS InYearPeriod, *
FROM ContractBenefitActualsDetail
) AS InYearValue
WHERE InYearValue.ActualsID = contractBenefitActuals.actualsid
AND InYearValue.Period <= @InYearMonths) ELSE 0 END ELSE 0 END AS InYearCR,
         ContractUptakeStatus.UptakeStatusID
FROM    ContractBenefitReporting INNER JOIN
        ContractBenefitReporting_SharesInReport ON ContractBenefitReporting.ReportID = ContractBenefitReporting_SharesInReport.ReportID INNER JOIN
        ContractSharing ON ContractBenefitReporting_SharesInReport.ShareID = ContractSharing.ShareID INNER JOIN
        Contracts ON ContractSharing.ContractID = Contracts.ContractID INNER JOIN
        Categories ON Contracts.CategoryID = Categories.CategoryID INNER JOIN
        ContractUptakeStatus ON ContractSharing.UptakeStatusID = ContractUptakeStatus.UptakeStatusID INNER JOIN
        Organisations ON ContractSharing.OrgID = Organisations.OrgID LEFT OUTER JOIN
        ContractBenefitOpportunity ON ContractSharing.ShareID = ContractBenefitOpportunity.ShareID AND 
        ContractBenefitReporting_SharesInReport.BenefitVersion = ContractBenefitOpportunity.Version AND 
        ContractBenefitReporting.Year = ContractBenefitOpportunity.FiscalYear AND ContractBenefitReporting.Period = ContractBenefitOpportunity.Period LEFT OUTER     JOIN
        ContractBenefitActuals ON ContractSharing.ShareID = ContractBenefitActuals.ShareID AND 
        ContractBenefitReporting_SharesInReport.BenefitVersion = ContractBenefitActuals.Version AND 
        ContractBenefitReporting.Year = ContractBenefitActuals.FiscalYear AND ContractBenefitReporting.Period = ContractBenefitActuals.Period
WHERE   (ContractBenefitReporting.ReportID = @ReportID) AND (Organisations.Organisation LIKE @OrgID)

第二个查询用于确定变量'@InYearMonths':

Declare @FYStartdate datetime, @Reportid int
Select @FYStartdate = '2013-04-01', @Reportid = '58'

SELECT     ISNULL(DATEDIFF(MM, dbo.ContractSharing.SavingsStartDate, @FYStartDate), 0) AS     InYearMonths
FROM         dbo.ContractBenefitReporting_SharesInReport INNER JOIN
                      dbo.ContractBenefitActuals ON dbo.ContractBenefitReporting_SharesInReport.BenefitVersion = dbo.ContractBenefitActuals.Version AND 
                      dbo.ContractBenefitReporting_SharesInReport.ShareID = dbo.ContractBenefitActuals.ShareID INNER JOIN
                      dbo.ContractBenefitReporting ON dbo.ContractBenefitReporting_SharesInReport.ReportID = dbo.ContractBenefitReporting.ReportID AND 
                      dbo.ContractBenefitActuals.FiscalYear = dbo.ContractBenefitReporting.Year AND 
                      dbo.ContractBenefitActuals.Period = dbo.ContractBenefitReporting.Period INNER JOIN
                      dbo.ContractSharing ON dbo.ContractBenefitReporting_SharesInReport.ShareID = dbo.ContractSharing.ShareID
WHERE     (dbo.ContractBenefitReporting_SharesInReport.ReportID = @Reportid)

我需要在第一个查询中动态计算这个'@InYearMonths'变量 - 有没有人有任何建议?

1 个答案:

答案 0 :(得分:0)

通过动态,你的意思是在第一个查询中,@ InYearMonths的值可以逐行变化吗?如果不是 - 如果这个变量是单例,我不明白为什么你想要组合这两个查询。似乎第一个查询依赖于第二个来评估@InYearMonths。你能更清楚地澄清两个查询之间的关系吗?