SQL Server row and column conversions

Source: Internet
Author: User

    1. Row and column conversions:
  1. Name Course Score
  2. Zhang San language 74
  3. Zhang San Mathematics 83
  4. Zhang San Physics 93
  5. John Doe Language 74
  6. John Doe Mathematics 84
  7. John Doe Physics 94
  8. Want to become (get the following result):
  9. Name Chinese mathematics Physics
  10. ---- ---- ---- ----
  11. Lee 474 84 94
  12. Sheet 374 83 93
  13. Create Table SC (name varchar (10), Course varchar (10), fractional float)
  14. INSERT INTO SC
  15. Select ' Zhang San ',' language ',
  16. Union
  17. Select ' Zhang San ',' math ',
  18. Union
  19. Select ' Zhang San ',' physics ',
  20. Union
  21. Select ' John Doe ',' language ',
  22. Union
  23. Select ' John Doe ',' math ',
  24. Union
  25. Select ' John Doe ',' physical ', 94
  26. Method 1:
  27. DECLARE @sql varchar (max)
  28. Set @sql =' SELECT '
  29. Select @[email protected]+', max (case when course = ' +course + ' thenfraction Else ' "end) [' + Course +'] ' From (Select Distinct course from SC) T
  30. Set @sql = STUFF (@sql, 8,1,")
  31. Print @sql
  32. Set @[email protected]+', name from SC Group by name '
  33. EXEC (@sql)
  34. Method 2:
  35. Select name, Math, physics, language from SC pivot ( max (score) for course in (Math, Physics, language)) T
  36. Method 3:
  37. DECLARE @sql varchar (8000)
  38. Select @sql = isnull (@sql + '],[', ') + courses from the SC GROUP by course
  39. Print @sql
  40. Set @sql = ' [' + @sql + '] '
  41. EXEC (' select * from "(SELECT * from TB) a pivot (max (score) for course in (' + @sql + ')) b ')


Row and column substitution:

  1. Name Chinese mathematics Physics
  2. ----------------------------------
  3. Sheet 380 90 85
  4. Lee 485 92 82
  5. Requires the use of the T-SQL language to achieve the following results:
  6. Course Dick and Harry
  7. ----------------------
  8. Language 80 85
  9. Mathematics 90 92
  10. Physics 85 82
  11. Drop Table SC
  12. Create Table SC (name varchar (10), language int, math int, physical int)
  13. INSERT INTO SC
  14. Select ' Zhang San ', 80,90,85
  15. Union All
  16. Select ' John Doe ', 85,92,82
  17. SELECT * FROM SC
  18. -------------This process is not unpivot, there is time to add?
  19. SELECT * into SC1 from (
  20. Select name,' language ' course, language score from SC
  21. Union
  22. Select name,' math ' course, math from SC
  23. Union
  24. Select name,' Physics ' course, physical from SC
  25. ) T
    1. Complementary unpivot, same effect as above
    1. Select Name, course, score into #sc1 from SC Unpivot (score for course in ([Language],[Mathematics],[Physics]) a
  1. DECLARE @sql varchar (8000)
  2. Set @sql =' SELECT '
  3. Select @[email protected]+', max (case when name = "+ name +" thenfraction Else ""End) [' + name +'] ' From (select distinct name from
  4. SC1) T
  5. Set @sql =stuff (@sql, 8,1,")
  6. Set @[email protected]+', course from the SC1 Group by course '
  7. Print @sql
  8. EXEC (@sql)
    1. Supplemental Dynamic pivot and Unpiot
      1. --------------Pivot
      2. DECLARE @sql varchar (8000)
      3. Select @sql = isnull (@sql + '],[', ') + courses from the TB GROUP by course
      4. Print @sql
      5. Set @sql = ' [' + @sql + '] '
      6. EXEC (' select * from "(SELECT * from TB) a pivot (max (score) for course in (' + @sql + ')) b ')
      7. --------------Unpivot
      8. DECLARE @sql varchar (8000)
      9. Select @sql = isnull (@sql + '],[', ") + name from syscolumns where id=object_id (' TB ') and colorder>1
      10. Set @sql = ' [' + @sql + '] '
      11. EXEC (' SELECT ' Name, course, score from (SELECT * from TBTB) a Unpivot (score for Course in (' + @sql + ')) b ')

SQL Server row and column conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.