2

Hello I have a problem with SQL in SQL Server 2005.

Suppose that I have a table called myTable with data as below:

|   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 |

I want to output the result as below:

|  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 |   

Where

- 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

Please go to this URL to test this SQL: http://sqlfiddle.com/#!3/196c6/1

How Can I create SQL to make output like this?

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

当一个简单的 group by 就足够时,我不喜欢进行外部联接:

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;

现在我们必须使用前面的查询进行动态查询:创建临时表,因为有学期 首先动态创建一个选择查询,循环中所有学期表的所有字段:foreach 临时表在选择查询中连接该表的所有字段并使用该表的学期值添加构造标签字段,并使用联合添加临时表

'选择' + #tbls1.fieldName + ',' + ... + + #tbls2.fieldName +

于 2012-08-15T16:21:11.030 回答