某些格式选择与分组依据在后gresql

所以我必须通过在后格雷SQL中读取查询来在Java中编写CSV。问题是csv必须具有某种格式,我无法做到这一点。我试图在postgresql中完成这项工作,但如果这是不可能的,我也接受关于如何在Java中解决它的想法。


我尝试了很多按语法分组的方法,但它无法正常工作,在这一点上,我甚至不确定它在postgresql中是否可行,但如果可能的话,我想这样做。假设我想看到所有学生参加一个班级分组。我以为这是因为日期,但我删除了它,它没有区别。


SELECT classID,

       firstName || secondName || fathersName,

       studentGender,

       to_char(studentBirthDate, 'DD.MM.YYYY HH24:mm:ss')

FROM Student, Class

WHERE studentGender = Male

GROUP BY classID, firstName, secondName, fathersName, studentGender, sutdentBirthdate

ORDER BY studentID;

我期望以下结果:


1, PaulLoganDan, JakeIanGolagan, JohnDoeNick, Male, NotSureHere

2, MatthewJoshuaSamuel, JosephJamesBenjamin, AdamLukeHarry, LewisNathanBrad, Male, NotSureHere

我得到了什么:


1, PaulLoganDan, Male, 1.1.1999 \n

1, JakeIanGolagan, Male, 2.2.1999 \n

1, JohnDoeNick, Male, 3.3.1999 \n


2, MatthewJoshuaSamuel, Male, 4.4.1999 \n

2, JosephJamesBenjamin, Male, 5.5.1999 \n

2, AdamLukeHarry, Male, 6.6.1999 \n

2, LewisNathanBrad, Male, 7.7.1999 \n


扬帆大鱼
浏览 82回答 1
1回答

弑天下

我相信STRING_AGG功能是您正在寻找的,并且是为此类问题而设计的。它自后greSQL 9.0以来就存在了。我认为它应该看起来像这样:SELECT classID,       STRING_AGG(firstName || secondName || fathersName, ', ')FROM Student, ClassWHERE studentGender = MaleGROUP BY classIDORDER BY studentID;或者,从版本8.4开始,有ARRAY_AGG功能:SELECT classID,       ARRAY_TO_STRING(ARRAY_AGG(firstName || secondName || fathersName), ', ')FROM Student, ClassWHERE studentGender = MaleGROUP BY classIDORDER BY studentID;我不认为在旧版本中存在用于此的聚合函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java