Union Order通过使用不同的字段SQL Server

时间:2017-10-30 01:29:05

标签: sql sql-server tsql

我试图通过使用Order By对下面的查询进行排序。但是当我使用MonthYear字段时,输出结果显示如下,不按最新年份和月份排序。

按月份订单输出。

 MonthYear
    2017/9
    2017/8
    2017/7
    2017/6
    2017/5
    2017/4
    2017/10

所以我想按年份和月份的顺序尝试,但我不能这样做,因为联合之后的另一个查询所有行都有不同的字段,即a.created_year。有什么办法可以吗?

SELECT      CONVERT(varchar, A.YEAR) + '/' +  CONVERT(varchar, A.MONTH)AS MonthYear
FROM            PAY_Previous AS A LEFT OUTER JOIN
                         EMPLOYEE_BADGE AS B ON A.EMPLOYEE_NO = B.EMPLOYEE_NO LEFT OUTER JOIN
                         V_EMPLOYEE_PROFILES AS C ON A.EMPLOYEE_NO = C.EMPLOYEE_NO
WHERE   A.EMPLOYEE_NO = '710049' AND      A.FREQUENCY = 2 and (day(getdate())>=28 or month(getdate()) > a.Month or YEAR(getdate()) > A.YEAR )



UNION ALL-

SELECT       CONVERT(varchar, YEAR(A.CREATED_DATE)) + '/' + CONVERT(varchar, A.MONTH) AS MonthYear
FROM            PAY_Current AS A LEFT OUTER JOIN
                         EMPLOYEE_BADGE AS B ON A.EMPLOYEE_NO = B.EMPLOYEE_NO LEFT OUTER JOIN
                         V_EMPLOYEE_PROFILES AS C ON A.EMPLOYEE_NO = C.EMPLOYEE_NO
WHERE   A.EMPLOYEE_NO = '710049'AND  A.FREQUENCY = 2  and (day(getdate())>=28 or month(getdate()) > a.Month or YEAR(getdate()) > A.CREATED_DATE )

ORDER BY YEAR(A.CREATED_DATE) DESC ,A.MONTH DESC

预期产出

 MonthYear
    2017/10
    2017/9
    2017/8
    2017/7
    2017/6
    2017/5
    2017/4

2 个答案:

答案 0 :(得分:1)

将数据保留为数字,直到最后一刻,然后只转换最小的列表进行演示,这样就可以使用数字列来排序结果。

SELECT CONVERT(varchar, d.[YEAR]) + '/' + CONVERT(varchar, d.[MONTH]) AS MonthYear
FROM 
    SELECT
            A.[YEAR] , A.[MONTH]
    FROM PAY_Previous AS A
    LEFT OUTER JOIN EMPLOYEE_BADGE AS B ON A.EMPLOYEE_NO = B.EMPLOYEE_NO
    LEFT OUTER JOIN V_EMPLOYEE_PROFILES AS C ON A.EMPLOYEE_NO = C.EMPLOYEE_NO
    WHERE A.EMPLOYEE_NO = '710049'
    AND A.FREQUENCY = 2
    AND (DAY(GETDATE()) >= 28
    OR MONTH(GETDATE()) > a.Month
    OR YEAR(GETDATE()) > A.YEAR)

    UNION

    SELECT
            YEAR(A.CREATED_DATE) [Year] , A.[MONTH]
    FROM PAY_Current AS A
    LEFT OUTER JOIN EMPLOYEE_BADGE AS B ON A.EMPLOYEE_NO = B.EMPLOYEE_NO
    LEFT OUTER JOIN V_EMPLOYEE_PROFILES AS C ON A.EMPLOYEE_NO = C.EMPLOYEE_NO
    WHERE A.EMPLOYEE_NO = '710049'
    AND A.FREQUENCY = 2
    AND (DAY(GETDATE()) >= 28
    OR MONTH(GETDATE()) > a.Month
    OR YEAR(GETDATE()) > A.CREATED_DATE)
) d
ORDER BY d.[YEAR] DESC, d.[MONTH] DESC

顺便说一下:两年"年"和" MONTH"是T-SQL使用的术语,将它们用作列名称不是一个好主意。

如果您需要以可以可靠地重新排序的形式获得结果,那么您可以考虑使用月份数的前导零。 e.g。

CONVERT(varchar(4), d.[YEAR]) + '/' + RIGHT('0' + CONVERT(varchar(2), d.[MONTH]),2) AS MonthYear

哪个看起来像这样(并正确排序)

2017/10
2017/04
2017/03
2017/02
2017/01
2016/10
2016/05

答案 1 :(得分:0)

您可能遇到排序问题,因为您正在使用Created_Date进行排序,但是您在语句之间使用了不同的MonthYear字段。

试试这个:

select MonthYear from (
    SELECT      CONVERT(varchar, A.YEAR) + '/' +  CONVERT(varchar, A.MONTH)AS MonthYear, A.YEAR yr, A.MONTH mon
    FROM            PAY_Previous AS A LEFT OUTER JOIN
                             EMPLOYEE_BADGE AS B ON A.EMPLOYEE_NO = B.EMPLOYEE_NO LEFT OUTER JOIN
                             V_EMPLOYEE_PROFILES AS C ON A.EMPLOYEE_NO = C.EMPLOYEE_NO
    WHERE   A.EMPLOYEE_NO = '710049' AND      A.FREQUENCY = 2 and (day(getdate())>=28 or month(getdate()) > a.Month or YEAR(getdate()) > A.YEAR )

    UNION ALL

    SELECT       CONVERT(varchar, YEAR(A.CREATED_DATE)) + '/' + CONVERT(varchar, A.MONTH) AS MonthYear. YEAR(A.CREATED_DATE)) yr, CONVERT(varchar, A.MONTH) mon
    FROM            PAY_Current AS A LEFT OUTER JOIN
                             EMPLOYEE_BADGE AS B ON A.EMPLOYEE_NO = B.EMPLOYEE_NO LEFT OUTER JOIN
                             V_EMPLOYEE_PROFILES AS C ON A.EMPLOYEE_NO = C.EMPLOYEE_NO
    WHERE   A.EMPLOYEE_NO = '710049'AND  A.FREQUENCY = 2  and (day(getdate())>=28 or month(getdate()) > a.Month or YEAR(getdate()) > A.CREATED_DATE )
    )x

ORDER BY yr DESC, mon DESC