我有一个表格,其中插入与ID相关的值,这些值会随时间变化。
示例:
| Id | Timestamp | Value | | 1 | 2018-09-12 02:29:31.154 | 7.139 | | 1 | 2018-09-12 02:40:46.724 | 7.254 | | 2 | 2018-09-06 02:40:46.724 | 132.451 | | 1 | 2018-09-12 02:42:19.841 | 7.645 | | 3 | 2018-09-12 03:01:45.811 | 45.276 | | 1 | 2018-09-12 03:12:59.121 | 7.421 | | 2 | 2018-09-12 03:12:59.121 | 130.789 | | 1 | 2018-09-12 03:15:33.467 | 7.121 | | 2 | 2018-09-12 03:15:33.467 | 136.198 | | 3 | 2018-09-12 03:15:33.467 | 46.971 | | 2 | 2018-09-12 03:27:13.642 | 131.879 | | 3 | 2018-09-12 03:27:13.642 | 44.645 | | 1 | 2018-09-12 03:30:27.564 | 7.691 | | ... | ... | ... |
我的目标是进行单个查询,以获取Id
1、2和3的日期范围内的值,并将它们组合在一起。
结果将是这样的:
{ Timestamp: "2018-09-12 02:29:31.154", id1: 7.123, id2: null, id3: null }
{ Timestamp: "2018-09-12 02:40:46.724", id1: 7.254, id2: 132.451, id3: null }
{ Timestamp: "2018-09-12 02:42:19.841", id1: 7.645, id2: null, id3: null }
{ Timestamp: "2018-09-12 03:01:45.811", id1: null, id2: null, id3: 45.276 }
{ Timestamp: "2018-09-12 03:12:59.121", id1: 7.421, id2: 130.789, id3: null }
{ Timestamp: "2018-09-12 03:15:33.467", id1: 7.121, id2: 136.198, id3: 46.971 }
{ Timestamp: "2018-09-12 03:27:13.642", id1: null, id2: 131.879, id3: 44.645 }
{ Timestamp: "2018-09-12 03:30:27.564", id1: 7.691, id2: null, id3: null }
单个查询中可以吗?
答案 0 :(得分:1)
var ids = new List<int> { 1, 2, 3};
var result = (from item in db.Table
where ids.Contains(item.id) && item.Timestamp > fromDate && item.Timestamp < toDate
group item by item.Timestamp into sub
select new
{
Timestamp = sub.Key,
id1 = sub.Where(x => x.id == 1).Select(x => x.Value).FirstOrDefault(),
id2 = sub.Where(x => x.id == 2).Select(x => x.Value).FirstOrDefault(),
id3 = sub.Where(x => x.id == 3).Select(x => x.Value).FirstOrDefault()
}).ToList();
答案 1 :(得分:0)
在SQL Server 2017中使用STRING_AGG()
函数[documentation]时如何求助于字符串聚合呢?
SELECT [Timestamp]
-- building string very similar to what you've posted; you can certainly play with this column and format it the way you want
, STRING_AGG(CONCAT('id', CAST(Id AS nvarchar(50)), ': ', ISNULL(CAST([Value] AS nvarchar(50)), N'NULL')), ', ')
FROM schema.Table
GROUP BY [Timestamp];
如果您使用的是旧版SQL Server,逻辑将是相同的,那么您将诉诸于一种自定义的字符串聚合方式,例如以下一种:
当STRING_AGG()
不可用时,我本人通常使用XML解决方案。