1

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

我有一个表,其中包含年、月和数据。在下面的查询中添加了一个过滤器 c.ReportMonth = DATENAME(month, @12MonthsAgo) 和 c.ReportYear = Year(@12MonthsAgo) 这是一年前的。同样,如果我必须获取上个月和当前月份,我可以通过设置过滤器在同一个查询中做到这一点吗?我们该怎么做?

除了我最终编写 3 个选择查询然后将选择放入 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
4

2 回答 2

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
于 2013-11-01T11:16:46.457 回答
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
于 2013-11-01T11:25:41.310 回答