Hi! FriendI wrote this queryselect phone ,sum(borclar) as total ,sum([M?bl?g< 1 ay]) [M?bl?g< 1 ay],sum([M?bl?g< 2 ay]) [M?bl?g< 2 ay],sum([M?bl?g< 3 ay]) [M?bl?g< 3 ay],sum([M?bl?g< 4 ay]) [M?bl?g< 4 ay],sum([M?bl?g< 5 ay]) [M?bl?g< 5 ay],sum([M?bl?g> 5 ay]) [M?blg> 5 ay] from (select phone,sum(borclar) as borclar,sum([qacqin])as [qacqin],sum([umumi]) [umumi],sum([M?bl?g< 1 ay]) [M?bl?g< 1 ay],sum([M?bl?g< 2 ay]) [M?bl?g< 2 ay],sum([M?bl?g< 3 ay])[M?bl?g< 3 ay],sum([M?bl?g< 4 ay])[M?bl?g< 4 ay],sum([M?bl?g< 5 ay])[M?bl?g< 5 ay],sum([M?bl?g> 5 ay])[M?bl?g> 5 ay]from(select phone,borclar,[qacqin],[umumi], [1 ay ],[2 ay ],[3 ay ],[4 ay ],[5 ay ],[M?bl?g< 1 ay]=case when -(borclar) <=[1 ay ] then borclar else 0 end,[M?bl?g< 2 ay]=case when -(borclar) >[1 ay ] and -(borclar) <=[2 ay ] then borclar else 0 end,[M?bl?g< 3 ay]=case when -(borclar) >[2 ay ] and -(borclar) <=[3 ay ] then borclar else 0 end,[M?bl?g< 4 ay]=case when -(borclar) >[3 ay ] and -(borclar) <=[4 ay ] then borclar else 0 end,[M?bl?g< 5 ay]=case when -(borclar) >[4 ay ] and -(borclar) <=[5 ay ]then borclar else 0 end,[M?bl?g> 5 ay]=case when -(borclar) >[5 ay ]then borclar else 0 end from(select phone,borclar,ayliqlar as [1 ay ], ayliqlar*2 as [2 ay ], ayliqlar*3 as [3 ay ], ayliqlar*4 as [4 ay ], ayliqlar*5 as [5 ay ],[qacqin],[umumi] from(select phone,Abune+cdma_borc as borclar,cdma_ayliq+ay_abune as ayliqlar,[qacqin],[umumi] from (selectphone,Abune,cdma_borc,cdma_ayliq=case when phone<3999999 then 0 else cdma_ayliq end ,ay_abune=case when phone>3999999 then 0 else ay_abune end,[qacqin],[umumi] from(select phone ,sum(Abune) as Abune,sum(cdma_borc) as cdma_borc,sum(cdma_ayliq) as cdma_ayliq ,sum(ay_abune) as ay_abune,sum([qacqin]) as [qacqin] ,sum([umumi]) as [umumi]from(select phone,cdma_ayliq,ay_abune,Abune,cdma_borc,[qacqin] ,[umumi] from(select phone,sum (ay_abune) as ay_abune,sum(cdma_ayliq) as cdma_ayliq,sum(CASE WHEN abune<0 then abune else 0 end) as Abune,sum(CASE WHEN cdma_borc<0 then cdma_borc else 0 end) as cdma_borc,sum([qacqin]) as [qacqin] ,sum([umumi]) as [umumi]from(select phone, ay_abune, cdma_ayliq,abune=case when phone>3999999 then 0 else abune end,cdma_borc=case when phone<3999999 then 0 else cdma_borc end,shesab,[qacqin],[umumi]from(select phone,shesab,(ayliq+servis) as ay_abune,cdma as cdma_ayliq, (kborc-(ayliq+servis)+[abune odenis]+abune_kr_dax) as abune,(kborc-cdma+[abune odenis]+abune_kr_dax) as cdma_borc,kborc,cdma,[abune odenis],abune_kr_dax, [qacqin],[umumi] from (--select phone,shesab,servis,[abune odenis],kborc,ayliq,abune_kr_cix,abune_kr_dax,cdma, [qacqin]=case when shesab <>0 then 1 else 0 end ,[umumi] =case when shesab =0 then 1 else 0 end from(select ph.phone,ph.shesab ,isnull(meb,0) as servis,isnull(gt.abune,0)as [abune odenis],isnull(t2.debt,0) as kborc,isnull(ft.Qiymet,0) as ayliq,isnull(yu.abune_kor_cix,0) as abune_kr_cix,isnull(ikj.abune_kor_dax,0) as abune_kr_dax,isnull(f.Qiymet,0) as cdma from(select*from phone where shesab=0 or shesab=54989 ) as ph left join (select p.phone,p.cdmaalamet,nb.Qiymetfrom phone as p inner join natiq.budc as nb on p.cdmaalamet=nb.Name where shesab=0)as f on f.phone=ph.phoneleft join (select p.phone,p.budce,nb.Qiymetfrom phone as p inner join natiq.budc as nb on p.budce=nb.Name where p.shesab=0)as ft on ft.phone=ph.phoneleft join (select nomre, sum(qiyme) as meb from(select sw.phone as nomre,sw.name as nam ,qy.Qiymet as qiymefrom servis_new as sw inner join servis_baza as qy on sw.name=qy.Name)y group by nomre)o on o.nomre=ph.phoneleft join (select convert(int,telefon) as te,sum(convert(money,odenis)/100.0) as abune from absher where convert(int,kod)=92 and convert(int,sifre) in (0,66)group by convert(int,telefon)) as gt on ph.phone=gt.teleft join (select convert(int,phone1)as phone1,sum(convert(money,debt)) as debt FROM borc92 group by convert(int,phone1)) as t2 ON t2.phone1 =ph.phoneleft join(select convert(int,telefon ) as kij,sum(convert(money,odenis)/100.0) as abune_kor_cix from absherwhere convert(int,sifre)=93 and convert(int,kod)=92 group by convert(int,telefon ))yu on yu.kij=ph.phoneleft join(select convert(int,telefon ) as hu,sum(convert(money,odenis)/100.0) as abune_kor_dax from absherwhere convert(int,sifre)=5093 and convert(int,kod)=92 group by convert(int,telefon ))ikj on ikj.hu=ph.phone-----)ko )lop)llllll)kol group by phone )op )ll group by phone )yy)jjj)ppppp)oo)mn group by phone)llb group by phone
the result this query sophone total M?bl?g< 1 ay M?bl?g< 2 ay M?bl?g< 3 ay M?bl?g< 4 ay M?bl?g< 5 ay M?blg> 5 ay--------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------1421048 -3.200000000 -3.200000000 0.000000000 0.000000000 0.000000000 0.000000000 0.0000000003400000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.0000000003400001 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.0000000003400002 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.0000000003400003 -3.200000000 -3.200000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000how i make create procedureto select the column as desiredexec dbo.test @t1='1'phone M?bl?g< 1 ay--------------------------------------- ---------------------------------------1421048 -3.2000000003400000 0.0000000003400001 0.0000000003400002 0.0000000003400003 -3.2000000003400004 0.000000000exec dbo.test @t1='1,2'phone M?bl?g< 1 ay M?bl?g< 2 ay--------------------------------------- --------------------------------------- ---------------------------------------1421048 -3.200000000 0.0000000003400000 0.000000000 0.0000000003400001 0.000000000 0.0000000003400002 0.000000000 0.0000000003400003 -3.200000000 0.0000000003400004 0.000000000 0.0000000003400005 -3.200000000 0.0000000003400006 -3.200000000 0.0000000003400007 0.000000000 0.0000000003400008 -2.000000000 0.000000000http://sql-az.tr.gg/