Thank you.
The reason I'm doing this, is I'm trying to make the query more efficient. It times out when users try to run the report.
Select
count(*),
a.state,
case
when a.state = '' then 'UNKNOWN - MISSING STATE'
when a.state = s.state_code then s.description
else 'UNKNOWN - OTHER' end as state_descr,
sum(td.state_amount) as net_amount,
(select
sum(ttd.state_amount)
from
ALSReplicator.tb_ora_tran tt
JOIN ALSReplicator.TB_ORA_tran_detail ttd
ON ttd.tran_ref_id = tt.tran_ref_id
JOIN ALSReplicator.tb_ora_priv_def ppd
ON ttd.priv_code = ppd.priv_code and ttd.license_year = ppd.license_year
where
tt.process_date >= @begin_date and
tt.process_date < dateadd(dd, 1, @end_date) and
ttd.priv_code in (@priv_code) and
tt.tran_status = 'C' and
ttd.tran_status in ('I','C') and
tt.test_tran = 'N' and
ttd.replace_ind = 'N' and
ttd.tran_detail_id not in
(Select distinct purchase_tran_detail_id
From ALSReplicator.tb_ora_voucher_detail) and
ttd.tran_detail_id not in
(Select distinct redeem_tran_detail_id
From ALSReplicator.tb_ora_voucher_detail
Where redeem_tran_detail_id is not NULL) and
tt.location_id not in ('4811584', '4811287') and
tt.location_id not in (@test_lid)
) as total_amount
From
ALSReplicator.TB_ORA_tran t
JOIN ALSReplicator.TB_ORA_tran_detail td
ON t.tran_ref_id = td.tran_ref_id
JOIN ALSReplicator.TB_ORA_priv_def p
ON td.priv_code = p.priv_code and td.license_year = p.license_year
JOIN ALSReplicator.TB_ORA_cust c
ON t.cust_id = c.cust_id
LEFT JOIN ALSReplicator.TB_ORA_address a
ON c.aid = a.aid
LEFT JOIN ALSReplicator.state s
ON a.state = s.state_code
Where
t.process_date >= @begin_date and
t.process_date < dateadd(dd, 1, @end_date) and
td.priv_code in (@priv_code) and
t.tran_status = 'C' and
td.tran_status in ('I','C') and
t.test_tran = 'N' and
td.replace_ind = 'N' and
td.tran_detail_id not in
(Select distinct purchase_tran_detail_id
From ALSReplicator.tb_ora_voucher_detail) and
td.tran_detail_id not in
(Select distinct redeem_tran_detail_id
From ALSReplicator.tb_ora_voucher_detail
Where redeem_tran_detail_id is not NULL) and
t.location_id not in ('4811584', '4811287') and
t.location_id not in (@test_lid)
Group By s.description, a.state, s.state_code
It's basically running the query twice. Once to get the sum total of one column. The second to get the individual resutls.
@test_lid and @priv_code are the multi value params. @test_lid comes from another query attached to the report, and the results are static. @priv_code is chosen by the user of the report. (Another query in the report populates the list and let's users choose the codes they want to search for.)
I've been adding a couple Indexes to try to speed things up, but what else can I do in this report to make it run faster.