sql-server - Move an object up and separate based on item
问题描述
I have the following problem:
Apple | Document 1 | 5
Orange | Document 2 | 4
Apple | Document 3 | 7
What I would like to happen with a query is the following:
Apple | Document 1 | Document 3 | 12
Orange | Document 2 | 7
Is there a function that does this for you?
解决方案
在 SQL Server 2017 中,您可以使用以下STRING_AGG
功能GROUP BY
:
SELECT fruit, STRING_AGG(document, '|') AS documents, SUM(value) AS total
FROM t
GROUP BY fruit
推荐阅读
- javascript - HTML CSS:使用带有 SVG 图标的可折叠切换
- java - java流映射对象参数到hashset
- pycharm - 如何从 PEP8 命名约定中排除某些单词?
- java - 分发 Java LWJGL 程序
- functional-programming - 命题逻辑的解析器组合器
- stata - 使用零值对变量进行分类
- oracle - 在 PLSQL 中使用带有 Substr 的变量
- reactjs - 源组件在路由到其他网站页面时不断呈现
- laravel - Auth guard 仅在 laravel 中默认工作
- php - POST 数据时导致 mod_security 406 Not Acceptable 的原因是什么?