Summary of mysql row and column conversion methods

Source: Internet
Author: User
To switch to bbs. csdn. nettopics310045927, cross tabulation exists in some databases, but this feature is not available in MySQL. However, many friends on the Internet want to find a solution, in a broad sense. Yes or no. Data Sample: createtabletx (idintprimarykey, c1char (2), c2char (2), c3int); inser

Turn to http://bbs.csdn.net/topics/310045927 in some databases have a cross table, but in MySQL does not have this function, but the Internet saw a lot of friends want to find a solution, thji Si broad sense. Yes or no. Data Sample: createtabletx (idintprimarykey, c1char (2), c2char (2), c3int); inser

Zookeeper

Transfer http://bbs.csdn.net/topics/310045927

In some databases, there are cross tables, but this function is not available in MySQL. However, many friends on the Internet want to find a solution in a broad sense. Yes or no.

Data sample:
Create table tx (
Id int primary key,
C1 char (2 ),
C2 char (2 ),
C3 int
);

Insert into tx values
(1, 'a1', 'b1 ', 9 ),
(2, 'a2 ', 'b1', 7 ),
(3, 'a3 ', 'b1', 4 ),
(4, 'a4 ', 'b1', 2 ),
(5, 'a1', 'b2', 2 ),
(6, 'a2 ', 'b2', 9 ),
(7, 'a3 ', 'b2', 8 ),
(8, 'a4 ', 'b2', 5 ),
(9, 'a1', 'b3', 1 ),
(10, 'a2 ', 'b3', 8 ),
(11, 'a3 ', 'b3', 8 ),
(12, 'a4 ', 'b3', 6 ),
(13, 'a1', 'b4 ', 8 ),
(14, 'a2 ', 'b4', 2 ),
(15, 'a3 ', 'b4', 6 ),
(16, 'a4 ', 'b4', 9 ),
(17, 'a1', 'b4 ', 3 ),
(18, 'a2 ', 'b4', 5 ),
(19, 'a3 ', 'b4', 2 ),
(20, 'a4 ', 'b4', 5 );

Result (excluding rows/columns)
[Code = BatchFile] + ------ + ----- + ------ +
| C1 | B1 | B2 | B3 | B4 | Total |
+ ------ + ----- + ------ +
| A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6 | 14 | 27 |
| Total | 22 | 24 | 23 | 40 | 109 |
+ ------ + ----- + ------ + [/Code]


Method 1:

