0

我在 Microsoft Visual Studio 2010 中有一个包含 tablix 的报告。我有一个按月分组的客户销售列表。我想为每个客户添加所有月份的总数。然后我想按总额的降序排序。我已经添加了总计,但我不知道如何对其进行排序。有什么建议么?

这是初始数据集查询:

SELECT
Customer, CustomerName, FiscalMonthNum, FiscalYear, SalesDlr
FROM
CustomerSalesDollars
WHERE
FiscalYear IN ('2013')
ORDER BY
SalesDlr DESC
4

1 回答 1

1
with CSD as (
    select Customer, CustomerName, FiscalMonthNum, FiscalYear, SalesDlr
    from CustomerSalesDollars
    WHERE FiscalYear in ('2013')
), YearlyTotals as (
    select FiscalYear, Customer, CustomerName, SUM(SalesDlr) as YearlyTotal
    from CSD
    group by FiscalYear, Customer, CustomerName
)

select * from YearlyTotals
order by YearlyTotal desc

如果您仍然想要所有月度故障:

with CSD as (
    select Customer, CustomerName, FiscalMonthNum, FiscalYear, SalesDlr
    from CustomerSalesDollars
    WHERE FiscalYear in ('2013')
), YearlyTotals as (
    select FiscalYear, Customer, CustomerName, SUM(SalesDlr) as YearlyTotal
    from CSD
    group by FiscalYear, Customer, CustomerName
)

select CSD.*, YT.YearlyTotal from YearlyTotals YT
join CSD on CSD.FiscalYear = YT.FiscalYear
and CSD.Customer = YT.Customer
and CSD.CustomerName = YT.CustomerName
order by YearlyTotal desc, CSD.SalesDlr desc
于 2013-06-21T00:16:29.477 回答