sql >> Database teknologi >  >> RDS >> Sqlserver

Row_Number() med unionsforespørgsel

Prøv dette:

SELECT *, ROW_NUMBER() OVER(ORDER BY Id) ROW_NUM
  FROM (
        select Id, VersionNumber from documents where id=5 
        Union all  
        select Id, VersionNumber from versions where id=5
       ) a
order by VersionNumber desc

FOR AT filtrere efter version nummer 5, brug:

SELECT * 
FROM   (SELECT *, 
               Row_number() OVER(ORDER BY versionnumber DESC, id) row_num 
        FROM   (SELECT id, 
                       versionnumber 
                FROM   documents 
                WHERE  id = 5 
                UNION ALL 
                SELECT id, 
                       versionnumber 
                FROM   versions 
                WHERE  id = 5) a) b 
WHERE  version = 5 


  1. Belastningsbalancering med ProxySQL For Percona XtraDB Cluster

  2. 5 MySql nyttige forespørgsler under projektudvikling

  3. Hvordan rettes dårligt formateret JSON i PHP?

  4. Et stort dataproblem:hardware eller software ... apparater ...