在合并查询中按月和年排序

时间:2018-09-05 05:09:52

标签: sql sql-server sql-order-by

这是我的查询

    SELECT DateName
    ,SubTotal
    ,Discount
    ,TotalAmount
    ,Investment
    ,(CASE WHEN Expense.expense IS NULL THEN 0 ELSE Expense.expense END) + (CASE WHEN MonthlyExpense.Expense IS NULL THEN 0 ELSE Monthlyexpense.Expense END) Expense
    ,Profit - ((CASE WHEN Expense.expense IS NULL THEN 0 ELSE Expense.expense END) + (CASE WHEN MonthlyExpense.Expense IS NULL THEN 0 ELSE Monthlyexpense.Expense END)) AS NetProfit
    ,TotalAmount - ((CASE WHEN Expense.expense IS NULL THEN 0 ELSE Expense.expense END) + (CASE WHEN MonthlyExpense.Expense IS NULL THEN 0 ELSE Monthlyexpense.Expense END)) AS NetCash
FROM (
    SELECT datepart(month, SaleDate) Month
        ,datename(year, SaleDate) Year
        ,datename(month, SaleDate) + '-' + datename(year, SaleDate) AS DateName
        ,SUM(SubTotal) SubTotal
        ,SUM(Discount) AS Discount
        ,SUM(TotalAmount) AS TotalAmount
        ,SUM(Investment) AS Investment
        ,SUM(Profit) AS Profit
    FROM (
        SELECT TOP 100 PERCENT NS.SaleID
            ,SaleDate
            ,Max(NS.SubTotal) AS SubTotal
            ,Max(NS.Discount) AS Discount
            ,Max(NS.TotalAmount) AS TotalAmount
            ,SUM(NSD.Investment) AS Investment
            ,SUM(NSD.Profit) - Max(NS.Discount) AS Profit
        FROM [Sales] NS
        INNER JOIN [SalesDetail] NSD ON NS.SaleID = NSD.SaleID
        WHERE NS.SaleDate >= '01-06-2015 08:45:59 PM'
            AND NS.SaleDate < '01-06-2015 12:00:00 AM'
        GROUP BY NS.SaleID
            ,SaleDate
        ORDER BY DatePart(month, SaleDate)
        ) T
    GROUP BY datename(year, SaleDate)
        ,datepart(month, SaleDate)
        ,datename(month, SaleDate) + '-' + datename(year, SaleDate)
    ) Revenue
LEFT JOIN (
    SELECT datepart(month, ExpenseDate) Month
        ,datename(year, ExpenseDate) Year
        ,SUM(Amount) AS Expense
    FROM DailyExpenses
    WHERE ExpenseDate >= '01-06-2015 08:45:59 PM'
        AND ExpenseDate < '01-06-2015 12:00:00 AM'
    GROUP BY datepart(month, ExpenseDate)
        ,datename(year, ExpenseDate)
    ) Expense ON Expense.Month = Revenue.Month
    AND Expense.Year = Revenue.Year
LEFT JOIN (
    SELECT Month
        ,[Year]
        ,SUM(Amount) AS Expense
    FROM MonthlyExpenses
    WHERE ExpenseDate >= '01-06-2015 08:45:59 PM'
        AND ExpenseDate < '01-06-2015 12:00:00 AM'
    GROUP BY Month
        ,[Year]
    ) MonthlyExpense ON MonthlyExpense.Month = Revenue.Month
    AND MonthlyExpense.Year = Revenue.Year

这里月份和年份合并。 Here is the output which i have.不知道在哪里下订单。我想按月顺序获得结果。

January-2015
February-2015
March-2015
----
---
---
February-2018
March-2018
April-2018

3 个答案:

答案 0 :(得分:1)

Select DateName
    ,SubTotal
    ,Discount
    ,TotalAmount
    ,Investment
    ,NetCash
    from(
SELECT Year
    ,Month
    ,DateName
    ,SubTotal
    ,Discount
    ,TotalAmount
    ,Investment
    ,(CASE WHEN ... END)) AS NetCash
FROM (
    ...
    ) Revenue
LEFT JOIN (
    ...
    ) Expense ON Expense.Month = Revenue.Month
    AND Expense.Year = Revenue.Year
