DB2 row to column, column change, and other operations

Source: Internet
Author: User
Tags case statement

DB2 row to column

----Start

See this question on the Internet: (Problem address:http://www.mydb2.cn/bbs/read.php?tid=1297&page=e& #a)

[C-sharp]View Plaincopyprint?
  1. Class Subject Score
  2. 1 Languages 8800
  3. 1 Mathematics 8420
  4. 1 English 7812
  5. ......
  6. 2 languages 8715
  7. 2 Mathematics 8511
  8. 2 English 8512
  9. ......
  10. The request is converted to the following result
  11. Class Chinese Math English
  12. 1 8800 8420 7812
  13. 2 8715 8511 8512

This is a very classic 4 attribute of the table design pattern, as the name implies, such a table generally has four columns, namely:entity_id, attribute_name,Attribute_type, Attribute_ Value, such a design makes it very easy to add fields, such as: we want to add a physical result is very simple, we just insert a record into the table. However, this design has a very serious problem, that is: query difficulty increases, query efficiency is very poor.

One principle to implement the above query is to create a virtual field with a case statement, make the result set a two-dimensional array, and then apply the aggregate function to return a single record. What do you think? Do not understand, take a closer look at the following diagram and analyze the following statement you understand.

[C-sharp]View Plaincopyprint?
  1. CREATE TABLE Score
  2. (
  3. Banji Integer,
  4. Kemu varchar (10),
  5. Fengshu integer
  6. )
  7. Go
  8. INSERT INTO score values
  9. (1, ' language ', 8800),
  10. (1, ' math ', 8420),
  11. (1, ' English ', 7812),
  12. (2, ' language ', 8715),
  13. (2, ' math ', 8511),
  14. (2, ' English ', 8512)
  15. Go
  16. Select Banji,
  17. Max (Yuwen) languages,
  18. Max (Shuxue) Mathematics,
  19. Max (Yingyu) English
  20. From
  21. (Select Banji,
  22. Case Kemu
  23. When ' Chinese ' then Fengshu
  24. Else 0
  25. End Yuwen,
  26. Case Kemu
  27. When ' math ' then Fengshu
  28. Else 0
  29. End Shuxue,
  30. Case Kemu
  31. When ' English ' then Fengshu
  32. Else 0
  33. End Yingyu
  34. From score
  35. ) as inner
  36. GROUP BY Inner.banji
  37. ORDER BY 1
  38. Go

You may be sigh, such a solution is how ingenious, unfortunately not I think out of, here, I also dare not to take the master's ideas for appropriation, the above ideas from the <sql language Art > 11th chapter, want to know more comprehensive information, we can refer to.

---See more:DB2 SQL Extract

---- statement: Reprint please indicate the source.

----Last Updated on 2009.12.20

----written by Shangbo on 2009.12.16

----End

DB2 Career

Row to Column

Give the following data:
CREATE TABLE Sales (year int, Quarter int, Results int)

Year QUARTER RESULTS
----------- ----------- -----------
2004 1 20
2004 2 30
2004 3 15
2004 4 10
2005 1 18
2005 2 40
2005 3 12
2005 4 27
Desired to the result:
Year Q1 Q2 Q3 Q4
----------- ----------- ----------- ----------- -----------
2004 20 30) 15 10
2005 18 40) 12 27
This SQL solves this problem:
SELECT year,
MAX (case when Quarter = 1
Then Results END) as Q1,
MAX (case when Quarter = 2
Then Results END) as Q2,
MAX (case when Quarter = 3
Then Results END) as Q3,
MAX (case when Quarter = 4
Then Results END) as Q4
From Sales
GROUP by year
Explain why you want to add Max, because without Max, the result would be this:
Year Q1 Q2 Q3 Q4
----------- ----------- ----------- ----------- -----------
2004 20---
2004-30--
2004--15-
2004---10
2005 18---
2005-40--
2005--12-
2005---27


Row career

Give the following data

CREATE TABLE Salesagg
(Year INTEGER,
Q1 INTEGER,
Q2 INTEGER,
Q3 INTEGER,
Q4 INTEGER);

Year Q1 Q2 Q3 Q4
----------- ----------- ----------- ----------- -----------
2004 20 30) 15 10
2005 18 40) 12 27

The desired result
Year QUARTER RESULTS
----------- ----------- -----------
2004 1 20
2004 2 30
2004 3 15
2004 4 10
2005 1 18
2005 2 40
2005 3 12
2005 4 27

This SQL can be implemented as follows:

SELECT s.year, Q.quarter, Q.results
From Salesagg as S,
TABLE (VALUES (1, s.q1),
(2, S.Q2),
(3, S.Q3),
(4, S.q4))
As Q (Quarter, Results);
 
table function creates a table, which is a multi-line table with value implementation, and the value implements the virtual table example:
DB2 = select * FROM (values, (2,3)) as T1 (col1,col2)

col1 col2
----------------------
1 2
2 3

2 records have been selected.

DB2 = SELECT * FROM (values 1) as a

1
-----------
1

1 records have been selected.

The difference is that it has a relationship with a table in the FROM clause, and a column in the table is taken as data.

DB2 row to column, column change, and other operations

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.