如何进行“group by”部分匹配

发布于 2024-11-06 18:15:03 字数 671 浏览 2 评论 0原文

我在 SQL 服务器中有一个表,其中项目代码和子项目代码位于相同的字段中。

结构是这样的

+----+------+-------------+-------+--------+--------+
| id | date | projectcode | debit | credit | budget |
+----+------+-------------+-------+--------+--------+
| 1  | bla  | A100        | bla
| 2  | bla  | A100.01     |
| 3  | bla  | A112        |
| 4  | bla  | A112.02

我如何做这样的选择

SELECT projectcode
  , sum(debit) as debit
  , sum(credit) as credit
  , sum(budget) as budget
FROM table1
GROUP BY -insert-answer-here-

我想要输出 group by A100 和 A100.01 和 A100.x 一起以及 A112 + A112.x

我该怎么做这?

我无法控制表格的结构。

I have a table in SQL-server with projectcodes and sub-project-codes in the same fields.

The stucture is something like this

+----+------+-------------+-------+--------+--------+
| id | date | projectcode | debit | credit | budget |
+----+------+-------------+-------+--------+--------+
| 1  | bla  | A100        | bla
| 2  | bla  | A100.01     |
| 3  | bla  | A112        |
| 4  | bla  | A112.02

How do I do a select like this

SELECT projectcode
  , sum(debit) as debit
  , sum(credit) as credit
  , sum(budget) as budget
FROM table1
GROUP BY -insert-answer-here-

I want the output to group by A100 and A100.01 and A100.x together as well as A112 + A112.x

How do I do this?

I have no control over the structure of the table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

不如归去 2024-11-13 18:15:04
GROUP BY LEFT(projectcode ,CHARINDEX('.',projectcode  + '.')-1)
GROUP BY LEFT(projectcode ,CHARINDEX('.',projectcode  + '.')-1)
我最亲爱的 2024-11-13 18:15:04

也许这会起作用:

SELECT Substring(projectcode, 1, 4) as Project
  , sum(debit) as debit
  , sum(credit) as credit
  , sum(budget) as budget
FROM table1
GROUP BY Substring(projectcode, 1, 4)

Maybe this would work:

SELECT Substring(projectcode, 1, 4) as Project
  , sum(debit) as debit
  , sum(credit) as credit
  , sum(budget) as budget
FROM table1
GROUP BY Substring(projectcode, 1, 4)
童话里做英雄 2024-11-13 18:15:04

如果项目代码始终遵循相同的模式(cnnn / cnnn.nn),您只需获取前四个字符:

group by substring(projectcode, 1, 4)

If the project code always follows the same pattern (cnnn / cnnn.nn) you can just get the first four characters:

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