在C#应用程序中,我从Teradata表中填充了DataTable。 teradata表数据如下所示:
UnitNum UnitName
------- --------
123456 Location A1 - 123456
123456 Location B1 - 123456
我想创建如下所示的输出:
UnitNum UnitName
------- --------
123456 Location A1 / B1 - 123456
我已在C#中使用以下代码完成此操作:
string compSql2 = "SELECT UnitNum ,
UPPER(MAX((CASE a.RNK WHEN 1 THEN a.UnitName ELSE NULL END))) Location1,
UPPER(MAX((CASE a.RNK WHEN 2 THEN a.UnitName ELSE NULL END))) Location2
FROM (SELECT UnitNum , UnitName, RANK(UnitName) AS RNK
FROM idw_app_field_sync.compressor_list GROUP BY UnitNum) AS a
GROUP BY 1";
DataTable compdt = TeradataConnector.RunQuery(compSql2);
compdt.Columns.Add("UnitName");
for (int i = 0; i < compdt.Rows.Count; i++)
{
if (compdt.Rows[i].Field<string>("Location2") != null)
{
string loc1 = compdt.Rows[i].Field<string>("Location1");
string loc2 = compdt.Rows[i].Field<string>("Location2");
string unitname;
for (int j = 0; j < loc1.Length - 9; j++)
{
if (!loc2[j].Equals(loc1[j]))
{
unitname = loc2.Substring(0, loc2.Length - 9) + @" / " + loc1.Substring(j);
compdt.Rows[i].SetField<string>("UnitName", unitname);
}
}
}
else
{
compdt.Rows[i].SetField<string>("UnitName", compdt.Rows[i].Field<string>("Location1"));
}
}
如果可能的话,我想在Teradata SQL中完成此任务。但是,我不知道如何将字符串的字符与SQL进行比较,就像我使用C#一样。