我在应用case时遇到问题..当在sql的where子句中时。当我在where子句中将值放在0和4之间时,我得到了预期的结果,但是当应用case时,它就失败了。
表名:myTbl
data | amount | date | ReceiptFrequency A 2000 02-03-1988 Annual A 2000 02-03-1990 Semiannual A 2000 02-03-1991 Annual A 2000 02-03-1992 Annual
create TABLE #Table ( num int NOT NULL, );
;WITH Nbrs ( n ) AS (
SELECT 1 UNION ALL
SELECT 1 + n FROM Nbrs WHERE n < 60 )
INSERT #Table(num)
SELECT n FROM Nbrs
OPTION ( MAXRECURSION 60 )
SELECT *FROM #Table AS x,#Table AS y,myTbl AS d
WHERE (d.ReceiptFrequency<>'Random') AND(x.num BETWEEN 0 AND 1(CASE WHEN d.ReceiptFrequency='Annual' THEN 1 WHEN d.ReceiptFrequency='Quarter' THEN 1 WHEN d.ReceiptFrequency='Month' THEN 12 WHEN d.ReceiptFrequency='SemiAnnual' THEN 2 ELSE 1 END -1))
答案 0 :(得分:1)
为什么 -1 在CASE..WHEN结尾?
CASE WHEN d.ReceiptFrequency='Annual' THEN 1 WHEN d.ReceiptFrequency='Quarter' THEN 1 WHEN d.ReceiptFrequency='Month' THEN 12 WHEN d.ReceiptFrequency='SemiAnnual' THEN 2 ELSE 1 END -1))