sql SORT BY对UNION查询

时间:2018-12-27 10:04:17

标签: sql

实际上,我尝试对我的sql进行排序,但没有排序

我需要按TSTATUS添加SORT

 $sql="
(
    SELECT
        tt.tstatus,
        tt.ticketnbr,
        tt.col1,
        tt.col2,
        NULL as col3,
        tt.col4,
        tt.col5,
        tt.col6,
        tt.col7,
        tt.col8,
        'cmg' as tickettype,
        CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
    FROM
        aradmin.cmg_troubleticket tt
        LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
            ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
    WHERE
      ( tt.TSTATUS < 9 )
      {$customer_list}             
) UNION (
    SELECT
        tt.tstatus,
        tt.ticketnbr,
        tt.col1,
        tt.col2,
        tt.col3,
        tt.col4,
        tt.col5,
        tt.col6,
        tt.col7,
        tt.col8,
        'ar' as tickettype,
        CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
    FROM
        aradmin.ar_troubleticket tt
        LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
            ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
    WHERE
        ( tt.TSTATUS < 10 )
        {$customer_list} 
)";

3 个答案:

答案 0 :(得分:0)

只需在查询末尾附加recyclerView即可获得排序结果。

答案 1 :(得分:0)

创建UNION结果的虚拟表,然后按所需的属性进行排序。试试这个:

SELECT * FROM (
              SELECT
                tt.tstatus,
                tt.ticketnbr,
                tt.col1,
                tt.col2,
                NULL as col3,
                tt.col4,
                tt.col5,
                tt.col6,
                tt.col7,
                tt.col8,
                'cmg' as tickettype,
              CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
              FROM
              aradmin.cmg_troubleticket tt
                LEFT JOIN
                  ARADMIN.TT_CUSTOMERLOGENTRY cl 
            ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
        WHERE
          ( tt.TSTATUS < 9 )
          {$customer_list} 

      ) UNION (
        SELECT
          tt.tstatus,
          tt.ticketnbr,
          tt.col1,
          tt.col2,
          tt.col3,
                tt.col4,
                tt.col5,
                tt.col6,
                tt.col7,
                tt.col8,
                'ar' as tickettype,
                CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
              FROM
                aradmin.ar_troubleticket tt
                LEFT JOIN
                  ARADMIN.TT_CUSTOMERLOGENTRY cl 
                  ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
              WHERE
                ( tt.TSTATUS < 10 )
                {$customer_list} 
          )) dummy ORDER BY dummy.tstatus 

答案 2 :(得分:0)

您只需要在查询的最后添加ORDER BY子句。在标准SQL中,ORDER BY应用于整个结果集。

我还认为您不需要UNION ed子查询周围的括号,因此我将其删除。

查询:

SELECT
    tt.tstatus,
    tt.ticketnbr,
    tt.col1,
    tt.col2,
    NULL as col3,
    tt.col4,
    tt.col5,
    tt.col6,
    tt.col7,
    tt.col8,
    'cmg' as tickettype,
    CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
FROM
    aradmin.cmg_troubleticket tt
    LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
        ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
WHERE
  ( tt.TSTATUS < 9 )
  {$customer_list}
UNION
SELECT
    tt.tstatus,
    tt.ticketnbr,
    tt.col1,
    tt.col2,
    tt.col3,
    tt.col4,
    tt.col5,
    tt.col6,
    tt.col7,
    tt.col8,
    'ar' as tickettype,
    CASE WHEN cl.parentid IS NOT NULL THEN 1 ELSE 0 END as has_log
FROM
    aradmin.ar_troubleticket tt
    LEFT JOIN ARADMIN.TT_CUSTOMERLOGENTRY cl 
        ON ( tt.ticketnbr=cl.parentid AND cl.schema='AR:TroubleTicket' AND cl.status=0 )
WHERE
   ( tt.TSTATUS < 10 )
   {$customer_list}           
ORDER BY tstatus