表中的采样日期:
+--------+---------+---------+--------------+-----------+------------+---+
| School | Class | Student | Student desc | Section | Date | |
+--------+---------+---------+--------------+-----------+------------+---+
| ABC | Grade 2 | Stud 1 | AAA | Mango | 5/12/2015 | 1 |
| DEF | Grade 2 | Stud 1 | AAA | Mango | 12/25/2018 | |
| DEF | Grade 2 | Stud 1 | AAA | Orange | 9/8/2016 | |
| GHI | Grade 3 | Stud 2 | BBB | Apple | 12/28/2016 | 2 |
| JKL | Grade 3 | Stud 2 | BBB | Pear | 12/19/2016 | |
| ABC | Grade 2 | Stud 3 | CCC | Guava | 12/28/2016 | 3 |
| GHI | Grade 3 | Stud 4 | DDD | StarFruit | 9/8/2018 | 4 |
+--------+---------+---------+--------------+-----------+------------+---+
理想情况下,映射应为1名学生,只分配给班级中的一个部分。
我需要构建查询以获取数据以满足以下要求:-
- 无论学校需要为那些被分配到同一班中多个部分的学生显示不同的数据。
+--------+---------+---------+--------------+----------+------------+
| School | Class | Student | Student desc | Section | Date |
+--------+---------+---------+--------------+----------+------------+
| DEF | Grade 2 | Stud 1 | AAA | Mango | 12/25/2018 |
| DEF | Grade 2 | Stud 1 | AAA | Orange | 9/8/2016 |
| GHI | Grade 3 | Stud 2 | BBB | Apple | 12/28/2016 |
| JKL | Grade 3 | Stud 2 | BBB | Pear | 12/19/2016 |
+--------+---------+---------+--------------+----------+------------+
以下是如果获取学校信息则提供正确数据的查询 :
select distinct a.class
,a.student
,a.Stud desc
,a.section
,to_date(max(a.date),'MM-DD-YYYY')"Date"
from Table1 a,
( select class
,student
,count(distinct section) cot
from Table1 c
where 1=1
and class is not null
and incoming_qty >= 1
group by class
,student
Having count(distinct section) > 1
) b
where 1=1
and a.class = b.class
and a.student=b.student
and b.cot > 1
and b.class is not null
and a.incoming_qty_new >= 1
group by a.class,a.student,a.Stud desc,a.section
order by a.class,a.student,a.Stud desc,a.section;
但是尝试获取学校详细信息时,查询无法按预期工作。
请提出建议。
答案 0 :(得分:1)
这里是您的数据使用解析函数的示例。尝试针对您的特定情况扩展它。
WITH t(School, Class, Student, StudentDesc, SectionName, Dates) AS
(
SELECT 'ABC','Grade 2','Stud 1','AAA','Mango',date'2015-05-12' FROM dual UNION ALL
SELECT 'DEF','Grade 2','Stud 1','AAA','Mango',date'2018-12-25' FROM dual UNION ALL
SELECT 'DEF','Grade 2','Stud 1','AAA','Orange',date'2016-09-08' FROM dual UNION ALL
SELECT 'GHI','Grade 3','Stud 2','BBB','Apple',date'2016-12-28' FROM dual UNION ALL
SELECT 'JKL','Grade 3','Stud 2','BBB','Pear',date'2016-12-19' FROM dual UNION ALL
SELECT 'ABC','Grade 2','Stud 3','CCC','Guava',date'2016-12-28' FROM dual UNION ALL
SELECT 'GHI','Grade 3','Stud 4','DDD','StarFruit',date'2018-09-08' FROM dual
)
SELECT *
FROM (
SELECT t.*,
COUNT(DISTINCT SectionName) OVER (PARTITION BY Class, Student) AS cntStudentSections,
ROW_NUMBER() OVER (PARTITION BY Class, Student ORDER BY Dates) AS StudentRowNumber
FROM t
)
WHERE cntStudentSections > 1 AND StudentRowNumber = 1;
答案 1 :(得分:0)
您可以使用分析功能:
select t1.*
from (select t1.*,
count(*) over (partition by class, student, section) as cnt
from table1 t1
) t1
where cnt >= 2;