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/']
You might try something like this:

```
DECLARE @MyTable TABLE(idLigne VARCHAR(2), idperson INT, statut CHAR(1));

INSERT INTO @MyTable VALUES ('L1',1,'A')
, ('L2',1,'B')
, ('L3',1,'A')
, ('L4',1,'B')
, ('L5',2,'A')
, ('L6',2,'B')
, ('L7',3,'A')
, ('L8',3,'B')

; WITH a AS (
    SELECT idLigne=t2.idLigne+'-'+t1.idLigne
    , idpersonne=t1.idperson
    , firstLigne=t1.idLigne
    , secondLigne=t2.idLigne
    , r1=ROW_NUMBER()OVER(PARTITION BY t1.idLigne ORDER BY t2.idLigne)
    , r2=ROW_NUMBER()OVER(PARTITION BY t2.idLigne ORDER BY t1.idLigne)
    FROM @MyTable t1
    INNER JOIN @MyTable t2 ON t1.idperson=t2.idperson AND t1.statut='A' AND t2.statut='B'
)
SELECT idLigne
    , idpersonne
    , firstLigne
    , secondLigne
FROM a WHERE r1=r2
GO

```

Result:

![enter image description here](https://i.stack.imgur.com/tlDJX.jpg)
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>