如何对串联字段进行分组?
假设我有一张供上学的学生使用的桌子,如下所示:
St_ID |St_Name|Class_ID|Year
------+-------+--------+----
02345|John | 345|2011
07871|Jane | 234|2010
等等。 我希望对每个类运行查询,我可以通过连接 Class_ID 和 Class_ID 来获得这些查询。年。 即 345-2011 是一类
如何通过在 Group by 查询中连接字符串来进行 count(*)?
Say I have a table for students attending school like this:
St_ID |St_Name|Class_ID|Year
------+-------+--------+----
02345|John | 345|2011
07871|Jane | 234|2010
and so on.
I wish to run queries on each Class which I can get by concatenating Class_ID & Year.
i.e 345-2011 is one class
How do I do count(*), by having the concatenated string in the Group by query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个选项:
1)将连接列添加到 group by 子句中,
例如。
2)分别添加组中连接列的每一列,例如
。
Two options:
1) Add the concatenated column to the group by clause
Eg.
2) Add each column that apart's the concatenated column in the group by seperately
Eg.
您不需要连接它们,只需在
GROUP BY
语句中列出所有三列即可。如果您确实必须连接,那么只需将连接命令放在 GROUP BY 中即可。
You don't need to concatenate them, just list all three columns in the
GROUP BY
statement.If you do have to concatenate then just put the concatenate command in the GROUP BY.
使用
group by
,您可以引用数字列。Anal-rententives 会建议您应该引用表达式,如下所示:
group by Class_ID||Year
,但我认为数字更清晰,因为它更容易阅读,尤其是因为如果您稍后< em>更改表达式,您不必记住还更改分组依据中的表达式。With
group by
you can refer to the column number.Anal-rententives will suggest you should refer to the expression, like this:
group by Class_ID||Year
, but I think it's clearer as the number, because it's easier to read and especially because if you later change the expression, you don't have to remember to also change the expression in the group by.