我有一个名为vwMemberPolicyInfo
的视图,我需要显示当年输入的所有记录。
我的查询是:
select
EffectiveDate AS [Month],
count(MemberID) AS Agents,
sum(ActualPremium) AS Premium
from
vwMemberPolicyInfo
Where
EffectiveDate > DATEADD(year, -1, GETDATE())
GROUP BY
EffectiveDate
但它显示效果并不正常:
2010-11-01 00:00:00.000 74 40644.00
2010-07-01 00:00:00.000 86 50418.00
2011-03-08 00:00:00.000 1 744.00
2011-04-08 00:00:00.000 1 0.00
2010-11-02 00:00:00.000 5 2676.00
2011-04-14 00:00:00.000 1 1185.00
2011-02-28 00:00:00.000 7 2988.00
2011-02-23 00:00:00.000 8 8518.00
2011-04-10 00:00:00.000 1 1332.00
2011-04-07 00:00:00.000 3 2544.00
我只需要当前年份条目即可显示..谢谢
答案 0 :(得分:7)
尝试where YEAR(EffectiveDate) = YEAR(GETDATE())
希望它有所帮助。
答案 1 :(得分:4)
SELECT MONTH(EffectiveDate) AS [Month],
count(MemberID) AS Agents,
sum(ActualPremium) AS Premium
FROM vwMemberPolicyInfo
WHERE EffectiveDate >= DATEADD(year, YEAR(GETDATE()) - 1, CAST('0001-01-01' AS DATE))
AND EffectiveDate < DATEADD(year, YEAR(GETDATE()), CAST('0001-01-01' AS DATE))
GROUP BY
MONTH(EffectiveDate)
答案 2 :(得分:1)
它工作正常 - 它正在做完全你告诉它的东西 - 它会让你回到今天和一年之间的所有行(例如2011年4月15日到2010年4月15日之间) )
你可能希望达到的目标是:
SELECT
EffectiveDate AS [Month],
COUNT(MemberID) AS Agents,
SUM(ActualPremium) AS Premium
FROM
dbo.vwMemberPolicyInfo
WHERE
YEAR(EffectiveDate) = 2011
GROUP BY
EffectiveDate
这将从2011年开始返回仅行。
答案 3 :(得分:0)
选择
EffectiveDate AS [月],
count(MemberID)AS代理商,
总和(ActualPremium)AS Premium
从
vwMemberPolicyInfo
哪里
dateadd(year,0,EffectiveDate)= DATEADD(年,0,GETDATE())
通过...分组
EFFECTIVEDATE
dateadd(year,0,EffectiveDate) = DATEADD(year, -1, GETDATE())
dateadd(year,-1,EffectiveDate) = DATEADD(year, -1, GETDATE())