我在表中有3列,分别是Id(int),Value(int)和Time(datetime)。我想要查询获得前10分钟的数据,然后获得第2组10分钟的数据并继续...
表中的数据示例
我尝试使用此查询,但它没有显示任何内容
SELECT sensor_Id
,sensor_Value
,sensor_Timestamp
FROM TABLE
WHERE DATEDIFF(MINUTE, sensor_Timestamp, GETDATE()) <= 10
答案 0 :(得分:1)
如果要在查询运行之前的10分钟内显示值,则查询没问题。
如果您想要一些特定的 10分钟间隔,则需要指定(通过传递该日期和时间代替between
或使用SELECT sensor_Id ,sensor_Value ,sensor_Timestamp
FROM TABLE where sensor_Timestamp between '2016-10-10 15:50' and '2016-10-10 16:00'
开始和结束时间。
例如:
public void purchased(int qtyPurchased) throws SQLException {
// TODO: Update the ProductsDB table's quantity for this
// object's product code.
try (Connection conn = SimpleDataSource.getConnection()) {
try (PreparedStatement stat = conn.prepareStatement(
"UPDATE ProductsDB SET Quantity = ? WHERE Product_Code = ?")) {
stat.setInt(1, qtyPurchased);
stat.setString(2, productCode);
stat.execute();
}
}
}
答案 1 :(得分:0)
以下查询将为您提供所需的结果
SELECT sensor_Id, sensor_Value, CONVERT(VARCHAR(10), CONVERT(DATETIME, sensor_Timestamp, 0), 101) + CONVERT(VARCHAR(4),CONVERT(DATETIME, sensor_Timestamp, 0), 108) as sensorTimestamp
FROM TABLE
group by sensor_Id, sensor_Value, sensorTimestamp
我已经截断了分钟的第二个数字,将聚合函数放在上面。
答案 2 :(得分:0)
请尝试这个(我试图创建开始和结束日期变量,其中可分为10分钟)
declare @current_time datetime = getdate()
declare @total_minutes int = datediff(minute, 0, @current_time)
declare @add_subtract_minutes int = @total_minutes % 10
declare @start_date datetime = dateadd(minute, @total_minutes - @add_subtract_minutes, 0)
declare @end_date datetime = dateadd(minute, @total_minutes + (10 - @add_subtract_minutes), 0)
SELECT sensor_Id
,sensor_Value
,sensor_Timestamp
FROM TABLE
WHERE sensor_Timestamp between @start_date and @end_date
答案 3 :(得分:0)
这可能不是确切的解决方案,您可以尝试这样的事情:
Declare @startdatetime datetime= '2016-10-10 15:49:15.000'
Declare @executeForThisNumber int = 3 -- for 3 intervals of 10 mins after @startdatetime
While @executeForThisNumber > 0
begin
declare @cmd varchar(max) = 'Select * from table
Where sensor_TimeStamp > ''' + convert(varchar(40), DATEADD(mi,-10,@startdatetime), 121) + ''' AND sensor_TimeStamp <= ''' + convert(varchar(40), @startdatetime, 121) + ''''
EXEC (@cmd)
set @startdatetime = convert(varchar(40), DATEADD(mi,10,@startdatetime), 121)
set @executeForThisNumber = @executeForThisNumber - 1
end
答案 4 :(得分:0)
您在这里,这应该可以帮助您。您只需要格式化日期时间值即可截断最后一分钟的数字。
-- SET UP TEST DATA
IF OBJECT_ID('tempdb..#SensorData', 'U') IS NOT NULL DROP TABLE #SensorData;
CREATE TABLE #SensorData (sendor_ID int, sensor_Value int, sensor_Timestamp DATETIME);
INSERT #SensorData (sendor_ID, sensor_Value, sensor_Timestamp)
SELECT A.sendor_ID, A.sensor_Value, A.sensor_Timestamp
FROM (
SELECT * FROM #SensorData WHERE 1=2
UNION ALL SELECT 1, 0001, '06/18/2020 01:01'
UNION ALL SELECT 1, 0002, '06/18/2020 01:02'
UNION ALL SELECT 1, 0003, '06/18/2020 01:03'
UNION ALL SELECT 1, 0005, '06/18/2020 01:05'
UNION ALL SELECT 1, 0008, '06/18/2020 01:08'
UNION ALL SELECT 1, 1013, '06/18/2020 01:13'
UNION ALL SELECT 1, 1016, '06/18/2020 01:16'
UNION ALL SELECT 1, 2021, '06/18/2020 01:21'
UNION ALL SELECT 1, 2027, '06/18/2020 01:27'
UNION ALL SELECT 1, 3032, '06/18/2020 01:32'
UNION ALL SELECT 1, 3036, '06/18/2020 01:36'
UNION ALL SELECT 1, 4041, '06/18/2020 01:41'
UNION ALL SELECT 1, 4047, '06/18/2020 01:47'
UNION ALL SELECT 1, 5054, '06/18/2020 01:54'
UNION ALL SELECT 1, 5059, '06/18/2020 01:59'
UNION ALL SELECT 1, 20001, '06/18/2020 02:01'
UNION ALL SELECT 1, 20002, '06/18/2020 02:02'
) A
-- SIMPLY RETURN THE DATA AND CHANGE TIMES TO 10 MINUE INTERVALS
SELECT A.sendor_ID
,A.sensor_Value
,(CONVERT(char(15), A.sensor_Timestamp, 121)+'0')
FROM #SensorData A
-- AGGREGATE IN 10 MINUTE INTERVALS
SELECT A.sendor_ID
,COUNT(*) AS LogInstances
,SUM(A.sensor_Value) AS Total_sensor_Value
,(CONVERT(char(15), A.sensor_Timestamp, 121)+'0') AS sensor_Timestamp_10min_Intervals
FROM #SensorData A
GROUP BY A.sendor_ID
,(CONVERT(char(15), A.sensor_Timestamp, 121)+'0')
ORDER BY A.sendor_ID
,(CONVERT(char(15), A.sensor_Timestamp, 121)+'0')
答案 5 :(得分:-1)
DATEDIFF的结果是天数(https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff)
我认为你可以做类似的事情(未经测试):
rm -rf ~/.gradle/caches/