在sqlite中合并未知数量的值 [英] Concatinate unknown number of values in sqlite
问题描述
我正在使用sqlite 3.15.1。
I am using sqlite 3.15.1 .
我有一个表 master
包含一所大学的主时间表
I have a table master
containing master timetable of a college.
看起来像是:
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 ,这意味着-在星期一1st小时,第五位B的学生可能有两个或更多的实验(sub_id)。 (分批进行)。
There are multiple sub_id for same other values , meaning - On Monday 1st hour, 5th B students might have 2 or more lab (sub_id). (Its conducted in batches).
要获得适当的时间表,我正在这样做:
To get a proper timetable, I am doing this :
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()值。 如何同时获得这两者?
But it gives only one value when multiple values occur i.e, the max()
value. When I use min()
, I get the min() value. How can I get both ?
结果视图如下:
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
但是我想要这样的东西:
But I want something like this :
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 ()。
That is, All the classes - comma separated , instead of min() or max().
是否可以实现?
谢谢。
推荐答案
替换最小/最大使用GROUP_CONCAT
Replace MIN/MAX with GROUP_CONCAT
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;
这篇关于在sqlite中合并未知数量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!