如何在单个查询中执行多个计算

时间:2013-11-01 10:48:37

标签: sql-server-2008 tsql

我有一种情况,我必须从年前,上个月和本月获取数据。实现这一目标的最佳方法是什么?

我有一张表格,其中包含年份,月份和数据。在下面的查询中添加了一个过滤器  c.ReportMonth = DATENAME(月,@ 12MonthsAgo)和c.ReportYear =年(@ 12MonthsAgo) 这是一年前的事了。以同样的方式,如果我必须得到上个月和当前月份,我可以通过设置过滤器在同一查询中执行此操作吗?我们该怎么做?

除了我最终编写3个选择查询然后将select放到tmp表并稍后合并表之外,还有更好的方法吗?

create table #TPTABLE
(
    KPIName varchar(150)
    ,MetricName Varchar(200)
    ,MetricId INT
    ,DataSource varchar(50)
    ,[AnYearAgo] Float
    ,[PreviousMonth]  float
    ,[CurrentMonth] float
);


insert into #TPTABLE 
(KPIName,MetricName,MetricId,DataSource,[AnYearAgo])
SELECT
     p.KPIName
    ,p.MetricName
    ,p.MetricId
    ,p.DataSource
    ,c.Value as [AnYearAgo]
    FROM [IntegratedCare].[report].[KPIMetricDetails] p
    LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c
    ON p.[MetricId] = c.MetricId
    WHERE c.ReportMonth = DATENAME(month, @12MonthsAgo) and c.ReportYear = Year(@12MonthsAgo) 
    ORDER BY KPI_Id ASC, [MetricId] ASC

2 个答案:

答案 0 :(得分:0)

我认为你需要的是:

insert into #TPTABLE 
(KPIName,MetricName,MetricId,DataSource,[AnYearAgo])
SELECT
    KPIName
    ,MetricName
    ,MetricId
    ,DataSource
    ,[AnYearAgo]
    ,[PreviousMonth]
    ,[CurrentMonth]
FROM (
    SELECT
        KPIName
        ,MetricName
        ,MetricId
        ,DataSource
        ,KPI_Id
        ,sum(case when c.ReportMonth = DATENAME(month, @12MonthsAgo) and c.ReportYear = Year(@12MonthsAgo) then c.Value else 0 end) as [AnYearAgo]
        ,sum(case when c.ReportMonth = DATENAME(month, @PreviousMonth) and c.ReportYear = Year(@PreviousMonth) then c.Value else 0 end) as [PreviousMonth]
        ,sum(case when c.ReportMonth = DATENAME(month, @CurrentMonth) and c.ReportYear = Year(@CurrentMonth) then c.Value else 0 end) as [CurrentMonth]
    FROM [IntegratedCare].[report].[KPIMetricDetails] p
    LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c
    ON p.[MetricId] = c.MetricId
    GROUP BY KPIName, MetricName, MetricId, DataSource, KPI_Id
ORDER BY KPI_Id ASC, [MetricId] ASC

答案 1 :(得分:0)

SELECT
     p.KPIName
    ,p.MetricName
    ,p.MetricId
    ,p.DataSource
    ,c.Value
    ,c2.Value
    ,c3.Value
    FROM [IntegratedCare].[report].[KPIMetricDetails] p
    LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c
    ON p.[MetricId] = c.MetricId
        AND c.[CommissionerCode] = COALESCE(NULLIF(@Commissioner, ''), c.[CommissionerCode])
        ANd ReportMonth = DATENAME(month, @12MonthsAgo) and c.ReportYear = Year(@12MonthsAgo)
    LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c2
    ON p.[MetricId] = c2.MetricId
        AND c2.[CommissionerCode] = COALESCE(NULLIF(@Commissioner, ''), c2.[CommissionerCode])
        ANd c2.ReportMonth = DATENAME(month, @PreviousMonth) and c2.ReportYear = Year(@PreviousMonth)
    LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c3
    ON p.[MetricId] = c3.MetricId
        AND c3.[CommissionerCode] = COALESCE(NULLIF(@Commissioner, ''), c3.[CommissionerCode])
        ANd c3.ReportMonth = DATENAME(month, @PreviousMonth) and c3.ReportYear = Year(@PreviousMonth)
    ORDER BY p.KPI_Id ASC, p.[MetricId] ASC