我想创建每小时报告,此查询运行正常。
SELECT
FORMAT(PingLogDate,'yyyy-MM-dd-HH:00:00') AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM
tbl_PingLog
GROUP BY
FORMAT(PingLogDate,'yyyy-MM-dd-HH:00:00')
ORDER BY
FORMAT(PingLogDate,'yyyy-MM-dd-HH:00:00')
但是我支持SQL Server 2008而Format
是SQL Server 2012的新功能,所以我尝试了这段代码
SELECT
CAST(PingLogDate AS date) AS CalculatedDate,
DATEPART(hour, PingLogDate) AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM
tbl_PingLog
GROUP BY
CAST(PingLogDate AS date), DATEPART(hour, PingLogDate)
ORDER BY
CAST(PingLogDate AS date), DATEPART(hour, PingLogDate)
问题是不希望2列用于日期和小时,例如
第一个查询的结果是
CalculatedTime | PingCount
--------------------+-----------
2017-11-07-12:00:00 | 359
2017-11-07-13:00:00 | 359
2017-11-07-14:00:00 | 350
第二个查询的结果是
CalculatedDate |CalculatedTime | PingCount
---------------+---------------+----------
2017-11-07 |12 | 359
2017-11-07 |13 | 359
2017-11-07 |14 | 350
我不想要第二个。我想要第一个。如何使用SQL Server 2008支持的工具。
答案 0 :(得分:0)
您可以使用CONVERT将datetime
列转换为yyyy-mm-dd hh:mi:ss(24h)
格式,然后将该空格替换为-
。
这样的事情:
SELECT REPLACE(CONVERT(VARCHAR(32), PingLogDate, 120), ' ', '-') AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM tbl_PingLog
GROUP BY REPLACE(CONVERT(VARCHAR(32), PingLogDate, 120), ' ', '-')
ORDER BY REPLACE(CONVERT(VARCHAR(32), PingLogDate, 120), ' ', '-')
答案 1 :(得分:0)
尝试使用转换和连接小时的方法。 这将为您提供与SQL Server 2012中使用 FORMAT 方法相同的每小时和日期时间格式的ping计数:
var arr = [ "Kzljszzz15OFcln1XGM" ,
{
make: "John Deere ",
model: "5055",
uid: "Kzljszzz15OFcln1XGM",
year: "1953"
}
];
for(var prop in arr) {
var cheackObjProp = (arr[prop] === "Kzljszzz15OFcln1XGM") ? true : false;
var obj = {};
//-------first index of arr----------
if(cheackObjProp) {
var objprop = arr[prop];
//------second index of arr-----------
}else {
obj[objprop] = arr[prop];
}
};
console.log(obj);
答案 2 :(得分:0)
其他解决方案的简化版
SELECT
replace(convert(varchar(13),PingLogDate,120),' ','-')+':00:00' AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM
tbl_PingLog
GROUP BY
convert(varchar(13),PingLogDate,120)
ORDER BY CalculatedTime
答案 3 :(得分:0)
在代码中使用这个简单的语句怎么样?
declare @date datetime = getdate()
select DATEADD(dd, DATEDIFF(dd, 0, @date), 0)
结果将类似于2017-11-25 00:00:00.000
这是从datetime
字段