简体   繁体   中英

Get the first rownumber by variable

I have a code as below, I want to pull the first rownu = 1 in the table, but when I write it in the where statement I get an error:

SELECT 
    * 
FROM 
    (SELECT  
         VID,
         SPRTELE_PHONE_NUMBER AS Phone,
         ROW_NUMBER() OVER (PARTITION BY VID ORDER BY SPRTELE_TELE_cODE desc) AS ROWNU         
     FROM 
         PERSONNEL_VIEW, SPRTELE 
     WHERE     
         SPRTELE_PIDM = PIDM   
         AND (SPRTELE_SEQNO = (SELECT MAX (SPRTELE_SEQNO)
                               FROM SPRTELE
                               WHERE SPRTELE_PIDM = PIDM)))
    ) DATA
WHERE 
    DATA.ROWNU = 1

UNION

SELECT
    *  
FROM
    (SELECT 
         VID,
         SPRTELE_PHONE_NUMBER AS Phone,
         '1' AS ROWNU
     FROM 
         STUDENT_VIEW, SPRTELE
     WHERE     
         SPRTELE_PIDM = PIDM  
         AND (SPRTELE_SEQNO = (SELECT MAX (SPRTELE_SEQNO)
                               FROM SPRTELE
                               WHERE SPRTELE_PIDM = PIDM))
         AND TERM_CODE = '201920'
    ) DATA
WHERE
    data.rownu = 1;

The output is something like

VID           PHONE    ROWNU
-----------------------------
VI1003365     5891449     1
VI2380659     4932389     1
VI2997998     6371006     1 
VI2997998     5821347     2

I need to pull only rownu=1 , but I get an error when I put "rownu=1" in the code.

You will need to create a sub query for that :

SELECT * 
FROM (
  SELECT vid, 
   sprtele_phone_number                 AS Phone, 
   Row_number() 
     OVER ( 
       partition BY vid 
       ORDER BY sprtele_tele_code DESC) AS ROWNU 
   FROM   personnel_view, 
       sprtele 
   WHERE  sprtele_pidm = pidm 
       AND ( sprtele_seqno = (SELECT Max (sprtele_seqno) 
                              FROM   sprtele 
                              WHERE  sprtele_pidm = pidm) )
) DATA
WHERE DATA.ROWNU =1; 

You can read why here : https://www.sqltheater.com/blog/cant-use-row-number-where/

--EDIT--

If your query gets more complicated, you could use a WITH clause like so :

WITH UNION_DATA AS ( 
  SELECT  VID,
          SPRTELE_PHONE_NUMBER    AS Phone,
          ROW_NUMBER()
          OVER (
             PARTITION BY VID 
             ORDER BY SPRTELE_TELE_cODE desc)  AS ROWNU         
  FROM PERSONNEL_VIEW ,  SPRTELE 
  WHERE     SPRTELE_PIDM = PIDM   
        AND (SPRTELE_SEQNO =
            (SELECT MAX (SPRTELE_SEQNO)
               FROM SPRTELE
                  WHERE SPRTELE_PIDM = PIDM ))
  UNION
  SELECT VID,
      SPRTELE_PHONE_NUMBER    AS Phone,
      '1'  AS ROWNU
  FROM STUDENT_VIEW, SPRTELE
  WHERE     SPRTELE_PIDM = PIDM  
       AND (SPRTELE_SEQNO =
            (SELECT MAX (SPRTELE_SEQNO)
               FROM SPRTELE
              WHERE SPRTELE_PIDM = PIDM ))
       AND TERM_CODE = '201920')
SELECT *
FROM UNION_DATA UD 
WHERE UD.ROWNU = 1

Read more on WITH clause : https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html

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