Search code examples
jquerysqlreporting-servicesdatasetreport

How to return a single row with a total using a sql query and or expressions in sql server


I have a query that I have written to return a total of all payments. What is happening is that when I run my report it is returnng multiple rows instead of one row and I think this is because of the groupby clause I have in my query. The thing is it seems I need to have the group by because some of the fields are included in my dataset for example I actually filter using the transaction date so I cannot ommit it. This is my query below

select distinct Transaction_Amount, Transaction_Type, Transaction_Date  --'R '+ CONVERT(varchar,SUM(Transaction_Amount)) as [Epurse Credits/Deposits]
from Transactional_Account
where Transaction_Type like '%credit%'

Even when I place my expression as =SUM(Fields!Transaction_Amount.Value,"DataSet1") the result set still contains multiple rows


Solution

  • If you want one row per transaction, then remove the DISTINCT and use aggregation functions:

    select max(Transaction_Amount), max(Transaction_Type), max(Transaction_Date)
          sum(Transaction_Amount) as [Epurse Credits/Deposits]
    from Transactional_Account
    where Transaction_Type like '%credit%';
    

    This returns arbitrary (well, the maximum) values for each of the other columns. But you are guaranteed one row.