Occasionally need to understand, study this article, reprint record
From: http://blog.csdn.net/jxzkin/article/details/7949629
1. Create test data
[HTML]? View Plain Copy
- CREATE? TABLE? Cc??
- ?? (Student?) NVARCHAR2 (2), Course? NVARCHAR2 (2), Score?int??
- ??);??
[HTML]? View Plain Copy
- Insert?into? Cc???
- select? N ' Zhang San ', n ' language ', 78?from?dual?union?all??
- select? N ' Zhang San ', n ' math ', 87?from?dual?union?all??
- select? N ' Zhang San ', n ' English ', 82?from?dual?union?all??
- select? N ' Zhang San ', n ' physics ', 90?from?dual?union?all??
- select? n ' John Doe ', n ' language ', 65?from?dual?union?all??
- select? n ' John Doe ', n ' math ', 77?from?dual?union?all??
- select? n ' John Doe ', n ' English ', 65?from?dual?union?all??
- select? n ' John Doe ', n ' physics ', 85?from?dual?;??
- commit;??
want to see the results of the query :?
[HTML]? View Plain Copy
- John Doe ? 77?85?65?65?292??
- Zhang San ? 87?90?82?78?337 ??
2. using the wm_concat method
[HTML]? View Plain Copy
- SELECT? Student,wm_concat (Score), SUM (score)? From? Cc? GROUP? By? STUDENT;??
3. using the Oracle 11g pivot method
[HTML]? View Plain Copy
- SELECT? Kin.*,??
- ?? Kin.a+kin.b+kin.c+kin.d?as? Total??
- From??
- ?? (SELECT??????????????????????????????? *??
- ?? From? Cc? PIVOT? (? MAX (score)? For? COURSE? In? (' language '? As? A?,? ' Mathematics '? As? B,? ' English '? As? C, ' physical '? As? D)??
- ??)? KIN;??
4. using the DECODE method
[HTML]? View Plain Copy
- SELECT??
- Student,??
- MAX (Decode (COURSE,? ' ) language ',? Score))? A??
- MAX (DECODE (COURSE,? ' ) Mathematics ',? Score))? B??
- MAX (DECODE (COURSE,? ' ) English ',? Score))? C??
- MAX (DECODE (COURSE,? ' ) Physical ',? Score))? D??
- SUM (score)? Total??
- From??
- Cc??
- GROUP? By??
- Student;??
Such a problem, to find out his key points. In fact, it is a row to the column, this is a classmate in the itpub problem.
Solution to the problem:
Build table:
CREATE TABLE T_result
(d varchar2 (Ten), result Varchar2 (4));
Insert data:
INSERT into T_result values (' 2014-01-01 ', ' wins ');
INSERT into T_result values (' 2014-01-01 ', ' wins ');
INSERT into T_result values (' 2014-01-01 ', ' negative ');
INSERT into T_result values (' 2014-01-02 ', ' wins ');
INSERT into T_result values (' 2014-01-02 ', ' negative ');
INSERT into T_result values (' 2014-01-02 ', ' negative ');
?
To scan the table two times
Select T1.d,t1.c1 ' wins ', t2.c2 ' negative ' from
(select count (Result) c1,d from t_result where result = ' wins ' GROUP by D) T1
Left OUTER JOIN
(select count (Result) c2,d from t_result where result = ' negative ' GROUP by D) T2
On t1.d = t2.d
Row to column:
SELECT D,sum (Decode (result, ' wins ', 1,0)), SUM (decode (result, ' negative ', 1,0))
From T_result
GROUP by D
Or
Select D,
SUM (case result when ' wins ' then 1 else 0 end) wins,
SUM (case result when ' negative ' then 1 else 0 end) negative
From T_result GROUP by D order by D;
Oracle row-to-column (pivot, WM_CONCAT, decode) Usage summary (reprint)