在线等:数据行列转换问题
这是我从数据库中根据一定条件查询出的结果。
collectdata typename collecttime
120.00收缩压上 2012/5/11 13:19:45
90.00舒张压下 2012/5/11 13:19:46
34.00血糖餐前 2012/5/11 13:19:47
36.00血糖餐后 2012/5/11 13:19:48
37.50体温 2012/5/11 13:19:49
150.00心率 2012/5/11 13:19:50
180.00收缩压上 2012/5/11 16:30:31
110.00舒张压下 2012/5/11 16:30:32
33.00血糖餐前 2012/5/11 16:30:33
38.00血糖餐后 2012/5/11 16:30:34
39.00体温 2012/5/11 16:30:35
190.00心率 2012/5/11 16:30:36
最后想要的结果如下:
120.00 90.00 34.00 36.00 37.50 150.00 13:19
180.00 110.00 33.00 38.00 39.00 190.00 16:30
求各位帮下忙了,先谢谢了。要求是不能在数据库中做对sql语句进行处理。
[解决办法]
网上有很多行列转换的SQL语句,你可以参考下,
另外,也可以显示的时候转换,参见
http://dotnet.aspx.cc/file/Convert-GridView-Row-With-Column.aspx
[解决办法]
要求是不能在数据库中做对sql语句进行处理。
这个有点意思 难不成都利用后台来去处理这业务
直接一条SQL语句的事
[解决办法]
DataTable dt = new DataTable(); dt.Columns.Add("collectdata"); dt.Columns.Add("typename"); dt.Columns.Add("collecttime"); dt.Rows.Add(new object[] { "120.00", "收缩压上", "2012/5/11 13:19:45" }); dt.Rows.Add(new object[] { "90.00", "舒张压下", "2012/5/11 13:19:46" }); dt.Rows.Add(new object[] { "34.00", "血糖餐前", "2012/5/11 13:19:47" }); dt.Rows.Add(new object[] { "36.00", "血糖餐后", "2012/5/11 13:19:48" }); dt.Rows.Add(new object[] { "37.50", "体温", "2012/5/11 13:19:49" }); dt.Rows.Add(new object[] { "150.00", "心率", "2012/5/11 13:19:50" }); dt.Rows.Add(new object[] { "180.00", "收缩压上", "2012/5/11 16:30:31" }); dt.Rows.Add(new object[] { "110.00", "舒张压下", "2012/5/11 16:30:31" }); dt.Rows.Add(new object[] { "33.00", "血糖餐前", "2012/5/11 16:30:33" }); dt.Rows.Add(new object[] { "38.00", "血糖餐后", "2012/5/11 16:30:34" }); dt.Rows.Add(new object[] { "39.00", "体温", "2012/5/11 16:30:35" }); dt.Rows.Add(new object[] { "190.00", "心率", "2012/5/11 16:30:36" }); DataTable newDt = new DataTable(); Dictionary<string, int> dicCount = new Dictionary<string, int>(); int index = 1; foreach (DataRow row in dt.Rows) { if (!dicCount.ContainsKey(row["typename"].ToString())) { dicCount.Add(row["typename"].ToString(), index++); } } Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>(); List<string> list; string key = string.Empty; foreach (DataRow row in dt.Rows) { key = Convert.ToDateTime(row["collecttime"]).ToString("yyyy/MM/dd HH:mm"); if (dic.ContainsKey(key)) { list = dic[key]; list[dicCount[row["typename"].ToString()]] = row["collectdata"].ToString(); dic[key] = list; } else { list = new List<string>(); list.AddRange(new string[dicCount.Count]); list[0] = key; list.Add(row["collectdata"].ToString()); list[dicCount[row["typename"].ToString()]] = row["collectdata"].ToString(); dic.Add(key, list); } } newDt.Columns.Add("时间"); foreach (KeyValuePair<string, int> k in dicCount) newDt.Columns.Add(k.Key); foreach (KeyValuePair<string, List<string>> k in dic) newDt.Rows.Add(k.Value.ToArray<string>()); /* 2012-05-11 13:19 120.00 90.00 34.00 36.00 37.50 150.00 2012-05-11 16:30 180.00 110.00 33.00 38.00 39.00 190.00 */