我做了一些個人查詢,我想合併在一張表中。每個查詢檢索F1,F2,F3和總數的日期和值。簡化SQL代碼
我想知道我是否可以使這個查詢更容易,尤其是如何實現這一點。所以更少的代碼,但實現相同的事情。特別是當我想稍後將其擴展到F20時。
查詢只是將數據每15分鐘添加一次。
2017-09-20 10:15 |49.0000|f1.1
2017-09-20 10:15 |40.0000|f1.2
2017-09-20 10:15 |11.0000|f1.3
2017-09-20 10:15 |0.0000|f1.4
2017-09-20 10:15 |0.0000 |f1.5
2017-09-20 10:15 |0.0000 |f1.6
查詢給我時間的總和。 2017-09-20 10:15
如果我嘗試條件聚合。它給出了所有delta值的總和。但它應該只將相同日期的值相加。
declare @days int
set @days = -165
SELECT a.Date, a.F1, b.F2, c.F3, d.Total
FROM
(SELECT rv.Date, sum(rv.value_num) as F1
FROM dbo.Channels c INNER JOIN
(SELECT rv.*, TRY_CONVERT(numeric(20, 4), rv.Value) AS value_num, (LEFT(TRY_CONVERT(datetime2(0), rv.Timestamp), 16)) AS Date
FROM dbo.RecordedValues rv) rv ON c.SourceId = rv.SourceId AND c.Id = rv.ChannelId
WHERE c.Tag LIKE N'%F1%'
AND Timestamp BETWEEN DATEADD(day,@days,GETDATE()) AND GETDATE()
GROUP BY rv.Date
) as a,
(SELECT rv.Date, sum(rv.value_num) as F2
FROM dbo.Channels c INNER JOIN
(SELECT rv.*, TRY_CONVERT(numeric(20, 4), rv.Value) AS value_num, (LEFT(TRY_CONVERT(datetime2(0), rv.Timestamp), 16)) AS Date
FROM dbo.RecordedValues rv) rv ON c.SourceId = rv.SourceId AND c.Id = rv.ChannelId
WHERE c.Tag LIKE N'%F2%'
AND Timestamp BETWEEN DATEADD(day,@days,GETDATE()) AND GETDATE()
GROUP BY rv.Date
) as b,
( SELECT rv.Date, sum(rv.value_num) as F3
FROM dbo.Channels c INNER JOIN
(SELECT rv.*, TRY_CONVERT(numeric(20, 4), rv.Value) AS value_num, (LEFT(TRY_CONVERT(datetime2(0), rv.Timestamp), 16)) AS Date
FROM dbo.RecordedValues rv) rv ON c.SourceId = rv.SourceId AND c.Id = rv.ChannelId
WHERE c.Tag LIKE N'%F3%'
AND Timestamp BETWEEN DATEADD(day,@days,GETDATE()) AND GETDATE()
GROUP BY rv.Date
) as c,
(SELECT rv.Date, sum(rv.value_num) as Total
FROM dbo.Channels c INNER JOIN
(SELECT rv.*, TRY_CONVERT(numeric(20, 4), rv.Value) AS value_num, (LEFT(TRY_CONVERT(datetime2(0), rv.Timestamp), 16)) AS Date
FROM dbo.RecordedValues rv) rv ON c.SourceId = rv.SourceId AND c.Id = rv.ChannelId
WHERE c.Tag LIKE N'%F%'
AND Timestamp BETWEEN DATEADD(day,@days,GETDATE()) AND GETDATE()
GROUP BY rv.Date
) as d
where a.Date = b.Date
AND b.Date = c.Date
AND c.Date = d.Date
這是目前如何給我的價值觀:
RecDate F1 |F2 |F3 |Total
2017-09-20 10:15| 100 |200|100|400
2017-09-20 10:30| 150 |200|150|500
2017-09-20 10:45| 125 |200|100|425
2017-09-20 11:00| 110 |210|110|440
etc.
即使您簡化查詢,性能也會變差*,因爲像LIKE'%F1%'這樣的每個子句都會強制進行全表掃描。 'tag'包含什麼? –
如果您可以提供原始數據的樣本,以及您期望輸出的樣子,這將是一個很大的幫助。看看[how-to-ask](http://stackoverflow.com/help/how-to-ask)頁面,瞭解一些提煉你的問題的提示。 – gmiley
爲什麼'(LEFT(TRY_CONVERT(datetime2(0),rv.Timestamp),16)''你將* something *轉換爲datetime2然後轉換爲一個字符串,然後將其截斷?爲什麼?是否要將某些東西轉換爲'日期'也許?'時間戳'的類型是什麼? –