如何进行“group by”部分匹配
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这会起作用:
Maybe this would work:
如果项目代码始终遵循相同的模式(cnnn / cnnn.nn),您只需获取前四个字符:
If the project code always follows the same pattern (cnnn / cnnn.nn) you can just get the first four characters: