Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 stored proc

Author  Topic 

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2014-06-05 : 02:22:18
Hi! Friend

I wrote this query


select 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 (
select
phone,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.Qiymet
from phone as p inner join natiq.budc as nb on p.cdmaalamet=nb.Name where shesab=0)as f on f.phone=ph.phone
left join




(select p.phone,p.budce,nb.Qiymet
from phone as p inner join natiq.budc as nb on p.budce=nb.Name where p.shesab=0
)as ft on ft.phone=ph.phone
left join
(select nomre, sum(qiyme) as meb
from(
select sw.phone as nomre,sw.name as nam ,qy.Qiymet as qiyme
from servis_new as sw inner join servis_baza as qy on sw.name=qy.Name

)y group by nomre
)o
on o.nomre=ph.phone




left 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.te
left 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.phone

left join
(select convert(int,telefon ) as kij,sum(convert(money,odenis)/100.0) as abune_kor_cix from absher
where convert(int,sifre)=93 and convert(int,kod)=92 group by convert(int,telefon ))yu on yu.kij=ph.phone
left join
(select convert(int,telefon ) as hu,sum(convert(money,odenis)/100.0) as abune_kor_dax from absher
where 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
so

phone 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.000000000
3400000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
3400001 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
3400002 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000
3400003 -3.200000000 -3.200000000 0.000000000 0.000000000 0.000000000 0.000000000 0.000000000



how i make create procedure
to select the column as desired

exec dbo.test @t1='1'

phone M?bl?g< 1 ay
--------------------------------------- ---------------------------------------
1421048 -3.200000000
3400000 0.000000000
3400001 0.000000000
3400002 0.000000000
3400003 -3.200000000
3400004 0.000000000


exec dbo.test @t1='1,2'

phone M?bl?g< 1 ay M?bl?g< 2 ay
--------------------------------------- --------------------------------------- ---------------------------------------
1421048 -3.200000000 0.000000000
3400000 0.000000000 0.000000000
3400001 0.000000000 0.000000000
3400002 0.000000000 0.000000000
3400003 -3.200000000 0.000000000
3400004 0.000000000 0.000000000
3400005 -3.200000000 0.000000000
3400006 -3.200000000 0.000000000
3400007 0.000000000 0.000000000
3400008 -2.000000000 0.000000000







http://sql-az.tr.gg/

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-06-05 : 11:20:30
You'll either need dynamic sequel or a series of if...else if statements to achieve the desired effect. Try to get it going in a script before building a proc. That is, add your variable:

declare @t1 int = 1


and change your query to a dynamic one that uses the value of @t1 to determine the output columns. Also check out Jeff Moden's string splitter. It will help you out

http://www.sqlservercentral.com/articles/Tally+Table/72993/

Once you have a script that works, you can wrap it in a proc.

BTW, those are some ugly column names! Try to use column names that don't require quoting (restrict them to valid characters for column names). Otherwise I pity the dev who has to support your proc after you move on.
Go to Top of Page
   

- Advertisement -