kamran_14
جمعه 29 آبان 1394, 10:42 صبح
سلام
خسته نباشید
من میخوام کوئری بنویسم که جمع مبلغ فروش فروشگاه رو به تفکیک ماه و براساس سال نمایش دهد.
در این کوئری تمامی ماهها در عنوان ستونها نشان داده میشود و داده ها هم جمع مبلغ فروش مربوط به هر ماه را نشان میدهد (گزارش گیری سالانه)
در جدول tblfactoreforush تاریخ فروش و شماره فاکتور فروش ذخیره میشود
در جدول tblAglamefactoreForush مبلغ فروش و شماره فاکتور فروش مربوطه ذخیره میشود
که فیلد شماره فاکتور در هر دو جدول فوق برابر است
آیا من میتونم این کد رو کوتاهتر کنم بدون اینکه خروجی تغیر کند
من خروجی رو هم فرستادم
select substring(tblsal.tarikh,1,4) as sal,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='01' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as farvardin,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='02' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as ordebehesht,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='03' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as khordad,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='04' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as tir,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='05' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as mordada,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='06' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as shahrivar,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='07' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as mehr,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='08' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as aban,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='09' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as azar,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='10' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as dey,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='11' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as bahman,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='12' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as esfand
--(select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
--from tblAglamefactoreForush
--inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID) as sal
from tblfactoreforush tblsal
group by substring(tblsal.tarikh,1,4)
خسته نباشید
من میخوام کوئری بنویسم که جمع مبلغ فروش فروشگاه رو به تفکیک ماه و براساس سال نمایش دهد.
در این کوئری تمامی ماهها در عنوان ستونها نشان داده میشود و داده ها هم جمع مبلغ فروش مربوط به هر ماه را نشان میدهد (گزارش گیری سالانه)
در جدول tblfactoreforush تاریخ فروش و شماره فاکتور فروش ذخیره میشود
در جدول tblAglamefactoreForush مبلغ فروش و شماره فاکتور فروش مربوطه ذخیره میشود
که فیلد شماره فاکتور در هر دو جدول فوق برابر است
آیا من میتونم این کد رو کوتاهتر کنم بدون اینکه خروجی تغیر کند
من خروجی رو هم فرستادم
select substring(tblsal.tarikh,1,4) as sal,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='01' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as farvardin,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='02' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as ordebehesht,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='03' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as khordad,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='04' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as tir,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='05' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as mordada,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='06' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as shahrivar,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='07' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as mehr,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='08' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as aban,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='09' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as azar,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='10' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as dey,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='11' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as bahman,
isnull((select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
from tblAglamefactoreForush
inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID
where SUBSTRING(tblfactoreforush.tarikh,6,2)='12' and SUBSTRING(tblfactoreforush.tarikh,1,4)=substring(t blsal.tarikh,1,4)),0) as esfand
--(select sum(tblAglamefactoreForush.tedad*tblAglamefactoreF orush.gemateforush)
--from tblAglamefactoreForush
--inner join tblfactoreforush on tblAglamefactoreForush.factoreforushID=tblfactoref orush.factoreforushID) as sal
from tblfactoreforush tblsal
group by substring(tblsal.tarikh,1,4)