我在SQL / C#中有一个Table / DataTable,其数据如下所示:
Description Name
ABC AB
ABCD AB
ABCD CD
ABCDF AB
ABCD BC
ABCDF BC
我正在尝试获取如下输出(名称应该连接以便描述):
Description Name
ABC AB
ABCD AB, BC and CD
ABCDF AB and BC
我在输出中按字母顺序写了名字,但这不是必需的。 例如,第二行的名称可以是 AB,CD和BC 或 BC,CD和AB 。 我只需要字符串中的所有名称来获取特定描述。
如何在SQL或C#中获得所需的输出?请帮帮我。
答案 0 :(得分:1)
我在C#和SQL中都得到了解决方案,有没有其他方法可以获得相同的输出? 如果性能很重要,我应该使用哪种方法(sql或c#)?
C#:
static void Main(string[] args)
{
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("Name"));
dt.Columns.Add(new DataColumn("Description"));
DataRow dr = dt.NewRow();
dr["Name"] = "AB";
dr["Description"] = "ABC";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["Name"] = "AB";
dr1["Description"] = "ABCD";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["Name"] = "CD";
dr2["Description"] = "ABCD";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["Name"] = "AB";
dr3["Description"] = "ABCDF";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["Name"] = "BC";
dr4["Description"] = "ABCD";
dt.Rows.Add(dr4);
DataRow dr5 = dt.NewRow();
dr5["Name"] = "BC";
dr5["Description"] = "ABCDF";
dt.Rows.Add(dr5);
StringBuilder sb = new StringBuilder();
var grouped = from table in dt.AsEnumerable()
group table by new { DescriptionCol = table["Description"] } into groupby
select new
{
Value = groupby.Key,
ColumnValues = groupby
};
foreach (var key in grouped)
{
Console.WriteLine(key.Value.DescriptionCol);
Console.WriteLine("---------------------------");
int i = 1;
foreach (var columnValue in key.ColumnValues)
{
string comma = " , ";
string and = " and ";
if (i > 1 && i < key.ColumnValues.Count())
{
sb.Append(comma);
}
else if (i > 1 && i == key.ColumnValues.Count())
{
sb.Append(and);
}
sb.Append(columnValue["Name"].ToString());
i++;
}
Console.WriteLine(sb.ToString());
sb.Clear();
Console.WriteLine();
}
Console.ReadLine();
}
SQL:
create table myTable
(
Description nvarchar(100),
Name nvarchar(100)
)
insert into myTable values
('ABC','AB'),
('ABCD','AB'),
('ABCD','CD'),
('ABCD','BC'),
('ABCDF','BC'),
('ABCDF','AB')
create table myTable1
(
Description nvarchar(100),
Name nvarchar(100)
)
insert into mytable1 (Description, Name)
SELECT Description, Name = STUFF((SELECT ', ' + Name
FROM myTable AS t2 WHERE t2.Description = t.Description
ORDER BY Description
FOR XML PATH('')), 1, 1, '')
FROM myTable AS t
GROUP BY Description
ORDER BY Description;
--select * from mytable1
select
Description,
ISNULL(
REVERSE(STUFF(REVERSE(Name), CHARINDEX(',', REVERSE(Name), 0),1,'dna ')),
Name) Name
From mytable1
select Description, SUBSTRING(Name, 0, (len(Name) - charindex(',', reverse(Name)))) +
Replace(SUBSTRING(Name, (len(Name) - charindex(',', reverse(Name))), len(Name)),
', ', ' and ') from mytable1
答案 1 :(得分:0)
如果要将多行连接成单个文本字符串,则有不同的方法。 SQL答案取决于您的数据库系统。在Microsoft SQL Server上,您可以使用FOR XML PATH
方法。请参阅此处的讨论Concatenate many rows into a single text string?。
答案 2 :(得分:0)
CREATE TABLE tb(aaa INT,bbb INT)
Go
INSERT INTO tb
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 1,4 UNION ALL
SELECT 2,2 UNION ALL
SELECT 2,5
select
aaa,
[values]=stuff(replace(replace((select [bbb]
from tb
where aaa=t.aaa for xml AUTO),
'"/><tb bbb="',','),
'"/>',''),1,9,'')
from tb t
group by aaa