我有两个名为
的表class_record_hist ,它会存储特定日期的课程出勤条目
student_record_hist ,它将存储特定班级的学生出勤列表。这里,transaction_id将是两个表的主键
我的数据看起来像这样
Working_date STUDENT_NAME STUDENT_STATUS ATTENDANCE_CODE TEACHER_UPDATE TRANSACTION_ID
------------- ------------ -------------- --------------- -------------- --------------
2017-11-10 10:00:00 JAMES ABSENT 2344 AB 23453
2017-11-10 10:00:00 PETER ABSENT 2344 AB 23453
2017-11-10 10:00:00 MURPHY PRESENT 1001 PR 23453
2017-11-10 10:00:00 MICA PRESENT 1001 PR 23453
2017-11-10 10:00:00 STELLA PRESENT 1001 PR 23453
2017-11-10 10:00:00 STEPHEN PRESENT 1001 PR 23453
2017-11-10 10:00:00 TRACY PRESENT 1001 PR 23453
2017-11-10 14:00:00 JAMES PRESENT 1001 PR 23454
2017-11-10 14:00:00 PETER PRESENT 1001 PR 23454
2017-11-10 14:00:00 MURPHY PRESENT 1001 PR 23454
2017-11-10 14:00:00 MICA PRESENT 1001 PR 23454
2017-11-10 14:00:00 STELLA PRESENT 1001 PR 23454
2017-11-10 14:00:00 STEPHEN PRESENT 1001 PR 23454
2017-11-10 14:00:00 TRACY ABSENT 2344 AB 23454
2017-11-11 11:24:00 JAMES PRESENT 1001 PR 23454
2017-11-11 11:24:00 PETER PRESENT 1001 PR 23454
2017-11-11 11:24:00 MURPHY PRESENT 1001 PR 23454
2017-11-11 11:24:00 MICA PRESENT 1001 PR 23454
2017-11-11 11:24:00 STELLA PRESENT 1001 PR 23454
2017-11-11 11:24:00 STEPHEN PRESENT 1001 PR 23454
2017-11-11 11:24:00 TRACY PRESENT 1001 PR 23454
2017-11-11 14:24:00 JAMES ABSENT 2344 AB 23454
2017-11-11 14:24:00 PETER ABSENT 2344 AB 23454
2017-11-11 14:24:00 MURPHY ABSENT 2344 AB 23454
2017-11-11 14:24:00 MICA ABSENT 2344 AB 23454
2017-11-11 14:24:00 STELLA ABSENT 2344 AB 23454
2017-11-11 14:24:00 STEPHEN PRESENT 1001 PR 23454
2017-11-11 14:24:00 TRACY PRESENT 1001 PR 23454
我将根据此记录生成报告
这是我的查询,如
select * from
(
select student_name, student_status,attendance_code,
working_date,class_id from
(
select to_char(a.date_tm,'yyyy-mm-dd hh24:mi:ss') AS working_date,
b.student_name,b.student_status,b.attendance_code,
b.teacher_update,a.transaction_id
from class_record_hist a,student_record_hist b
where a.school = 'DON BOSCO'
and a.building = 'A1'
and a.class_id = 'DB-3452'
and a.date_tm >= to_date('2017-11-10 06:00:00','yyyy-mm-dd hh24:mi:ss')
and a.date_tm <= to_date('2017-11-11 18:00:00','yyyy-mm-dd hh24:mi:ss')
and a.transaction_id = b.transaction_id
order by working_date desc,b.student_name asc
)
)
PIVOT
(
max(attendance_code) as code,
max(student_status) as status
for student_name in ('JAMES','PETER','MURPHY','MICA','STELLA','STEPHEN','TRACY');
) ;
结果:
WORKING_DATE CLASS_ID JAMES_STATUS JAMES_CODE PETER_STATUS PETER_CODE MURPHY_STATUS MURPHY_CODE MICA_STATUS MICA_CODE STELLA_STATUS STELLA_CODE STEPHEN_STATUS STEPHEN_CODE TRACY_STATUS TRACY_CODE
------------ -------- ------------ ---------- ------------ ---------- ------------- ----------- ----------- --------- ------------- ----------- -------------- ------------ ------------ ----------
2017-11-10 10:00:00 DB-3452 PRESENT 2344 PRESENT 2344 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001
2017-11-10 14:00:00 DB-3452 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 2344
2017-11-11 11:24:00 DB-3452 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001 PRESENT 1001
2017-11-11 14:24:00 DB-3452 PRESENT 2344 PRESENT 2344 PRESENT 2344 PRESENT 2344 PRESENT 2344 PRESENT 1001 PRESENT 1001
从上面的结果中,您可以看到数据不正确。
让詹姆斯说,他缺席了两次会议,从他的代码2344可以看出,但状态仍然显示为现在。同样的问题发生在整套记录。
我做错了什么?
任何建议&amp;非常感谢投入?
答案 0 :(得分:2)
min(student_status)等于max(attendance_code)
所以相应地调整枢轴
CREATE TABLE MY_RESULT
(WORKING_DATE timestamp, STUDENT_NAME varchar2(7), STUDENT_STATUS varchar2(7), ATTENDANCE_CODE int, TEACHER_UPDATE varchar2(2), TRANSACTION_ID int)
;
INSERT ALL
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'JAMES', 'ABSENT', 2344, 'AB', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'PETER', 'ABSENT', 2344, 'AB', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'MURPHY', 'PRESENT', 1001, 'PR', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'MICA', 'PRESENT', 1001, 'PR', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'STELLA', 'PRESENT', 1001, 'PR', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'STEPHEN', 'PRESENT', 1001, 'PR', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 10:00:00 AM', 'TRACY', 'PRESENT', 1001, 'PR', 23453)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'JAMES', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'PETER', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'MURPHY', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'MICA', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'STELLA', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'STEPHEN', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('10-Nov-2017 02:00:00 PM', 'TRACY', 'ABSENT', 2344, 'AB', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'JAMES', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'PETER', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'MURPHY', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'MICA', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'STELLA', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'STEPHEN', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 11:24:00 AM', 'TRACY', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'JAMES', 'ABSENT', 2344, 'AB', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'PETER', 'ABSENT', 2344, 'AB', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'MURPHY', 'ABSENT', 2344, 'AB', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'MICA', 'ABSENT', 2344, 'AB', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'STELLA', 'ABSENT', 2344, 'AB', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'STEPHEN', 'PRESENT', 1001, 'PR', 23454)
INTO MY_RESULT (WORKING_DATE, STUDENT_NAME, STUDENT_STATUS, ATTENDANCE_CODE, TEACHER_UPDATE, TRANSACTION_ID)
VALUES ('11-Nov-2017 02:24:00 PM', 'TRACY', 'PRESENT', 1001, 'PR', 23454)
SELECT * FROM dual
;
Oracle 11g R2架构设置:
select *
from (
select
student_name, student_status,attendance_code,working_date,class_id
from (
select * from my_result
) d
) d2
PIVOT (
max(attendance_code) as code,
min(student_status) as status
for student_name in ('JAMES','PETER','MURPHY','MICA','STELLA','STEPHEN','TRACY')
)
查询1 :
| WORKING_DATE | 'JAMES'_CODE | 'JAMES'_STATUS | 'PETER'_CODE | 'PETER'_STATUS | 'MURPHY'_CODE | 'MURPHY'_STATUS | 'MICA'_CODE | 'MICA'_STATUS | 'STELLA'_CODE | 'STELLA'_STATUS | 'STEPHEN'_CODE | 'STEPHEN'_STATUS | 'TRACY'_CODE | 'TRACY'_STATUS |
|-----------------------|--------------|----------------|--------------|----------------|---------------|-----------------|-------------|---------------|---------------|-----------------|----------------|------------------|--------------|----------------|
| 2017-11-11 14:24:00.0 | 2344 | ABSENT | 2344 | ABSENT | 2344 | ABSENT | 2344 | ABSENT | 2344 | ABSENT | 1001 | PRESENT | 1001 | PRESENT |
| 2017-11-11 11:24:00.0 | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT |
| 2017-11-10 10:00:00.0 | 2344 | ABSENT | 2344 | ABSENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT |
| 2017-11-10 14:00:00.0 | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 1001 | PRESENT | 2344 | ABSENT |
<强> Results 强>:
SELECT
to_char(c.date_tm, 'yyyy-mm-dd hh24:mi:ss') AS working_date
, s.student_name
, s.student_status
, s.attendance_code
, s.teacher_update
, c.transaction_id
FROM class_record_hist c
INNER JOIN student_record_hist s ON c.transaction_id = s.transaction_id
WHERE c.school = 'DON BOSCO'
AND c.building = 'A1'
AND c.class_id = 'DB-3452'
AND c.date_tm >= to_date('2017-11-10 06:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND c.date_tm <= to_date('2017-11-11 18:00:00', 'yyyy-mm-dd hh24:mi:ss')
--ORDER BY
-- working_date DESC
--, s.student_name ASC
超过25年前,ANSI标准正式化了“显式”连接语法,这是关于你加入这一变化的时间。 e.g。
<table width="500" border="0" cellpadding="5">
<tr>
<?php
$xml=simplexml_load_file("movie.xml") or die("Error: Cannot create object");
foreach($xml->children() as $movie) :
if($xml->sad<=$_GET['happiness']
&& $xml->agitated<=$_GET['calmness']
&& $xml->tired<=$_GET['tired']
&& $xml->scared<=$_GET['scared'] )
{?>
<td align="center" valign="center">
<img src="<?php echo $movie->path?>" width="150px" alt="<?php echo $movie->descrip?>"/>
<br />
<?php echo $movie->name ?>
</td>
<?php } endforeach; ?>
</tr>
</table>
顺便说一句,您不应该在该子查询中使用订单