简体   繁体   中英

how to display totals from two different queries at bottom of same column in SSRS report?

Is it possible (without c# code) to pull in a second TOTAL row from a different query, and have it appear under another total from another query, in the same SSRS report? How to do with the following example?

example:

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);

--first query shows how many leads were sent to each property and totals it at the bottom: 12 leads total.

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

select
count(distinct lead_id)
from LeadTracker;

--second query shows that only a total of 4 were unique leads. I need this 'unique lead' total to appear beneath the total leads.

select
count(distinct lead_id)
from LeadTracker;

---in SSRS I need the results combined into a single column, like this

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;

PLUS the result of this column underneath it

 Select count(distinct lead_id) 'TOTAL UNIQUE leads only'
 from LeadTracker;
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

Add a row under your total row and use =first(Fields!Unique.Value) in the report, otherwise ignore it.

If you need to bring it under another dataset, you can use the LOOKUP as

Lookup(source_expression, destination_expression, result_expression, dataset)

This <<Expr>> would look something like:

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

MSDN Page for LOOKUP

Maybe you're looking for a GROUP BY WITH ROLLUP ? Then you can do this all in one query:

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

See this SQLFiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM