简体   繁体   中英

sql SORT BY on a UNION query

Actually i try to sort my sql but it's not sorting

where i need to add SORT BY TSTATUS

 $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} 
)";

只需在查询末尾附加ORDER BY tt.tstatus即可获得排序结果。

Create a dummy table of the UNION results and sort by the attribute you need. Try this :

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 

You just need to add the ORDER BY clause at the very end of the query. In standard SQL the ORDER BY is applied to the whole resultset.

Also I believe that you do not need the surrounding parenthesis around the UNION ed subqueries, I removed them.

Query :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM