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?

2
  • You specify SQL Server 2005 in the question, but it's also tagged with 2008. Are you looking for a solution which works in both or are you constrained to 2005?
    – Bridge
    Aug 15, 2012 at 15:39
  • Perhaps this answer might help stackoverflow.com/questions/4284943/… Aug 15, 2012 at 20:38

3 Answers 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
3
  • 1
    +1 nice answer, I spent 20 minutes with an awful PIVOT solution that was obviously over-thinking it. Aug 15, 2012 at 16:04
  • @AaronBertrand I was doing the same thing. this is much easier
    – Taryn
    Aug 15, 2012 at 16:04
  • When I saw it, I thought PIVOT too. I gave up on figuring out how it could be pivotted sooner though.
    – podiluska
    Aug 15, 2012 at 17:57
1

I don't like doing an outer join, when a simple group by is sufficient:

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
0
select Name,credit, grade, year,semester from myTable
group by semester,year, Name,credit, grade;

now we have to make a dynamic query with this previous query: create as temporary table as there are semster first create dynamically a select query with all fields of all semester table in a loop: foreach temporary table concat all fields of this table in select query and add construct label field with semester value of this table and add temporary table with union

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.