2

こんにちは、SQLServer2005のSQLに問題があります。

以下のようなデータを持つmyTableというテーブルがあるとします。

|   NAME | CREDIT | GRADE | YEAR | SEMESTER |
---------------------------------------------
|  Name1 |      1 |     A |    1 |        1 |
|  Name2 |      4 |     B |    1 |        1 |
|  Name3 |      2 |     E |    1 |        1 |
|  Name4 |      7 |     F |    1 |        1 |
|  Name5 |      4 |     A |    1 |        2 |
|  Name6 |      3 |     C |    1 |        2 |
|  Name7 |      6 |     D |    1 |        2 |
|  Name8 |      1 |     A |    1 |        2 |
|  Name9 |      1 |     A |    1 |        2 |
| Name10 |      1 |     A |    1 |        2 |
| Name11 |      3 |     C |    2 |        1 |
| Name12 |      6 |     E |    2 |        1 |
| Name13 |      4 |     C |    2 |        1 |
| Name14 |      2 |     B |    2 |        2 |
| Name15 |      1 |     A |    2 |        2 |
| Name16 |      1 |     A |    2 |        2 |
| Name17 |      1 |     A |    2 |        2 |
| Name18 |      5 |     D |    3 |        1 |
| Name19 |      1 |     A |    3 |        1 |
| Name20 |      1 |     A |    3 |        1 |
| Name18 |      5 |     D |    3 |        2 |
| Name19 |      1 |     A |    3 |        2 |
| Name20 |      1 |     A |    3 |        2 |

結果を以下のように出力したい:

|  NAM1 | CRDT1 | GRD1 | YEAR1 | SEMER1 | NAM2 | CRDT2 | GRD2 | YEAR2 | SEMES2 |
   -----------------------------------------------------------------------------
|  Name1|     1 |    A |     1 |      1 |Name5 |     4 |    A |     1 |      2 |
|  Name2|     4 |    B |     1 |      1 |Name6 |     3 |    C |     1 |      2 |
|  Name3|     2 |    E |     1 |      1 |Name7 |     6 |    D |     1 |      2 |
|  Name4|     7 |    F |     1 |      1 |Name8 |     1 |    A |     1 |      2 |
                                        |Name9 |     1 |    A |     1 |      2 |
                                        |Name10|     1 |    A |     1 |      2 |
| Name11|    3 |     C |     2 |      1 |Name14|     2 |    B |     2 |      2 |
| Name12|    6 |     E |     2 |      1 |Name15|     1 |    A |     2 |      2 |
| Name13|    4 |     C |     2 |      1 |Name16|     1 |    A |     2 |      2 |
                                        |Name17|     1 |    A |     2 |      2 | 
| Name18|    5 |     D |     3 |      1 |Name18|     5 |    D |     3 |      2 |
| Name19|    1 |     A |     3 |      1 |Name19|     1 |    A |     3 |      2 |
| Name20|    1 |     A |     3 |      1 |Name20|     1 |    A |     3 |      2 |   

どこ

- Nam1= Name in Semester 1
- CRDT1= Credit in Semester 1
- GRD1= Grade in Semester 1
- Year1= Year in Semester 1 
- Semer1 = Semester in Semester 1

- Nam2= Name in Semester 2
- CRDT2= Credit in Semester 2
- GRD2= Grade in Semester 2
- Year2= Year in Semester 2 
- Semer2 = Semester in Semester 2

このSQLをテストするには、次のURLにアクセスしてください。http://sqlfiddle.com/#

!3/196c6/ 1このような出力を作成するSQLを作成するにはどうすればよいですか。

4

3 に答える 3

3
select
    s1.Name as nam1, s1.credit as crdt1, s1.Year as year1, s1.semester as semer1,
    s2.Name as nam2, s2.credit as crdt2, s2.Year as year2, s2.semester as semer2
from 
(select *, ROW_NUMBER() over (partition by year order by name) rn from myTable where semester=1 ) s1
    full outer join
(select *, ROW_NUMBER() over (partition by year order by name) rn from myTable where semester=2 ) s2
    on s1.year = s2.year
    and s1.rn = s2.rn
于 2012-08-15T15:48:03.303 に答える
1

単純なグループ化で十分な場合、外部結合を行うのは好きではありません。

select max(case when semester = 1 then Name end) as name1,
       max(case when semester = 1 then credit end) as credit1,
       max(case when semester = 1 then year end) as year1,
       max(case when semester = 1 then semester end) as semester1,
       max(case when semester = 2 then Name end) as name2,
       max(case when semester = 2 then credit end) as credit2,
       max(case when semester = 2 then year end) as year2,
       max(case when semester = 2 then semester end) as semester2
from (select t.*,
             row_number() over (partition by semester order by name) as rownum
      from t
     ) t2
group by rownum
order by rownum
于 2012-08-15T17:28:31.037 に答える
0
select Name,credit, grade, year,semester from myTable
group by semester,year, Name,credit, grade;

ここで、この前のクエリを使用して動的クエリを作成する必要があります。semsterがあるので、一時テーブルとして作成します。最初に、ループ内のすべての学期テーブルのすべてのフィールドを使用して動的にselectクエリを作成します。foreach一時テーブルは、selectクエリでこのテーブルのすべてのフィールドを連結します。このテーブルの学期値を使用して構成ラベルフィールドを追加し、ユニオンを使用して一時テーブルを追加します

'select' +#tbls1.fieldName +'、' + ... + +#tbls2.fieldName +

于 2012-08-15T16:21:11.030 に答える