Mysql> SELECT
-> IFNULL (c1, 'Total') AS total,
-> SUM (IF (c2 = 'b1 ', c3, 0) AS B1,
-> SUM (IF (c2 = 'b2', c3, 0) AS B2,
-> SUM (IF (c2 = 'b3', c3, 0) AS B3,
-> SUM (IF (c2 = 'b4 ', c3, 0) AS B4,
-> SUM (IF (c2 = 'Total', c3, 0) AS total
-> FROM (
-> SELECT c1, IFNULL (c2, 'Total') AS c2, SUM (c3) AS c3
-> FROM tx
-> Group by c1, c2
-> WITH ROLLUP
-> HAVING c1 IS NOT NULL
->) AS
-> Group by c1
-> With rollup;

"Total", "B1", "B2", "B3", "B4", "total"
"A1", 9, 2, 1, 11, 23
"A2", 7, 9, 8, 7, 31
"A3", 4, 8, 8, 28
"A4", 2,5, 6,14, 27
"Total", 109

5 rows in set, 1 warning (0.00 sec)


Method 2:

Static:
Select c1,
Sum (if (c2 = 'b1 ', C3, 0) AS B1,
Sum (if (c2 = 'b2', C3, 0) AS B2,
Sum (if (c2 = 'b3', C3, 0) AS B3,
Sum (if (c2 = 'b4 ', C3, 0) AS B4, SUM (C3) AS TOTAL
From tx
Group by C1
UNION
SELECT 'Total', sum (if (c2 = 'b1 ', C3, 0) AS B1,
Sum (if (c2 = 'b2', C3, 0) AS B2,
Sum (if (c2 = 'b3', C3, 0) AS B3,
Sum (if (c2 = 'b4 ', C3, 0) AS B4, SUM (C3) FROM TX



Method 3:

Dynamic:
SET @ EE = '';
SELECT @ EE: = CONCAT (@ EE, 'sum (IF (C2 = \ '', C2, '\'',', C3, 0) AS ', C2, ',') FROM
(Select distinct C2 from tx);
SET @ QQ = CONCAT ('select ifnull (c1, \ 'total \ '),', LEFT (@ EE, LENGTH (@ EE)-1 ),', SUM (C3) as total from tx group by C1 with rollup ');
PREPARE stmt2 FROM @ QQ;
EXECUTE stmt2;


Method 4:


-- Wood has a total, passed the test under SQL Server

If object_id ('tempdb .. # tx ') is not null drop table # tx
Go
Create table # tx (
Id int primary key,
C1 char (2 ),
C2 char (2 ),
C3 int
);

Insert into # tx (id, c1, c2, c3) values (1, 'a1', 'b1 ', 9)
Insert into # tx (id, c1, c2, c3) values (2, 'a2 ', 'b1', 7)
Insert into # tx (id, c1, c2, c3) values (3, 'a3 ', 'b1', 4)
Insert into # tx (id, c1, c2, c3) values (4, 'a4 ', 'b1', 2)
Insert into # tx (id, c1, c2, c3) values (5, 'a1', 'b2', 2)
Insert into # tx (id, c1, c2, c3) values (6, 'a2 ', 'b2', 9)
Insert into # tx (id, c1, c2, c3) values (7, 'a3 ', 'b2', 8)
Insert into # tx (id, c1, c2, c3) values (8, 'a4 ', 'b2', 5)
Insert into # tx (id, c1, c2, c3) values (9, 'a1', 'b3', 1)
Insert into # tx (id, c1, c2, c3) values (10, 'a2 ', 'b3', 8)
Insert into # tx (id, c1, c2, c3) values (11, 'a3 ', 'b3', 8)
Insert into # tx (id, c1, c2, c3) values (12, 'a4 ', 'b3', 6)
Insert into # tx (id, c1, c2, c3) values (13, 'a1', 'b4 ', 8)
Insert into # tx (id, c1, c2, c3) values (14, 'a2 ', 'b4', 2)
Insert into # tx (id, c1, c2, c3) values (15, 'a3 ', 'b4', 6)
Insert into # tx (id, c1, c2, c3) values (16, 'a4 ', 'b4', 9)
Insert into # tx (id, c1, c2, c3) values (17, 'a1', 'b4 ', 3)
Insert into # tx (id, c1, c2, c3) values (18, 'a2 ', 'b4', 5)
Insert into # tx (id, c1, c2, c3) values (19, 'a3 ', 'b4', 2)
Insert into # tx (id, c1, c2, c3) values (20, 'a4 ', 'b4', 5)


SELECT c1
, Sum (CASE
WHEN c2 = 'b1 'THEN c3
ELSE 0
END) AS [b1]
, Sum (CASE
WHEN c2 = 'b2' THEN c3
ELSE 0
END) AS [b2]
, Sum (CASE
WHEN c2 = 'b3' THEN c3
ELSE 0
END) AS [b3]
, Sum (CASE
WHEN c2 = 'b4 'THEN c3
ELSE 0
END) AS [b4]
FROM # tx
Group by c1



Method 5:


-- Dynamic words

If object_id ('tempdb .. # tx ') is not null drop table # tx
Go
Create table # tx (
Id int primary key,
C1 char (2 ),
C2 char (2 ),
C3 int
);

Insert into # tx (id, c1, c2, c3) values (1, 'a1', 'b1 ', 9)
Insert into # tx (id, c1, c2, c3) values (2, 'a2 ', 'b1', 7)
Insert into # tx (id, c1, c2, c3) values (3, 'a3 ', 'b1', 4)
Insert into # tx (id, c1, c2, c3) values (4, 'a4 ', 'b1', 2)
Insert into # tx (id, c1, c2, c3) values (5, 'a1', 'b2', 2)
Insert into # tx (id, c1, c2, c3) values (6, 'a2 ', 'b2', 9)
Insert into # tx (id, c1, c2, c3) values (7, 'a3 ', 'b2', 8)
Insert into # tx (id, c1, c2, c3) values (8, 'a4 ', 'b2', 5)
Insert into # tx (id, c1, c2, c3) values (9, 'a1', 'b3', 1)
Insert into # tx (id, c1, c2, c3) values (10, 'a2 ', 'b3', 8)
Insert into # tx (id, c1, c2, c3) values (11, 'a3 ', 'b3', 8)
Insert into # tx (id, c1, c2, c3) values (12, 'a4 ', 'b3', 6)
Insert into # tx (id, c1, c2, c3) values (13, 'a1', 'b4 ', 8)
Insert into # tx (id, c1, c2, c3) values (14, 'a2 ', 'b4', 2)
Insert into # tx (id, c1, c2, c3) values (15, 'a3 ', 'b4', 6)
Insert into # tx (id, c1, c2, c3) values (16, 'a4 ', 'b4', 9)
Insert into # tx (id, c1, c2, c3) values (17, 'a1', 'b4 ', 3)
Insert into # tx (id, c1, c2, c3) values (18, 'a2 ', 'b4', 5)
Insert into # tx (id, c1, c2, c3) values (19, 'a3 ', 'b4', 2)
Insert into # tx (id, c1, c2, c3) values (20, 'a4 ', 'b4', 5)

Select * from # tx

Declare @ SQL varchar (8000)
Set @ SQL = 'select c1 ,'
Select @ SQL = @ SQL + 'sum (case when c2 = ''' + cast (c2 as varchar (10) + ''' then c3 else 0 end) ['+ cast (c2 as varchar (10) +'],'
From (select distinct c2 from # tx)
Print (@ SQL)

Set @ SQL = left (@ SQL, len (@ SQL)-1) + 'from # tx group by c1'
Exec (@ SQL)
If object_id ('tempdb .. # tx ') is not null drop table # tx

/*
A1 9 2 1 11
A2 7 9 8 7
A3 4 8 8 8
A4 2 5 6 14
*/



Test

1,

Mysql> SELECT
-> IFNULL (c1, 'Total') AS total,
-> SUM (IF (c2 = 'b1 ', c3, 0) AS B1,
-> SUM (IF (c2 = 'b2', c3, 0) AS B2,
-> SUM (IF (c2 = 'b3', c3, 0) AS B3,
-> SUM (IF (c2 = 'b4 ', c3, 0) AS B4,
-> SUM (IF (c2 = 'Total', c3, 0) AS total
-> FROM (
-> SELECT c1, IFNULL (c2, 'Total') AS c2, SUM (c3) AS c3
-> FROM tx
-> Group by c1, c2
-> WITH ROLLUP
-> HAVING c1 IS NOT NULL
->) AS
-> Group by c1
-> With rollup;
+ ------- + ------ + ------- +
| Total | B1 | B2 | B3 | B4 | total |
+ ------- + ------ + ------- +
| A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6 | 14 | 27 |
| Total | 22 | 24 | 23 | 40 | 109 |
+ ------- + ------ + ------- +
5 rows in set, 1 warning (0.00 sec)


2,

Mysql> select c1,
-> Sum (if (c2 = 'b1 ', C3, 0) AS B1,
-> Sum (if (c2 = 'b2', C3, 0) AS B2,
-> Sum (if (c2 = 'b3', C3, 0) AS B3,
-> Sum (if (c2 = 'b4 ', C3, 0) AS B4, SUM (C3) AS TOTAL
-> From tx
-> Group by C1
-> UNION
-> SELECT 'Total', sum (if (c2 = 'b1 ', C3, 0) AS B1,
-> Sum (if (c2 = 'b2', C3, 0) AS B2,
-> Sum (if (c2 = 'b3', C3, 0) AS B3,
-> Sum (if (c2 = 'b4 ', C3, 0) AS B4, SUM (C3) FROM TX
->;
+ ------- + ------ + ------- +
| C1 | B1 | B2 | B3 | B4 | TOTAL |
+ ------- + ------ + ------- +
| A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6 | 14 | 27 |
| TOTAL | 22 | 24 | 23 | 40 | 109 |
+ ------- + ------ + ------- +
5 rows in set (0.00 sec)

Mysql>


3,

Mysql> select ifnull (c1, 'Total '),
-> Sum (if (c2 = 'b1 ', C3, 0) AS B1,
-> Sum (if (c2 = 'b2', C3, 0) AS B2,
-> Sum (if (c2 = 'b3', C3, 0) AS B3,
-> Sum (if (c2 = 'b4 ', C3, 0) AS B4, SUM (C3) AS TOTAL
-> From tx
-> Group by C1 with rollup;
+ -------------------- + ------ + ------- +
| Ifnull (c1, 'Total') | B1 | B2 | B3 | B4 | total |
+ -------------------- + ------ + ------- +
| A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6 | 14 | 27 |
| Total | 22 | 24 | 23 | 40 | 109 |
+ -------------------- + ------ + ------- +
5 rows in set (0.00 sec)

Mysql>



4,
Mysql> SET @ EE = '';
Mysql> SELECT @ EE: = CONCAT (@ EE, 'sum (IF (C2 = \ '', C2, '\'',', C3, 0) ', c2, ',') FROM (select distinct C2 from tx);


Mysql> SET @ QQ = CONCAT ('select ifnull (c1, \ 'total \ '),', LEFT (@ EE, LENGTH (@ EE)-1 ),', SUM (C3) as total from tx group by C1 with rollup ');
Query OK, 0 rows affected (0.00 sec)

Mysql> PREPARE stmt2 FROM @ QQ;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

Mysql> EXECUTE stmt2;
+ -------------------- + ------ + ------- +
| Ifnull (c1, 'Total') | B1 | B2 | B3 | B4 | total |
+ -------------------- + ------ + ------- +
| A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6 | 14 | 27 |
| Total | 22 | 24 | 23 | 40 | 109 |
+ -------------------- + ------ + ------- +
5 rows in set (0.00 sec)

Mysql>




5,


Mysql> SET @ EE = '';
Mysql> SELECT @ EE: = CONCAT (@ EE, 'sum (IF (C2 = \ '', C2, '\'',', C3, 0) ', c2, ',') FROM (select distinct C2 from tx);


Mysql> SET @ QQ = CONCAT ('select ifnull (c1, \ 'total \ '),', LEFT (@ EE, LENGTH (@ EE)-1 ),', SUM (C3) as total from tx group by C1 with rollup ');
Query OK, 0 rows affected (0.00 sec)

Mysql> PREPARE stmt2 FROM @ QQ;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

Mysql> EXECUTE stmt2;
+ -------------------- + ------ + ------- +
| Ifnull (c1, 'Total') | B1 | B2 | B3 | B4 | total |
+ -------------------- + ------ + ------- +
| A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6 | 14 | 27 |
| Total | 22 | 24 | 23 | 40 | 109 |
+ -------------------- + ------ + ------- +
5 rows in set (0.00 sec)

Mysql>

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.