sql server - SQL row to column using nested Case statement -
i trying nested case statement work in stages working have nested case statement complains syntax. have looked @ many examples , can't see have gone wrong.
what wish achieve row per policytransactionid (set in clause) each account code column. worked me, turns error adding nested entry @ debit/credit entry , turning number negative appropriate.
attempt far attached.
http://sqlfiddle.com/#!6/8db47/3
create table [dbo].[policytransactionsplits]( [policytransactionsplitid] [int] not null, [policytransactionid] [int] not null, [accountcode] [int] not null, [accountdesc] [nvarchar](max) null, [transactiontype] [nvarchar](max) null, [amount] [decimal](18, 2) not null, [adjusted] [bit] not null default ((0)) ) ; insert policytransactionsplits ([policytransactionsplitid], [policytransactionid], [accountcode], [accountdesc], [transactiontype], [amount], [adjusted]) values (1551,1096,1000,'total transaction premium','debit',50,0), (1552,1096,1010,'total net premium','debit',50,0) ;
...
select max(case when accountcode = 1000 case when transactiontype = 'debit' amount end else case when accountcode = 1000 case when transactiontype = 'credit' amount*-1 end) [total transaction premium] max(case when accountcode = 1000 case when transactiontype = 'debit' amount end else case when accountcode = 1000 case when transactiontype = 'credit' amount*-1 end) [total transaction premium] policytransactionsplits policytransactionid = 10
you don't need nest case
statements:
select max(case when accountcode = 1000 , transactiontype = 'debit' amount when accountcode = 1000 , transactiontype = 'credit' amount * -1 end) [total transaction premium], max(case when accountcode = 1000 , transactiontype = 'debit' amount when accountcode = 1000 , transactiontype = 'credit' amount * -1 end) [total transaction premium] policytransactionsplits policytransactionid = 10;
it unclear me why repeating same logic. perhaps want this:
select max(case when transactiontype = 'debit' amount when transactiontype = 'credit' amount * -1 end) [total transaction premium] policytransactionsplits policytransactionid = 10 , accountcode = 1000;
Comments
Post a Comment