尝试在本季度为SSRS报告创建一个硬编码数据集。我有这个:
SELECT CASE @Quarter
WHEN 'Q1' THEN (
SELECT 'January' as months, '01' as month_number
UNION ALL
SELECT 'February','02'
UNION ALL
SELECT 'March', '03')
WHEN 'Q2' THEN (
SELECT 'April' as months, '04' as month_number
UNION ALL
SELECT 'May','05'
UNION ALL
SELECT 'June', '06')
WHEN 'Q3' THEN (
SELECT 'July' as months, '07' as month_number
UNION ALL
SELECT 'August','08'
UNION ALL
SELECT 'September', '09')
ELSE (
SELECT 'October' as months, '10' as month_number
UNION ALL
SELECT 'November','11'
UNION ALL
SELECT 'December', '12')
END
但是我收到了这个错误:
当选择列表中只能指定一个表达式时 子查询不是用EXISTS引入的。
以前我尝试过使用表变量:
DECLARE @Months TABLE (months VARCHAR(9), month_start VARCHAR(2));
IF @Quarter = 'Q1'
INSERT INTO @Months VALUES ('January','01'), ('February','02'), ('March','03')
ELSE IF @Quarter = 'Q2'
INSERT INTO @Months VALUES ('April','04'), ('May','05'), ('June','06')
ELSE IF @Quarter = 'Q3'
INSERT INTO @Months VALUES ('July','07'), ('August','08'), ('September','09')
ELSE
INSERT INTO @Months VALUES ('October','10'), ('November','11'), ('December','12')
Select * FROM @Months
但是我得到了错误:
不支持Declare SQL构造或语句。
有没有人对如何在SSRS数据集中使用它有任何想法?
答案 0 :(得分:2)
也许就是这样。
Declare @Quarter varchar(2) = 'Q1'
Select months,month_number From (Values
('01','January','Q1'),('02','February','Q1'),('03','March','Q1'),
('04','April','Q2'),('05','May','Q2'),('06','June','Q2'),
('07','July','Q3'),('08','August','Q3'),('09','September','Q3'),
('10','October','Q4'),('11','November','Q4'),('12','December','Q4')
) N(month_number,months,Qtr)
Where Qtr=@Quarter
返回
Months Month_Number
January 01
February 02
March 03
答案 1 :(得分:0)
尝试使用所有月份和季度创建CTE。
WITH MONTHS AS (
SELECT 'January' as months, '01' as month_number, 'Q1' [quarter]
UNION ALL
SELECT 'February','02', 'Q1'
UNION ALL
SELECT 'March', '03', 'Q1'
UNION ALL
SELECT 'April' as months, '04' as month_number, 'Q2'
UNION ALL
SELECT 'May','05', 'Q2'
UNION ALL
SELECT 'June', '06', 'Q2'
UNION ALL
SELECT 'July' as months, '07' as month_number, 'Q3'
UNION ALL
SELECT 'August','08', 'Q3'
UNION ALL
SELECT 'September', '09', 'Q3'
UNION ALL
SELECT 'October' as months, '10' as month_number, 'Q4'
UNION ALL
SELECT 'November','11', 'Q4'
UNION ALL
SELECT 'December', '12', 'Q4')
SELECT * FROM MONTHS
WHERE [quarter] = @Quarter
如果有帮助,请告诉我。