无法在报表生成器中选择多个值
Posted
技术标签:
【中文标题】无法在报表生成器中选择多个值【英文标题】:Cant choose multiple values in Report Builder 【发布时间】:2022-01-23 19:13:16 【问题描述】:我在下面的报告构建器中犯了一个逻辑错误,有 2 个连接到@Income 参数的 Sequel 查询,但是在报告中我得到了这个结果,这没有意义,因为它只显示相同的收入。
我认为这是因为我无法选择多种收入。当我尝试选择多个值时出现此错误。
这个查询可以执行,但效率不高。我也遇到了同样的年龄查询,我无法选择多个年龄,如您在错误图片中看到的那样。
(SELECT MIN(YearlyIncome) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) as MinIncome,
(SELECT MAX(YearlyIncome) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) as MaxIncome
这是我的存储过程 (DataSet1)
CREATE PROC GET_TargetCustomer (
@Age INT,
@Income int,
@Buyer int
) as
BEGIN
SELECT DISTINCT
(SELECT AVG(Age) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) AS AVGAge,
(SELECT SUM(BikeBuyer) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) as TotalBuyers,
(SELECT MAX(Age) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) AS OldestCustomer,
(SELECT AVG(YearlyIncome) FROM vTargetCustomer WHERE(@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) as AVGIncome,
(SELECT MIN(Age) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) AS YoungestCustomer,
(SELECT MIN(YearlyIncome) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) as MinIncome,
(SELECT MAX(YearlyIncome) FROM vTargetCustomer WHERE (@Age = Age) AND (@Income = YearlyIncome) AND (@Buyer = BikeBuyer)) as MaxIncome
END
GO
我已经为每个参数创建了数据集
年龄参数的AgeDataSet
SELECT DISTINCT Age FROM vTargetCustomer ORDER BY Age ASC
IncomeDS 收入参数
SELECT DISTINCT YearlyIncome FROM vTargetCustomer ORDER BY YearlyIncome ASC
买方参数的BuyerDS
SELECT DISTINCT BikeBuyer FROM vTargetCustomer ORDER BY BikeBuyer ASC
谁能帮我解决这个问题?
【问题讨论】:
【参考方案1】:如果您在参数中使用多个值,则查询需要能够使用 IN 而不是等号 (=) 来使用它们。
您没有将它们全部组合在一个 SELECT 中是有原因的吗?
SELECT AVG(Age) AVGAge, SUM(BikeBuyer) TotalBuyers, MAX(Age), MIN(Age)
FROM vTargetCustomer
WHERE (Age = @Age)
AND (Income IN (@YearlyIncome))
AND (BikeBuyer = @Buyer)
【讨论】:
@HamzaB Hannover 的答案是正确的,但您必须直接在数据集查询中使用它,而不是在存储的过程中。 @AlanSchofield 我需要在我的存储过程中使用。但是如果不删除我在 proc 中的上述代码,如果我使用 Hannovers 回答我的数据集,你怎么看? 如果你真的必须使用存储过程,那么你不能直接使用IN
。您将不得不解析存储过程中的参数。为什么不能在数据集查询中直接使用查询?以上是关于无法在报表生成器中选择多个值的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server 报表生成器:根据子报表中的值隐藏 Tablix 行
无法在 s-s-rS 和 SQL Server 报表生成器中运行或预览报表