I'm a beginner in SQL and I have the following problem in SQL.

I need an SQL query that would calculate the difference between two continuous rows having the same value in field [idpersone] and regroupe them into a single row.

For example I need to transform my table to the desired data as shown below:

```
Table data:
idLigne | idperson | statut
--------|----------|-------
   L1         1         A
   L2         1         B
   L3         1         A
   L4         1         B
   L5         2         A
   L6         2         B
   L7         3         A
   L8         3         B

Desired output: 
idLigne | idpersonne | firstLighe | secondLigne
--------|------------|------------|------------
 L2-L1         1           L1           L2
 L4-L3         1           L3           L4
 L6-L5         2           L5           L6
 L8-L7         2           L7           L8

```
['https://Stackoverflow.com/questions/10452173', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1374633/']
The niaive solution is this...

I'm not sure what you want to do if there are three records for the same `idperson`. Or what to do if to sequential records have different idperson.

```
WITH
  sequenced_data
AS
(
  SELECT
    ROW_NUMBER() OVER (PARTITION BY idperson ORDER BY idLigne) AS sequence_id,
    *
  FROM
    myTable
)
SELECT
  *
FROM
  myTable as firstLigne
LEFT JOIN
  myTable as secondLigne
    ON  secondLigne.idperson    = firstLigne.idperson
    AND secondLigne.sequence_id = firstLigne.sequence_id + 1
WHERE
  (firstLigne.sequence_id % 2) = 1

```
I cannot exactly infer the intent of your query. But here it goes:

```
with a as
(
   select *, 
     (row_number() over(order by idLigne, idperson) - 1) / 2 as pair_number 
   from tbl
)
select 
   max(idligne) + '-' + min(idligne) as idLigne, 
   min(idperson) as idpersonne,
   min(idLigne) as firstlighe, max(idLigne) as secondLigne       
from a
group by pair_number

```

Output:

```
IDLIGNE IDPERSONNE  FIRSTLIGHE  SECONDLIGNE
L2-L1   1           L1          L2
L4-L3   1           L3          L4
L6-L5   2           L5          L6
L8-L7   3           L7          L8

```

Live test: <http://www.sqlfiddle.com/#!3/26371/20>