如何在SSRS报告中显示同一列底部两个不同查询的总计?

时间:2014-08-12 12:08:24

标签: sql-server reporting-services

是否有可能(没有c#代码)从另一个查询中提取第二个TOTAL行,并在相同的SSRS报告中将其显示在另一个查询的另一个总计中?如何处理以下示例?

示例:

create table LeadTracker
(property_name varchar(30), lead_id int);


insert into LeadTracker values
('Property1', 18709),
('Property1', 18323),
('Property1', 19547),
('Property2', 18709),
('Property2', 19015),
('Property2', 18323),
('Property2', 19547),
('Property3', 19015),
('Property3', 18323),
('Property3', 19547),
('Property4', 19015),
('Property4', 19547);

- 第一个查询显示向每个媒体发送了多少潜在客户,并在底部汇总了所有潜在客户:总共12个潜在客户。

select 
property_name,
count(distinct lead_id)
from leadtracker
group by property_name;

select
count(distinct lead_id)
from LeadTracker;

- 第二个查询显示只有4个唯一潜在客户。我需要这个“独特的领先”总计出现在总线之下。

select
count(distinct lead_id)
from LeadTracker;

---在SSRS中,我需要将结果合并为一个列,如此

select 
property_name,
count(distinct lead_id) as leads_sent
from leadtracker
group by property_name
union all
Select property_name = 'Total', leads_sent = 12;

将此列的结果添加到其下面

 Select count(distinct lead_id) 'TOTAL UNIQUE leads only'
 from LeadTracker;

2 个答案:

答案 0 :(得分:0)

DECLARE @Unique as int = (Select count(distinct lead_id) from LeadTracker);

select property_name
    ,count(distinct lead_id) as leads_sent
    ,@Unique [Unique]
from leadtracker
group by property_name

在总行下添加一行,并在报告中使用=first(Fields!Unique.Value),否则请忽略它。

如果您需要将其置于其他数据集下,则可以使用LOOKUP作为

Lookup(source_expression, destination_expression, result_expression, dataset)

<<Expr>>类似于:

 =Lookup(Fields!property_name.Value, Fields!property_name.Value, Fields!Unique.Value, "Unique_DataSet_Name")

MSDN Page for LOOKUP

答案 1 :(得分:0)

也许您正在寻找GROUP BY WITH ROLLUP?然后,您可以在一个查询中执行此操作:

SELECT 
  CASE WHEN GROUPING_ID(property_name)=0
    THEN property_name
    ELSE 'Total Unique'
  END AS leads_sent
 ,COUNT(DISTINCT lead_id)
FROM leadtracker
GROUP BY property_name WITH ROLLUP

请参阅此SQLFiddle