Database actual combat Full notes exercises (1) Teaching information query

Source: Internet
Author: User

This instance runs through the oracle10g:

First, the topic requirements

The instructor's teaching information is stored in the schedule table:

--------------------------------
Teacherid WEEKDAY Hascourse
---------- ---------- ----------
1 2 1
1 3 1
2 1 1
3 2 1
1 2 1
--------------------------------
-----------------------------------------------------------------
Design the query, output the following information (the number of days in the week: corresponding to the class of Teachers week):
Teacherid MONDAY Tuesday Wednesday Thursday FRIDAY
---------- ---------- ---------- ---------- ---------- ----------
1 0 2 1 0 0
2 1 0 0 0 0
3 0 1 0 0 0

-----------------------------------------------------------------

Second, Analytical solutions

1. Build a Table statement


2. Query statements
The first step is to get the following result:
Teacherid MONDAY Tuesday Wednesday Thursday FRIDAY
--------- ---------- ---------- ---------- ---------- ----------
1 0 1 0 0 0
1 0 0 1 0 0
2 1 0 0 0 0
3 0 1 0 0 0
1 0 1 0 0 0
The query statements are:

Select  teacherid,case when  Weekday=1 then 1 else 0 end monday,case If  weekday=2 then 1 else 0 end Tuesday, c ASE when  weekday=3 then 1 else 0 end wednesday,case when  weekday=4 and 1 else 0 end thursday,case when  weekda Y=5 then 1 else 0 end Fridayfrom SCHEDULE;
In the second step, the results of the first step are grouped and summed to get the final result:

Select  teacherid,sum (case if  Weekday=1 then 1 else 0 end) Monday,sum (case if  weekday=2 then 1 else 0 end) Tuesday,sum (case if  weekday=3 then 1 else 0 end) Wednesday,sum (case if  weekday=4 then 1 else 0 end) Thursday,s Um (case if  weekday=5 then 1 else 0 end) Fridayfrom schedulegroup by Teacherid;
If the data contains null, you can call the NVL function with the following statement, as follows:

Select TEACHERID,NVL ((SUM (decode (Weekday, ' 1 ', 1)), 0) as  monday,nvl ((SUM (decode (Weekday, ' 2 ', 1)), 0)  as TUESDAY,NVL ((SUM (decode (Weekday, ' 3 ', 1)), 0) as  wednesday,nvl ((SUM (decode (Weekday, ' 4 ', 1)), 0) as  Thursday , Nvl ((SUM (Decode (Weekday, ' 5 ', 1)), 0) as  fridayfrom ScheduleGroup by Teacherid;

Query results such as:





Database actual combat Full notes exercises (1) Teaching information query

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.