2009-11-17 53 views
1

我在編寫查詢時遇到問題。目前我可以檢索每臺機器每小時百分比的記錄,並且我有一張表格,每天顯示每臺機器每小時記錄的每個百分比。代碼如下所示:SQL Server中的查詢問題

....... 
WHERE  (tblCheckResult.DateTime >= @DateFrom) 
AND (tblCheckResult.DateTime <= DateTo) AND (tblCheck.CheckID = 69) 
....... 

CheckID是百分比的ID。我想要做的是顯示平均每月錄音和每年錄音1月80%,2月95%等任何人都可以請指教?

感謝

回答

4

您可以使用日期分量來檢索月份部分/年部分和組那些。

像這樣的事情

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

我認爲你是不正確輸入SQL查詢。爲了避免這個問題,你應該寫

insert into table <table_name> select <value>; 

如果你是在執行查詢遇到問題,那麼你應該對SQL查詢網上查詢。如果問題依然存在,那麼SQL數據庫中可能會有損壞。在這種情況下,您需要使用stellar SQL recovery應用程序來解決這個問題。