Friday, January 31, 2014

How to calculate totals based on year (date) and comparing othe columns in the same table please

Hello Good Evening,




Could you please help me here


how to write condition for self table year records, such 2012 name and acctno match with 2013 name and acctno then total, provided below,





create table #tab1 (MasterKey int, AcctNo varchar(12),name varchar(25), SumaofShares numeric, request_dat datetime )
--drop table #tab1

insert into #tab1 values (1000, 100,'Tom', 2500, '10/01/2012')
insert into #tab1 values (1001, 101,'Bat', 1550, '08/11/2012')
insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
insert into #tab1 values (1003, 103,'Vat', 1750, '04/15/2012')

insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
insert into #tab1 values (1011, 105,'Tim',500, '06/18/2013')
insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
insert into #tab1 values (1015, 103,'Vat',150, '03/18/2013')
insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
insert into #tab1 values (9999, 111,'AAA',110, '12/15/2014')


create table #tab2 (IssueKey int, totalOutstanding numeric, sharedBenefits varchar(1) )
--drop table #tab2

insert into #tab1 values (1000, 500, 'V')
insert into #tab1 values (1001, 150, 'U')
insert into #tab1 values (1002, 100, 'N')
insert into #tab1 values (1003, 170, 'U')
insert into #tab1 values (1010, 100, 'U')
insert into #tab1 values (1011, 200, 'K')
insert into #tab1 values (1012, 340, 'U')
insert into #tab1 values (1013, 560, 'N')
insert into #tab1 values (1014, 280, 'V')
insert into #tab1 values (1015, 150, 'V')
insert into #tab1 values (1016, 840, 'V')
insert into #tab1 values (1017, 530, 'N')

i would like to get 4 columns output

how to get sumofshares (#tab1) and TotalOutStanding(#tab2) summ up with these values please.,

MasterKey (#tab1) and IssueKey (#tab2) are like primary key and foreign key

so the request is

need to calculate, sumofshares (#tab1) and TotalOutStanding(#tab2) as below

1)ShareBenefist = U and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
then '2012 and 2013 accts UN Veriverted'
2)ShareBenefist = V and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
then '2012 and 2013 accts Veriverted'
3)ShareBenefist = N and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
then '2012 and 2013 accts NONVERT'
4)year( request_dat) =2102 and Name and Acctno not match with 2013 account name and acctno (#tab1)
then '2012 last year accounts'
5)year( request_dat) = 2013 and Name and Acctno not match with 2013 account name and acctno (#tab1)
then '2012 This year accounts'


for ex 1) the below accounts in #tab1 has both 2012 and 2013 and acctno same in both years and name is same in both years so it is condired as

insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')

for ex 2)
insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')


for ex 4) 2012 records there is not match acctno and name in 2013 recods
insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')

for ex 5) 2013 records there is no match of name and acct no with 2012 records
insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')


Expected Results (just for format)
AcctTypeDescription, SumofShares, OtotalutStand
'2012 and 2013 accts UN Veriverted',2700,234
'2012 and 2013 accts Veriverted' ,2890,234
'2012 and 2013 accts NONVERT' ,4533,325
'2012 last year accounts' ,2334,567
'2012 This year accounts' ,2222,877


Please


Thank youy in advance

asita



No comments:

Post a Comment