Resource 1: Source Jason_zhu blog jason-zhu.iteye.comblog430647 to a salary table as an example, the table structure is as follows: table structure, each employee id pair should have multiple salary items and amount, need to query turn, change the salary item of each employee to a horizontal display. In direct query (add some restrictions to narrow down
Resource 1: Source Jason_zhu blog http://jason-zhu.iteye.com/blog/430647 to a salary table as an example, the table structure is as follows: table structure, each employee id pair should have multiple salary items and amount, need to query turn, change the salary item of each employee to a horizontal display. In direct query (add some restrictions to narrow down
Source 1: Jason_zhu blog http://jason-zhu.iteye.com/blog/430647
Take a salary table as an example. The table structure is as follows:
In the table structure, each employee id pair should have multiple salary items and amounts. If you need to query the items, convert the salary items of each employee into a horizontal row.
In direct query (add some restrictions to narrow the result set ),
SQL code
- SELECT
- Ssp_pay_empid,
- Ssp_pay_sdate,
- Ssp_pay_edate,
- Ssp_pay_type,
- Ssp_pay_amount,
- Ssp_pay_item
- FROM ssp_pay
- Where ssp_pay_empid = 00000073 and ssp_pay_type = 'rt 'and ssp_pay_item
- IN ('/000000','/000000', '/000000','/000000') and ssp_pay_sdate = '2017-08-01'
SELECT ssp_pay_empid,ssp_pay_sdate,ssp_pay_edate,ssp_pay_type,ssp_pay_amount,ssp_pay_itemFROM ssp_paywhere ssp_pay_empid = 00000073 and ssp_pay_type = 'RT' and ssp_pay_item IN ('/101','/103','/401','/402') and ssp_pay_sdate = '2007-08-01'
The following result is displayed:
To display the preceding query results as one row, convert the unique value in a column of the expression to multiple columns in the output to rotate the table value expression by using the sequence added by SQL Server 2005, aggregate any other column values required in the final output when necessary.
Run the following SQL statement:
SQL code
- Select
- Ssp_pay_empid,
- Ssp_pay_sdate,
- Ssp_pay_edate,
- Ssp_pay_type,
- [/101],
- [/103],
- [/401],
- [/402]
- From (
- SELECT
- Ssp_pay_empid,
- Ssp_pay_sdate,
- Ssp_pay_edate,
- Ssp_pay_type,
- Ssp_pay_amount,
- Ssp_pay_item
- FROM ssp_pay
- Where ssp_pay_empid = 00000073 and ssp_pay_type = 'rt 'and ssp_pay_sdate = '2017-08-01'
- ) AS SourceTable
- Aggregate (SUM (ssp_pay_amount) FOR ssp_pay_item
- IN ([/101], [/103], [/401], [/402]) AS portable tTable
select ssp_pay_empid,ssp_pay_sdate,ssp_pay_edate,ssp_pay_type,[/101],[/103],[/401],[/402]from (SELECT ssp_pay_empid,ssp_pay_sdate,ssp_pay_edate,ssp_pay_type,ssp_pay_amount,ssp_pay_itemFROM ssp_paywhere ssp_pay_empid = 00000073 and ssp_pay_type = 'RT' and ssp_pay_sdate = '2007-08-01') AS SourceTable PIVOT (SUM(ssp_pay_amount) FOR ssp_pay_itemIN ([/101],[/103],[/401],[/402])) AS PivotTable
Result:
Resource 2 source http://www.cnblogs.com/gkl0818/archive/2009/02/25/1398078.html
1. Column and column conversion with fixed columns
For example
Student subject grade
---------------------------
Student1 language 80
Student1 mathematics 70
Student1 English 60
Student2 language 90
Student2 mathematics 80
Student2 100 English
......
Convert
Chinese, mathematics, and English
Student1 80 70 60
Student2 90 80 100
......
Statement: select student,
Sum (decode (subject, 'China', grade, null) "language ",
Sum (decode (subject, 'mat', grade, null) "Mathematics ",
Sum (decode (subject, 'English ', grade, null) "English"
From table
Group by student;
2. Changing columns and columns
For example
C1 c2
--------------
1. Me
1 is
1 WHO
2 Zhi
2 channels
3 No
......
Convert
1. Who am I?
2. Know
3 No
This type of conversion can be completed by PL/SQL. Here is an example.
Create or replace function get_c2 (tmp_c1 NUMBER)
RETURN VARCHAR2
IS
Col_c2 VARCHAR2 (4000 );
BEGIN
FOR cur IN (SELECT c2 FROM t WHERE c1 = tmp_c1) LOOP
Col_c2: = Col_c2 | cur. c2;
End loop;
Col_c2: = rtrim (Col_c2, 1 );
RETURN Col_c2;
END;
Select distinct c1, get_c2 (c1) cc2 from table;
You can also use Analytic Functions and CONNECT_BY without pl/SQL:
SELECT c1, SUBSTR (MAX (SYS_CONNECT_BY_PATH (c2, ';'), 2) NAME
FROM (SELECT c1, c2, rn, LEAD (rn) OVER (partition by c1 order by rn) rn1
FROM (SELECT c1, c2, ROW_NUMBER () OVER (order by c2) rn
FROM t ))
Start with rn1 IS NULL
Connect by rn1 = PRIOR rn
Group by c1;
3. The number of columns is not fixed (columns in the crosstab chart are transposed)
This is a very troublesome one. You need to use pl/SQL:
Raw data:
CLASS1 CALLDATE CALLCOUNT
1 2005-08-08 40
1 2005-08-07 6
2 2005-08-08 77
3 2005-08-09 33
3
3 2005-08-07 21
After transpose:
CALLDATE CallCount1 CallCount2 CallCount3
------------------------------------------
2005-08-09 0 0 33
6 0 21
The test is as follows:
1). Create test tables and data
Create table t (
Class1 VARCHAR2 (2 BYTE ),
Calldate DATE,
Callcount INTEGER
);
Insert into t (class1, calldate, callcount)
VALUES ('1', TO_DATE ('2014/1/123', 'Mm/DD/yyyy'), 40 );
Insert into t (class1, calldate, callcount)
VALUES ('1', TO_DATE ('2014/1/123', 'Mm/DD/yyyy'), 6 );
Insert into t (class1, calldate, callcount)
VALUES ('2', TO_DATE ('2014/1/123', 'Mm/DD/yyyy'), 77 );
Insert into t (class1, calldate, callcount)
VALUES ('3', TO_DATE ('2014/1/123', 'Mm/DD/yyyy'), 33 );
Insert into t (class1, calldate, callcount)
VALUES ('3', TO_DATE ('2014/1/123', 'Mm/DD/yyyy'), 9 );
Insert into t (class1, calldate, callcount)
VALUES ('3', TO_DATE ('2014/1/123', 'Mm/DD/yyyy'), 21 );
COMMIT;
2) create a ref cursor to prepare the output result set.
Create or replace package pkg_getrecord
IS
TYPE myrctype is ref cursor;
END pkg_getrecord;
/
3) create a dynamic SQL cross tabulation function to output the result set.
Create or replace function fn_rs
RETURN pkg_getrecord.myrctype
IS
S VARCHAR2 (4000 );
CURSOR c1 IS
SELECT ', sum (case when Class1 ='
| Class1
| 'Then CallCount else 0 end )'
| '"Callcount'
| Class1
| '"'C2
FROM t
Group by class1;
R1 c1 % ROWTYPE;
List_cursor pkg_getrecord.myrctype;
BEGIN
S: = 'select calldate ';
OPEN c1;
LOOP
FETCH c1 INTO r1;
Exit when c1 % NOTFOUND;
S: = s | r1.c2;
End loop;
CLOSE c1;
S: = s | 'from T group by CallDate order by CallDate desc ';
OPEN list_cursor FOR s;
RETURN list_cursor;
END fn_rs;
/
4). Run the test in SQL plus:
Var results refcursor;
Exec: results: = fn_rs;
Print results;
CALLDATE CallCount1 CallCount2 CallCount3
---------------------------------------------
2005-08-09 0 0 33
6 0 21