按日期列的月份和年份分组

时间:2016-08-09 13:47:55

标签: sql-server stored-procedures

我的查询如下

SELECT
    MAX(Reimbursement_EBSUtilization.Id) AS Id,
    ProviderReimbursementRequest.Contractor_Id,
    Reimbursement_EBSUtilization.ServiceMonth,
    fContractor.ContractorName,
    Reimbursement_EBSUtilization.SD_Id,
    MAX(StandardUnits) AS StandardUnits,
    MAX(Rate) AS Rate,
    SUM(Reimbursement_EBSUtilization.UnitsDelivered) AS UnitsDelivered,
    NULL AS ReduceUnits,
    CAST(1 AS bit) AS IsEbs,
    Reimbursement_EBSUtilization.BHFormName,
    fExpenseType.ExpenseType,
    CASE
        WHEN Reimbursement_EBSUtilization.BHFormName IS NULL THEN MAX(Rate) * SUM(Reimbursement_EBSUtilization.UnitsDelivered) * ISNULL(MAX(Reimbursement_EBSUtilization.StandardUnits), 0)
        ELSE (CASE
                WHEN fExpenseType.ExpenseType = 'Payable' THEN SUM(ISNULL(Reimbursement_BHForms.ReimburseAmount, 0)) - SUM(ISNULL(Reimbursement_BHForms.ReducedAmount, 0))
                ELSE 0
            END) -
            (CASE
                WHEN fExpenseType.ExpenseType = 'Offset' THEN SUM(ISNULL(Reimbursement_BHForms.ReimburseAmount, 0)) - SUM(ISNULL(Reimbursement_BHForms.ReducedAmount, 0))
                ELSE 0
            END)
    END AS ReimbursementAmount 
FROM 
    ProviderReimbursementRequest 
LEFT JOIN 
    Reimbursement_EBSUtilization ON ProviderReimbursementRequest.Id = Reimbursement_EBSUtilization.PRR_Id 
LEFT JOIN 
    Reimbursement_BHForms ON Reimbursement_EBSUtilization.Id = Reimbursement_BHForms.REU_Id 
LEFT JOIN 
    fExpenseCategory ON Reimbursement_BHForms.EC_Id = fExpenseCategory.ID 
LEFT JOIN 
    fExpenseType ON fExpenseCategory.ExpenseType = fExpenseType.Id 
LEFT JOIN 
    fContractor ON ProviderReimbursementRequest.Contractor_Id = fContractor.Id 
WHERE 
    MRR_Id = @MrrId 
    AND Reimbursement_EBSUtilization.SD_Id = @ServiceDetailId 
GROUP BY 
    ProviderReimbursementRequest.Contractor_Id,
    Reimbursement_EBSUtilization.ServiceMonth,
    fContractor.ContractorName,
    Reimbursement_EBSUtilization.SD_Id,
    Reimbursement_EBSUtilization.BHFormName,
    fExpenseType.ExpenseType

执行结果时

Id  Contractor_Id   ServiceMonth    ContractorName  SD_Id   StandardUnits   Rate    UnitsDelivered  ReduceUnits IsEbs   BHFormName  ExpenseType ReimbursementAmount
3976    845 2016-05-01  Payments SC1    2867    1.00    10.00   20  NULL    1   NULL    NULL    200.00
3966    845 2016-07-31  Payments SC1    2867    1.00    10.00   NULL    NULL    1   NULL    NULL    NULL
3974    846 2016-07-01  Payments SC2    2867    1.00    10.00   100 NULL    1   NULL    NULL    1000.00
3970    846 2016-07-31  Payments SC2    2867    1.00    10.00   20  NULL    1   NULL    NULL    200.00
3978    847 2016-07-31  Payments SC3    2867    1.00    10.00   30  NULL    1   NULL    NULL    300.00
3983    847 2016-08-01  Payments SC3    2867    1.00    10.00   NULL    NULL    1   NULL    NULL    NULL

如果您观察contractor_id = 846的服务月份列,我们可以看到同月的2条记录。 我希望输出结合这些列与2016-07-01一样,其他与2016-07-31相同,因为它们都属于同一个月和一年。我希望他们能够合并。

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你真的应该养成使用别名和格式化查询的习惯。据发布,查询无法破译。只需要一些别名和一点点格式,它就会更清晰。

select Max(ru.Id) as Id
    , prr.Contractor_Id
    , ru.ServiceMonth
    , c.ContractorName
    , ru.SD_Id
    , Max(StandardUnits) as StandardUnits
    , max(Rate) as Rate
    , sum(ru.UnitsDelivered) as UnitsDelivered
    , null as ReduceUnits
    , Cast(1 as BIT) as IsEbs
    , ru.BHFormName
    , et.ExpenseType
    , case when ru.BHFormName is null 
        then max(Rate) * sum(ru.UnitsDelivered) * ISNULL(max(ru.StandardUnits),0) 
        else 
        (
            case when et.ExpenseType = 'Payable' 
                then sum(ISNULL(f.ReimburseAmount,0)) - sum(ISNULL(f.ReducedAmount,0))
                else 0
            end 
        ) - 
        (
            case when et.ExpenseType = 'Offset' 
                then sum(ISNULL(f.ReimburseAmount,0)) - sum(ISNULL(f.ReducedAmount,0))
                else 0 
            end
        ) end as ReimbursementAmount
from ProviderReimbursementRequest prr
left join Reimbursement_EBSUtilization ru on prr.Id = ru.PRR_Id
left join Reimbursement_BHForms f on ru.Id = f.REU_Id
left join fExpenseCategory ec on f.EC_Id = ec.ID
left join fExpenseType et on ec.ExpenseType = et.Id
left join fContractor c on prr.Contractor_Id = c.Id
where MRR_Id = @MrrId 
    and ru.SD_Id = @ServiceDetailId
group by prr.Contractor_Id
    , ru.ServiceMonth
    , c.ContractorName
    , ru.SD_Id
    , ru.BHFormName
    , et.ExpenseType

要真正帮助解决您的问题,我认为我们需要更多细节。这是一个很好的起点。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/

- 编辑 -

如果我理解了这个问题,您需要在ru.ServiceMonth中按月份分组,而不是实际值。

像这样。

dateadd(month, datediff(month, 0, ru.ServiceMonth), 0)