[英]Mysql Group by With Condition
The below data shows time schedule having elective/ non-elective subjects of a student. 以下数据显示了具有学生选修科目/非选修科目的时间表。 My requirement is to select those rows when both elec and nonelec type has same period so in this case select elec type.
我的要求是,当电子类型和非电子类型都具有相同的周期时选择那些行,因此在这种情况下,请选择电子类型。
Means for a day's schedule elective (type elec) should be given preference when both having same period. 当两者的时间段相同时,应优先选择一天的日程选择方式(电子类型)。 And when type elec does not has period like period 5 then select the non-elective one.
并且当elec类型不具有如句点5的句点时,则选择非选修句点。
My Query 我的查询
SELECT s.sch_id, s.sch_subtype, sd.sdetail_id, sd.sdetail_period
FROM schedule s
INNER JOIN schedule_detail sd ON s.sch_id = sd.sdetail_schedule
WHERE '2014-04-30'
BETWEEN sch_datefrom
AND sch_dateto
AND
(
(
sch_section =1
AND sch_subtype = 'nonelec'
)
OR
(
sch_subtype = 'elec'
AND 272
IN
(
SELECT edetail_stuid
FROM elective_detail
WHERE edetail_elective = sch_section
)
)
)
AND sch_course =3
AND sch_batch =2
AND sch_termid =2
AND sdetail_day = 'wed'
AND sdetail_period >0
AND CASE WHEN sch_subtype = 'nonelec'
THEN 1 =1
WHEN sch_subtype = 'elec'
THEN sdetail_subject >0
AND sdetail_faculty >0
AND sdetail_room >0
END GROUP BY CASE WHEN sch_subtype = 'elec'
THEN sdetail_period
ELSE 1
END ORDER BY sdetail_period
Output of above query 以上查询的输出
Required Output 所需输出
You can try something of this sort : 您可以尝试以下方法:
SELECT sch_id,sch_subtype,sdetail_id,sdetail_period
FROM table
WHERE condition
group by sch_subtype
It would be better if you can post the query which u tried. 如果您可以发布您尝试过的查询,那就更好了。
Dont really get whats the problem here: 不要真正在这里得到什么问题:
SELECT * FROM schedule s
JOIN schedule_detail sd ON s.sch_id = sd.sdetail_schedule
LEFT JOIN elective_detail ed ON sd.sch_section = ed.edetail_elective
WHERE
(
sd.sch_section =1
AND s.sch_subtype = 'nonelec'
)
OR
(
ed.edetail_stuid = 227
AND sch_subtype = 'elec'
)
GROUP BY sd.sdetail_period
SQL Fiddle: http://sqlfiddle.com/#!2/69d4e/1 SQL小提琴: http ://sqlfiddle.com/#!2 / 69d4e / 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.