如何通过列名将两个数据行联接到一个数据行中
本文关键字:数据 一个 何通过 两个 | 更新日期: 2023-09-27 18:25:58
我正试图弄清楚如何通过Department列名将两个相应的数据行连接到数据集中的单个数据行中。
在提供的数据集输出中,我想通过列名将胃肠病学和Medical Gastroen(两个数据行)连接到单个数据行(类似于合并行的必需最终数据集输出)。
需要你的想法/帮助如何在asp.net和/或C#中实现。
数据集输出
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 15 25 40
General Medicine 25 05 30
Medical Gastroen 30 20 50
合并行所需的最终数据集输出
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 45 45 90
General Medicine 25 05 30
我认为您必须在数据库查询中为此使用联接。那会更好。表A
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
表B.
+-----+---------------------+-------------+--------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
SQL查询SQL>选择ID、名称、金额、日期来自客户内部联接订单ON CUSTOMERS.ID=ORDERS.CUSTOMER_ID;
结果表:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
我希望它能帮助你。
您可以这样做。
DataTable _dataTable = new DataTable();
DataRow _dataRow1 = null;
_dataTable.TableName = "Products";
_dataTable.Columns.Add("ID",typeof(int));
_dataTable.Columns[0].AutoIncrementSeed = 1;
_dataTable.Columns[0].AutoIncrement = true;
_dataTable.Columns.Add("ProductsName");
_dataTable.Columns.Add("Price");
_dataRow1 = _dataTable.NewRow();
_dataRow1["ProductsName"] = "Sony Laptop";
_dataRow1["Price"] = "15000";
_dataTable.Rows.Add(_dataRow1);
DataRow _dataRow2 = null;
_dataRow2 = _dataTable.NewRow();
_dataTable.Rows.Add(_dataRow2);
_dataRow2["ProductsName"] = "LG Laptop";
_dataRow2["Price"] = "15000";
DataSet _dataSet = new DataSet();
_dataSet.Tables.Add(_dataTable);