如何对串联字段进行分组?

发布于 2024-11-28 04:38:09 字数 295 浏览 0 评论 0原文

假设我有一张供上学的学生使用的桌子,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-12-05 04:38:09

两个选项:

1)将连接列添加到 group by 子句中,

例如。

SELECT columna || columnb || columnc
FROM table
GROUP BY columna || columnb || columnc

2)分别添加组中连接列的每一列,例如

SELECT columna || columnb || columnc
FROM table
GROUP BY columna, columnb, columnc

Two options:

1) Add the concatenated column to the group by clause

Eg.

SELECT columna || columnb || columnc
FROM table
GROUP BY columna || columnb || columnc

2) Add each column that apart's the concatenated column in the group by seperately

Eg.

SELECT columna || columnb || columnc
FROM table
GROUP BY columna, columnb, columnc
眸中客 2024-12-05 04:38:09

您不需要连接它们,只需在 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.

等你爱我 2024-12-05 04:38:09
select Class_ID||Year as class_year, count(*)
from table
group by 1

使用group by,您可以引用数字列。

Anal-rententives 会建议您应该引用表达式,如下所示:group by Class_ID||Year,但我认为数字更清晰,因为它更容易阅读,尤其是因为如果您稍后< em>更改表达式,您不必记住更改分组依据中的表达式。

select Class_ID||Year as class_year, count(*)
from table
group by 1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文