-4

これは私の「連絡先」テーブルです。

| Names      |   Gender   |

| Mike       | Male       |
| Steve      | Male       |
| Kelly      | Female     |
| Jane       | Female     |
| Natasha    | Female     |
| Mathew     | Male       |
| Jack       | Male       |

私はそのような結果が欲しいです:

|  Male      |   Female   |   

| Mike       | Kelly      |
| Steve      | Jane       |
| Mathew     | Natasha    |
| Jack       |            |

SQLサーバーでこの結果を達成するための解決策を教えてください...どんな提案でも大歓迎です!

4

1 に答える 1

5
with Male as
(
  select Names,
         row_number() over(order by Names) as rn
  from YourTable
  where Gender = 'Male'
),
Female as
(
  select Names,
         row_number() over(order by Names) as rn
  from YourTable
  where Gender = 'Female'
)
select Male.Names as Male,
       Female.Names as Female
from Male
  full outer join Female
    on Male.rn = Female.rn

SQLフィドル

希望する正確な結果ではありませんが、別の列を並べ替えなくても十分です。

Male    Female
------- -------
Jack    Jane
Mathew  Kelly
Mike    Natasha
Steve   NULL
于 2012-10-23T06:03:11.163 に答える