How to get next/previous record number?

别来无恙 提交于 2020-01-15 04:39:13

问题


How to get previous record number in SQL Server?

Data Table:

Name   | SO No.
-----------------
Adrian | SO-00001
Adrian | SO-00002
Bianca | SO-00003
Carrie | SO-00004
Adrian | SO-00005
Bianca | SO-00006
Adrian | SO-00007

How can I get the result as below:

Name   | SO No.   | Previous SO
-------------------------------
Adrian | SO-00005 | SO-00002

回答1:


This one will work the same in all major RDBMS (including SQL Server and MySql)

UPDATED based on your comments

SELECT name, 
       so_no,
       (
         SELECT MAX(so_no)
           FROM table1
          WHERE so_no < t.so_no
            AND name = t.name
       ) prev_so_no
  FROM table1 t
 WHERE so_no = 'SO-00005'

SQL Server:

UPDATED based on your comments

SELECT name, 
       MAX(so_no) so_no, 
       CASE WHEN MAX(so_no) = MIN(so_no) 
            THEN NULL
            ELSE MIN(so_no)
       END prev_so_no
  FROM
(
  SELECT TOP 2 t1.name, t1.so_no
    FROM table1 t1 JOIN table1 t2
      ON t1.name = t2.name
   WHERE t2.so_no = 'SO-00005'
     AND t1.so_no <= t2.so_no
   ORDER BY so_no DESC
) q
   GROUP BY name

if you're using SQL Server 2012 then you can also utilize analytic function LAG

SELECT name, so_no, prev_so_no
  FROM
(
  SELECT name, so_no,
         LAG(so_no, 1, NULL) OVER (ORDER BY so_no) prev_so_no,
         ROW_NUMBER() OVER (ORDER BY so_no DESC) rnum
    FROM table1
   WHERE name = 'Adrian'
     AND so_no <= 'SO-00005'
) q
WHERE rnum = 1

or

SELECT TOP 1 name, so_no, prev_so_no
  FROM
(
  SELECT name, so_no,
         LAG(so_no, 1, NULL) OVER (ORDER BY so_no) prev_so_no
    FROM table1
   WHERE name = 'Adrian'
     AND so_no <= 'SO-00005'
) q
 ORDER BY so_no DESC

MySQL:

SELECT name, 
       MAX(so_no) so_no, 
       CASE WHEN MAX(so_no) = MIN(so_no) 
            THEN NULL
            ELSE MIN(so_no)
       END prev_so_no
  FROM
(
  SELECT name, so_no
    FROM table1
   WHERE name = 'Adrian'
     AND so_no <= 'SO-00005'
   ORDER BY so_no DESC
   LIMIT 2
) q
   GROUP BY name

or

SELECT name, 
       SUBSTRING_INDEX(so_no, ',', 1) so_no,
       CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(so_no, ',', 2), ',', -1) = SUBSTRING_INDEX(so_no, ',', 1)
            THEN NULL
            ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(so_no, ',', 2), ',', -1)
       END prev_so_no
  FROM
(
  SELECT name, GROUP_CONCAT(so_no ORDER BY so_no DESC) so_no
    FROM table1
   WHERE name = 'Adrian'
     AND so_no <= 'SO-00005'
   GROUP BY name
) q

Output for all queries:

|   NAME |    SO_NO | PREV_SO_NO |
|--------|----------|------------|
| Adrian | SO-00005 |   SO-00002 |

Here is SQLFiddle demo (SQL Server 2008) UPDATED
Here is SQLFiddle demo (SQL Server 2012)
Here is SQLFiddle demo (MySQL)




回答2:


Try with offset, this way was not so clean but perhaps can help you with

SELECT name as "Name",
(SELECT so_no FROM table WHERE name LIKE '%Adrian%' ORDER BY so_no DESC LIMIT 1) as "SO No.", 
(SELECT so_no FROM table WHERE name LIKE '%Adrian%' ORDER BY so_no DESC OFFSET 1 LIMIT 1) as "Previous SO" 
FROM table
WHERE name LIKE '%Adrian%'
LIMIT 1;

Sorry for bad query, kind of newbie also :(



来源:https://stackoverflow.com/questions/18691444/how-to-get-next-previous-record-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!