Cited:
To illustrate this, a score table is created, as shown in:
Comparison:
1, in SQL, these two functions we can only use case, the code and the results are as follows:
Select Name,
Case Subject
When ' Chinese ' then 1
When ' math ' then 2
When ' English ' then 3--else 3
End as ' account code '
From Results
Similarly, we can use case to implement row to column, the code and results are as follows:
Select Name,
SUM (case when subject= ' language ' then Result end) ' Language ',
SUM (case when subject= ' math ' then Result end) ' Math ',
SUM (case when subject= ' English ' then Result end) ' English '
From Results
GROUP BY Name
2, however, in Oracle, these two functions we can use, the code and the results are as follows:
Decode usage: Select Name,decode (Subject, ' language ', 1, ' Math ', 2, ' English ', 3) Subject code from results;
Select Name,decode (Subject, ' language ', 1, ' Math ', 2, 3) Subject code from Results;
Case USAGE:
Select Name,
Case
When subject= ' language ' then 1
When subject= ' math ' then 2
When subject= ' English ' then 3--else 3
End as account code
From Results;
Both methods can achieve the same result:
Similarly, we can use these two functions to implement row-to-column, the code and results are as follows:
Decode
Select
Name,
SUM (Decode (Subject, ' language ', result,0)) language,
SUM (Decode (Subject, ' mathematics ', result,0)) Mathematics,
SUM (Decode (Subject, ' English ', result,0)) English
From Results
Group BY Name;
Case
Select
Name,
SUM (subject= ' then Result else 0 end)) language,
SUM (case when subject= ' math ' then Result else 0 end)) Mathematics,
SUM (case when subject= ' English ' then Result else 0 end)) English
From Results
Group BY Name;
Intermediate result of database table and row-to-column (think about it or attach it too)
In Oracle:
Select
Name,
Decode (Subject, ' language ', result,0) languages,
Decode (Subject, ' mathematics ', result,0) mathematics,
Decode (Subject, ' English ', result,0) English
From Results;
Or
Select
Name,
(Case when subject= ' language ' then Result else 0 end) language,
(Case when subject= ' math ' then Result else 0 end) Math,
(Case when subject= ' English ' then Result else 0 end) English
From Results;
The results are as follows:
Please correct me where the above is wrong. This article only wants to ask ...
SQL vs. Oracle's use of case and decode (row to column) and comparison (go