我在编写查询时遇到问题。目前我可以检索每台机器运行百分比的每小时记录,并且我有一张表格,每天显示每台机器每小时记录的每个百分比。代码如下所示:
.......
WHERE (tblCheckResult.DateTime >= @DateFrom)
AND (tblCheckResult.DateTime <= DateTo) AND (tblCheck.CheckID = 69)
.......
CheckID是百分比的ID。我想要做的是显示平均每月录音和每年录音,例如1月80%,2月95%等。任何人都可以建议吗?
由于
答案 0 :(得分:4)
您可以使用DatePart检索月份/年份部分和组。
像这样的东西
DECLARE @Table TABLE(
DVal DATETIME,
Val FLOAT
)
INSERT INTO @Table (DVal,Val) SELECT '01 Jan 2008', 10
INSERT INTO @Table (DVal,Val) SELECT '02 Jan 2008', 20
INSERT INTO @Table (DVal,Val) SELECT '03 Jan 2008', 30
INSERT INTO @Table (DVal,Val) SELECT '04 Jan 2008', 40
INSERT INTO @Table (DVal,Val) SELECT '05 Jan 2008', 50
INSERT INTO @Table (DVal,Val) SELECT '06 Jan 2008', 60
INSERT INTO @Table (DVal,Val) SELECT '01 Feb 2008', 11
INSERT INTO @Table (DVal,Val) SELECT '02 Feb 2008', 22
INSERT INTO @Table (DVal,Val) SELECT '03 Feb 2008', 33
INSERT INTO @Table (DVal,Val) SELECT '04 Feb 2008', 44
INSERT INTO @Table (DVal,Val) SELECT '05 Feb 2008', 55
INSERT INTO @Table (DVal,Val) SELECT '06 Feb 2008', 66
INSERT INTO @Table (DVal,Val) SELECT '01 Jan 2009', 16
INSERT INTO @Table (DVal,Val) SELECT '02 Jan 2009', 26
INSERT INTO @Table (DVal,Val) SELECT '03 Jan 2009', 36
INSERT INTO @Table (DVal,Val) SELECT '04 Jan 2009', 46
INSERT INTO @Table (DVal,Val) SELECT '05 Jan 2009', 56
INSERT INTO @Table (DVal,Val) SELECT '06 Jan 2009', 66
INSERT INTO @Table (DVal,Val) SELECT '01 Feb 2009', 17
INSERT INTO @Table (DVal,Val) SELECT '02 Feb 2009', 27
INSERT INTO @Table (DVal,Val) SELECT '03 Feb 2009', 37
INSERT INTO @Table (DVal,Val) SELECT '04 Feb 2009', 47
INSERT INTO @Table (DVal,Val) SELECT '05 Feb 2009', 57
INSERT INTO @Table (DVal,Val) SELECT '06 Feb 2009', 67
SELECT DATEPART(yy, DVal) YearPart,
DATEPART(MM, DVal) MonthPart,
AVG(Val)
FROM @Table
GROUP BY DATEPART(yy, DVal),
DATEPART(MM, DVal)
SELECT DATEPART(yy, DVal) YearPart,
AVG(Val)
FROM @Table
GROUP BY DATEPART(yy, DVal)
答案 1 :(得分:1)
我认为您输入的SQL查询不正确。为了避免这个问题,你应该写
insert into table <table_name> select <value>;
如果您在执行查询时遇到问题,那么您应该在线检查SQL查询。如果问题仍然存在,那么SQL数据库可能会有损坏。