难以在图表上显示大量值
Difficulty in displaying large number of values on a Chart
我正在尝试在图表上显示数据库中的一些记录。一分钟内保存的记录一般为70条左右。如此多的记录在折线图上看起来很疯狂,但当用户对数据感兴趣的时间间隔为四小时时,期望用户继续请求每分钟查看记录会更加疯狂和不现实。
我决定按分钟对记录进行分组,结果卡在了查询中。
以下是我的查询示例:
SELECT DISNTINCT YEAR(recievedon), MONTH(recievedon), DAY(recievedon), HOUR(recievedon), referenceprice
FROM dbname
WHERE recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
ORDER BY recievedon
如何使用 GROUP BY
或 DISTINCT
按分钟对记录进行分组
其次,有没有更好的方法在(折线图)图表中呈现大型数据集而不是对记录进行分组?
如果我猜到你在使用 MySQL,那么你可以使用 to_seconds()
。下面给出每分钟的平均参考价,以及区间内第一个价格的date/time:
select min(recievedon), avg(referenceprice)
from dbname
where recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
group by floor(to_seconds(receivedon) / 60)
编辑:
在SQL服务器中,您可以:
select min(receivedon), avg(referenceprice)
from dbname
where recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
group by datediff(minute, 0, receivedon);
如果您想要周期的开始而不是更早的时间戳:
select dateadd(minute, 0, datediff(minute, 0, receivedon)) as timeperiod,
avg(referenceprice)
from dbname
where recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
group by dateadd(minute, 0, datediff(minute, 0, receivedon)) ;
使用简单的 group by DATEPART
而不用担心溢出。 SELECT
与 Gordon 的回答完全相同。
SELECT
MIN(ReceivedOn) AS Time,
-- Assuming you need average price
AVG(ReferencePrice) AS ReferencePrice
FROM
dbname
WHERE
ReceivedOn BETWEEN '2015-06-05 10:30' AND '2015-06-05 10:50'
GROUP BY
DATEPART(YEAR, ReceivedOn),
DATEPART(MONTH, ReceivedOn),
DATEPART(DAY, ReceivedOn),
DATEPART(HOUR, ReceivedOn),
DATEPART(MINUTE, ReceivedOn)
我正在尝试在图表上显示数据库中的一些记录。一分钟内保存的记录一般为70条左右。如此多的记录在折线图上看起来很疯狂,但当用户对数据感兴趣的时间间隔为四小时时,期望用户继续请求每分钟查看记录会更加疯狂和不现实。
我决定按分钟对记录进行分组,结果卡在了查询中。 以下是我的查询示例:
SELECT DISNTINCT YEAR(recievedon), MONTH(recievedon), DAY(recievedon), HOUR(recievedon), referenceprice
FROM dbname
WHERE recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
ORDER BY recievedon
如何使用 GROUP BY
或 DISTINCT
其次,有没有更好的方法在(折线图)图表中呈现大型数据集而不是对记录进行分组?
如果我猜到你在使用 MySQL,那么你可以使用 to_seconds()
。下面给出每分钟的平均参考价,以及区间内第一个价格的date/time:
select min(recievedon), avg(referenceprice)
from dbname
where recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
group by floor(to_seconds(receivedon) / 60)
编辑:
在SQL服务器中,您可以:
select min(receivedon), avg(referenceprice)
from dbname
where recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
group by datediff(minute, 0, receivedon);
如果您想要周期的开始而不是更早的时间戳:
select dateadd(minute, 0, datediff(minute, 0, receivedon)) as timeperiod,
avg(referenceprice)
from dbname
where recievedon >= '2015-06-05 10:30' AND recievedon <= '2015-06-05 10:50'
group by dateadd(minute, 0, datediff(minute, 0, receivedon)) ;
使用简单的 group by DATEPART
而不用担心溢出。 SELECT
与 Gordon 的回答完全相同。
SELECT
MIN(ReceivedOn) AS Time,
-- Assuming you need average price
AVG(ReferencePrice) AS ReferencePrice
FROM
dbname
WHERE
ReceivedOn BETWEEN '2015-06-05 10:30' AND '2015-06-05 10:50'
GROUP BY
DATEPART(YEAR, ReceivedOn),
DATEPART(MONTH, ReceivedOn),
DATEPART(DAY, ReceivedOn),
DATEPART(HOUR, ReceivedOn),
DATEPART(MINUTE, ReceivedOn)