简体   繁体   中英

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

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.

There is no need to include your total in your dataset query. Just add a row group to your table/matrix in the report design (or change the existing 'details' row group), and group by all the common fields that you need to show.

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