LEFT JOIN (
    ...
    ) MonthlyExpense ON MonthlyExpense.Month = Revenue.Month
    AND MonthlyExpense.Year = Revenue.Year
    ) sql1 
    order by Year, Month

答案 1 :(得分:1)

在如下所示的选择中添加其他列,并按如下所示通过查询添加订单,

SELECT DateName,
     CAST(('01-'+DateName) as Date) as dateOrderCol
    ,SubTotal
    ,Discount
    ,TotalAmount
    ,
    ......
    order by dateOrderCol asc

答案 2 :(得分:0)

只需按Revenue.Month, Revenue.Month下订单

SELECT DateName
            ,SubTotal
            ,Discount
            ,TotalAmount
            ,Investment
            ,(CASE WHEN Expense.expense IS NULL THEN 0 ELSE Expense.expense END) + (CASE WHEN MonthlyExpense.Expense IS NULL THEN 0 ELSE Monthlyexpense.Expense END) Expense
            ,Profit - ((CASE WHEN Expense.expense IS NULL THEN 0 ELSE Expense.expense END) + (CASE WHEN MonthlyExpense.Expense IS NULL THEN 0 ELSE Monthlyexpense.Expense END)) AS NetProfit
            ,TotalAmount - ((CASE WHEN Expense.expense IS NULL THEN 0 ELSE Expense.expense END) + (CASE WHEN MonthlyExpense.Expense IS NULL THEN 0 ELSE Monthlyexpense.Expense END)) AS NetCash
        FROM (
            SELECT datepart(month, SaleDate) Month
                ,datename(year, SaleDate) Year
                ,datename(month, SaleDate) + '-' + datename(year, SaleDate) AS DateName
                ,SUM(SubTotal) SubTotal
                ,SUM(Discount) AS Discount
                ,SUM(TotalAmount) AS TotalAmount
                ,SUM(Investment) AS Investment
                ,SUM(Profit) AS Profit
            FROM (
                SELECT TOP 100 PERCENT NS.SaleID
                    ,SaleDate
                    ,Max(NS.SubTotal) AS SubTotal
                    ,Max(NS.Discount) AS Discount
                    ,Max(NS.TotalAmount) AS TotalAmount
                    ,SUM(NSD.Investment) AS Investment
                    ,SUM(NSD.Profit) - Max(NS.Discount) AS Profit
                FROM [Sales] NS
                INNER JOIN [SalesDetail] NSD ON NS.SaleID = NSD.SaleID
                WHERE NS.SaleDate >= '01-06-2015 08:45:59 PM'
                    AND NS.SaleDate < '01-06-2015 12:00:00 AM'
                GROUP BY NS.SaleID
                    ,SaleDate
                ORDER BY DatePart(month, SaleDate)
                ) T
            GROUP BY datename(year, SaleDate)
                ,datepart(month, SaleDate)
                ,datename(month, SaleDate) + '-' + datename(year, SaleDate)
            ) Revenue
        LEFT JOIN (
            SELECT datepart(month, ExpenseDate) Month
                ,datename(year, ExpenseDate) Year
                ,SUM(Amount) AS Expense
            FROM DailyExpenses
            WHERE ExpenseDate >= '01-06-2015 08:45:59 PM'
                AND ExpenseDate < '01-06-2015 12:00:00 AM'
            GROUP BY datepart(month, ExpenseDate)
                ,datename(year, ExpenseDate)
            ) Expense ON Expense.Month = Revenue.Month
            AND Expense.Year = Revenue.Year
        LEFT JOIN (
            SELECT Month
                ,[Year]
                ,SUM(Amount) AS Expense
            FROM MonthlyExpenses
            WHERE ExpenseDate >= '01-06-2015 08:45:59 PM'
                AND ExpenseDate < '01-06-2015 12:00:00 AM'
            GROUP BY Month
                ,[Year]
            ) MonthlyExpense ON MonthlyExpense.Month = Revenue.Month
            AND MonthlyExpense.Year = Revenue.Year
                                                                                                                                                        where ProductCode=SP.ProductCode  and PackageCode=SP.PackageCode and FranchiseID=39802),0) > 0
        order by Revenue.Month, Revenue.Month