[英]organizing query result in sql developer (oracle 11g)
Currently i have a table called schedule
in db (SQL developer). 目前,我在db中有一个名为schedule
的表(SQL开发人员)。
Assuming the availableID
s are 1,3,7,8. 假设availableID
为1,3,7,8。 The table consists of something like this: 该表包含以下内容:
Stud Title Supervisor Examiner availableID
abc Hello 1024 1001 1
def Hi 1024 1001 1
ghi Hey 1002 1004 1
xxx hhh 1020 1011 1
jkl hhh 1027 1010 1
try ttt 1001 1011 1
654 bbb 1007 1012 1
gyg 888 1027 1051 1
yyi 333 1004 1022 3
fff 111 1027 1041 3
ggg 222 1032 1007 3
hhh 444 1007 1001 3
ppp 444 1005 1072 7
ooo 555 1067 1009 7
uuu 666 1030 1010 7
yyy 777 1004 1001 7
qqq yhh 1015 1072 8
www 767 1017 1029 8
eee 566 1030 1020 8
rrr 888 1004 1031 8
abc 5555 1045 1051 8
As you can see, I have sort these value using ORDER BY availableID asc
. 如您所见,我使用ORDER BY availableID asc
对这些值进行了排序。 However, I would like to ORGANIZE them again into something like this: 但是,我想再次将它们组织成这样的东西:
Stud Title Supervisor Examiner availableID
abc Hello 1024 1001 1
def Hi 1024 1001 1
ghi Hey 1002 1004 1
xxx hhh 1020 1011 1
yyi 333 1004 1022 3
fff 111 1027 1041 3
ggg 222 1032 1007 3
hhh 444 1007 1001 3
ppp 444 1005 1072 7
ooo 555 1067 1009 7
uuu 666 1030 1010 7
yyy 777 1004 1001 7
qqq yhh 1015 1072 8
www 767 1017 1029 8
eee 566 1030 1020 8
rrr 888 1004 1031 8
jkl hhh 1027 1010 1
try ttt 1001 1011 1
654 bbb 1007 1012 1
gyg 888 1027 1051 1
........
abc 5555 1045 1051 8
For every availableID
it will called four times then proceed to next availableID
. 对于每个availableID
,它将调用四次,然后进入下一个availableID
。 Next it will iterate back to the lowest ID but using different other values. 接下来,它将迭代回最低的ID,但使用其他不同的值。 Stud
must be distinct. Stud
必须与众不同。
Is it possible to achieve this by using sql query? 是否有可能通过使用sql查询来实现?
You can do this with row_number()
and some arithmetic. 您可以使用row_number()
和一些算术来做到这一点。 Something like: 就像是:
Select t.*
From (select t.*,
Row_number() over (partition by availableid order by stud) as seqnum
From t
) t
Order by trunc((seqnum - 1) / 4), availableid
A slightly another equivalent approach as above, using floor and partitioning and grouping by the same availableID column - 如上所述的另一种等效方法,即使用楼板,分区和按同一availableID列进行分组-
select a.stud,
a.title,
a.supervisor,
a.examiner,
a.availableID
from ( select s.*,row_number() over (partition by availableID order by availableID) rn
from student s) a
order by floor((rn-1)/4),availableID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.