我正在使用sqlite 3.15.1。
我有一张master桌子,里面有一所大学的时间表。
看上去:
day sem sec hour sub_id
---------- ---------- ---------- ---------- ----------
MON 5 B 4 10IS51
MON 5 B 4 10IS53
MON 5 B 5 10CS54
MON 5 B 6 10CS55
MON 5 B 7 10CS53
MON 3 A 1 10CS33 还有更多的价值观..。
,对于相同的其他值,有多个sub_id,意思是--星期一第一小时,第五名B级的学生可能有两个或更多的实验室(sub_id)。(分批进行)。
为了有一个适当的时间表,我会这样做:
select day,
max( case when hour =1 then sub_id end ) as 'hour-1',
max( case when hour =2 then sub_id end ) as 'hour-2',
max( case when hour =3 then sub_id end ) as 'hour-3',
max( case when hour =4 then sub_id end ) as 'hour-4',
max( case when hour =5 then sub_id end ) as 'hour-5',
max( case when hour =6 then sub_id end ) as 'hour-6',
max( case when hour =7 then sub_id end ) as 'hour-7',
max( case when hour =8 then sub_id end ) as 'hour-8'
from master
where sem=5 and sec='B'
group by day
order by day;但当出现多个值时,它只给出一个值,即max()值。当我使用min()时,我得到min()值。我怎样才能两者兼得?
结果视图如下所示:
day hour-1 hour-2 hour-3 hour-4 hour-5 hour-6 hour-7 hour-8
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
FRI 10CS52 10CS54 10CS53 10CS55 HRD HRD TUT
MON 10CSL58 10CSL58 10CSL58 10IS51 10CS54 10CS55 10CS53
SAT 10IS51 10CS55 10CS56 10CS52
THU 10CS53 10IS51 10CS54 10CS52
TUE 10CS54 10CS52 10CS56 10CS56
WED 10CS56 10IS51 10CS53 10CS55 CSA CSA CSA 但我想要这样的东西:
day hour-1 hour-2 hour-3 hour-4 hour-5 hour-6 hour-7 hour-8
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
FRI 10CS52,10CS53 10CS54 10CS53 10CS55 HRD HRD TUT
MON 10CSL58 10CSL58,10CSL33 10CSL58 10IS51 10CS54 10CS55 10CS53
SAT 10IS51,10IS48 10CS55 10CS56 10CS52
THU 10CS53 10IS51 10CS54 10CS52
TUE 10CS54 10CS52 10CS56 10CS56
WED 10CS56 10IS51 10CS53 10CS55 CSA CSA CSA 也就是说,所有类-逗号分隔,而不是min()或max()。
是否有可能做到这一点?请帮帮我。
谢谢。
发布于 2016-11-19 05:39:54
用GROUP_CONCAT替换MIN/MAX
select day,
group_concat( case when hour =1 then sub_id end ) as 'hour-1',
group_concat( case when hour =2 then sub_id end ) as 'hour-2',
group_concat( case when hour =3 then sub_id end ) as 'hour-3',
group_concat( case when hour =4 then sub_id end ) as 'hour-4',
group_concat( case when hour =5 then sub_id end ) as 'hour-5',
group_concat( case when hour =6 then sub_id end ) as 'hour-6',
group_concat( case when hour =7 then sub_id end ) as 'hour-7',
group_concat( case when hour =8 then sub_id end ) as 'hour-8'
from master
where sem=5 and sec='B'
group by day
order by day;https://stackoverflow.com/questions/40688895
复制相似问题