Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

adding a double transaction row to group.

$
0
0
Hi. i have a transaction 29801 that will have 2 rows, L and E.
L= type of transaction and E will only be the extra line if the transaction is canceled ,
So if a transaction is NOT cancelled it will have only one row (L) but if the transaction is cancelled it will have 2 rows(L and E).
I am trying to create a query that will sum transactions and will also display the E (negative number of the transaction) in another column.
So If i do
select * from tblTrans_Cash where transC_lgnnumber = 29801
the i will get 2 lines L and E , since this is a cancelled transaction.
I would like to incorporate that into a sum query i have:
Code:

declare @datefrom datetime,@dateto Datetime,@Cinema AS VARCHAR(2),@CinemaDescription VARCHAR(50),@CardStrType varchar(10)
set @datefrom ='20141101'
set @dateto = '20141126'
set @Cinema = 03
set @CinemaDescription = NULL
set @CardStrType = 'L'

SELECT       
--transc_curvalue,
--transc_strsummarisedflag,
@DateFrom,
                @DateTo,
                convert(datetime,convert(char(10),TransC_dtmRealTransTime,101))        AS DateRecord,
                Count(*) AS TotalTransactions,
                SUM(CASE WHEN Transc_curValue>=0 THEN 1 ELSE 0 END) AS TotalPayments ,
                SUM(CASE WHEN Transc_curValue>=0 THEN Transc_curValue ELSE 0 END) AS PaymentValue ,
                SUM(CASE WHEN Transc_curValue<0 THEN 1 ELSE 0 END) AS TotalRefunds ,
                SUM(CASE WHEN Transc_curValue<0 THEN Transc_curValue ELSE 0 END) AS RefundValue,
                ISNULL(@Cinema,C.Cinema_strCode) AS Cinema  ,
                ISNULL(@CinemaDescription,C.Cinema_strName)
FROM        Vista.dbo.tblTrans_Cash
--inner join tbltrans_ticket
--ON tblTrans_Cash.TransC_lgnNumber = tblTrans_Ticket.TransT_lgnNumber
                CROSS JOIN tblCinema C
WHERE        --TransC_strType= @CardStrType and
TransC_strType= @CardStrType
and
(TransC_strType= 'E' or TransC_strType= @CardStrType)
                AND TransC_dtmRealTransTime BETWEEN @DateFrom AND @DateTo
--and transC_lgnnumber = 2980
GROUP BY
                convert(datetime,convert(char(10),TransC_dtmRealTransTime,101)),
                ISNULL(@Cinema,C.Cinema_strCode),
                ISNULL(@CinemaDescription,C.Cinema_strName)

--,transc_curvalue
--,transc_strsummarisedflag
ORDER BY convert(datetime,convert(char(10),TransC_dtmRealTransTime,101))

my problem here is that if i do
where TransC_strType= @CardStrType or TransC_strType= 'E'
It will display transactions that may or may not have 'L' so i will get extra sums.
If i do TransC_strType= @CardStrType and TransC_strType= 'E' it will only display transactions that have both 'L' and 'E'
What i would like to do is see if a transaction is 'L' and if so then if it has 'E' then add it to the sum.
So any ideas? A Case maybe in the where clause?
Thanks.

Edit: I forgot that a transaction transC_lgnnumber is the same on both columns that will come up if it's L and E.

Viewing all articles
Browse latest Browse all 2918

Trending Articles