Sql 如何将列中的行值分组并将行转换为列?
我的要求如下:在SQL2005中使用Sql查询,如果可能的话要避免存储过程Sql 如何将列中的行值分组并将行转换为列?,sql,Sql,我的要求如下:在SQL2005中使用Sql查询,如果可能的话要避免存储过程 EmpType EmpName Role 1 Ram Admin 2 Raja Admin 1 John update 2 Tom Admin 1 Adam Admin 我需要一个分组的输出,按以下方式清空:
EmpType EmpName Role
1 Ram Admin
2 Raja Admin
1 John update
2 Tom Admin
1 Adam Admin
我需要一个分组的输出,按以下方式清空:
EmpType EmpName Admin Update
1 Ram, John, Adam Adam,Ram John
2 Raja , Tom Tom,Raja
谁能帮帮我吗
谢谢,要执行此任务,您需要编写自己的包含字符串的聚合函数。下面是一个例子:
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize=8000)]
public struct strconcat : IBinarySerialize{
private List values;
public void Init() {
this.values = new List();
}
public void Accumulate(SqlString value) {
this.values.Add(value.Value);
}
public void Merge(strconcat value) {
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate() {
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r) {
int itemCount = r.ReadInt32();
this.values = new List(itemCount);
for (int i = 0; i <= itemCount - 1; i++) {
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w) {
w.Write(this.values.Count);
foreach (string s in this.values) {
w.Write(s);
}
}
}
您可能还希望选中,以执行编写自己的包含字符串的聚合函数所需的任务。下面是一个例子:
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize=8000)]
public struct strconcat : IBinarySerialize{
private List values;
public void Init() {
this.values = new List();
}
public void Accumulate(SqlString value) {
this.values.Add(value.Value);
}
public void Merge(strconcat value) {
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate() {
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r) {
int itemCount = r.ReadInt32();
this.values = new List(itemCount);
for (int i = 0; i <= itemCount - 1; i++) {
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w) {
w.Write(this.values.Count);
foreach (string s in this.values) {
w.Write(s);
}
}
}
如果您不想创建额外的结构(存储过程、函数等),您可能还需要选中此项:
select
EmpType,
stuff((select ', ' + EmpName from tmp t2 where t2.EmpType = t1.EmpType
for xml path('')),1,2,'') EmpName,
stuff((select ', ' + EmpName from tmp t2 where t2.EmpType = t1.EmpType and t2.Role = 'Admin'
for xml path('')),1,2,'') [Admin],
stuff((select ', ' + EmpName from tmp t2 where t2.EmpType = t1.EmpType and t2.Role = 'Update'
for xml path('')),1,2,'') [Update]
from tmp t1
group by EmpType
如果您不想创建额外的结构(存储过程、函数等),这就是您需要的:
select
EmpType,
stuff((select ', ' + EmpName from tmp t2 where t2.EmpType = t1.EmpType
for xml path('')),1,2,'') EmpName,
stuff((select ', ' + EmpName from tmp t2 where t2.EmpType = t1.EmpType and t2.Role = 'Admin'
for xml path('')),1,2,'') [Admin],
stuff((select ', ' + EmpName from tmp t2 where t2.EmpType = t1.EmpType and t2.Role = 'Update'
for xml path('')),1,2,'') [Update]
from tmp t1
group by EmpType
这对你有帮助吗?让我们知道。这对你有帮助吗?让我们知道。