SQL-将一列数据转到一行
我希望将单列的值拿到每个clientguid的相应行中。 语句的一个简单示例是:
62.5 | 12345 |
---|---|
42.1 | 我 |
要 | 完成 |
的 | 工作 |
是,如果
可能 | clientguid | 50.8 | 拥有的 |
---|---|---|---|
我 | 12345 | 12345 | dxcode |
帖子选择 这样的:
SELECT ClientGUID, DxCode
FROM MyTable
WHERE ClientGUID = 12345
任何想法或方向都将不胜感激!谢谢!
I am looking to take the values of a single column and transpose them into the corresponding row for each ClientGUID. A simple example of the of what I have post SELECT statement is:
ClientGUID | DxCode |
---|---|
12345 | 50.8 |
12345 | 62.5 |
12345 | 42.1 |
What I am trying to accomplish is this, if possible:
ClientGUID | DxCode1 | DxCode2 | DxCode3 |
---|---|---|---|
12345 | 50.8 | 62.5 | 42.1 |
For this example my SELECT statement looks a such:
SELECT ClientGUID, DxCode
FROM MyTable
WHERE ClientGUID = 12345
Any thoughts or direction would be greatly appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
You can use
PIVOT
for this, eg:Output:
You can use
PIVOT
for this, e.g.:Output:
在此类查询中,值的数量可能会有所不同,使用
STRING_AGG()
列出所有值通常更简单、更灵活。我们可以选择值之间使用的分隔符。一个有趣的选项是使用换行符并将值显示为垂直列表。
db<>fiddle 此处
In this type of query the number of values may vary and it is generally simpler and more flexible to use
STRING_AGG()
to list all the values.We can choose the seperator used between the values. One option which can be interesting is to use a newline and present the values as a vertical list.
db<>fiddle here