SQL vs. Oracle's use of case and decode (row to column) and comparison (go

Source: Internet
Author: User

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

Related Article

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.