我對數據庫不熟悉。爲什麼我在SQL Server查詢中使用「?」之間的「關鍵字附近的語法錯誤」?
我正在使用Microsoft SQL Server,並且在嘗試執行此查詢時使用鍵盤之間的選擇Date範圍的問題。
我的查詢是:
select NumeroPolizza ,sum(v.Ctv) as Ctv_RI
from (
select r.NumeroPolizza,SUM(r.ImportoPrestazioneIniziale) as Ctv
from Prestazione r with(nolock)
where r.NumeroPolizza in (select ID from Polizza p with(nolock) where TipoSistemaProvenienzaID=8)
--and r.DataInizio <= '2015-12-31'
and between '2016-01-01' and '2016-04-01'
group by r.NumeroPolizza
UNION
select NumeroPolizza,SUM(ImportoRivalutazioneDaPiano+ImportoRivalutazioneEstemporaneo)as Ctv
from Rivalutazione with(nolock)
where NumeroPolizza in (select ID from Polizza p with(nolock) where TipoSistemaProvenienzaID=8)
--and DAtaDecorrenza <= '2015-12-31'
and between '2016-01-01' and '2016-04-01'
group by NumeroPolizza
) v
group by NumeroPolizza
order by NumeroPolizza
正如你可以看到我使用2 之間作爲過濾器2 有條件的地方,這樣的事情:
and between '2016-01-01' and '2016-04-01'
的問題是, SQL Server給我以下錯誤信息:
11:30:36 [SELECT - 0 row(s), 0.000 secs] [Error Code: 156, SQL State: S0001] Incorrect syntax near the keyword 'between'.
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec [0 successful, 0 warnings, 1 errors]
我錯過了什麼?我該如何解決這個問題?