View Full Version : بدست آوردن جمع یا تفریق هر ستون که ردیف منحصر بفرد میباشد
khorsandreza
یک شنبه 20 فروردین 1391, 00:40 صبح
سلام بر همه دوستان
من با استفاده از دستور زیر دو ردیف اطلاعات را می خوان که حاصل اجرای دستور تصویر شماره یک ساخته میشود
شکل 1
http://etaksan.thrnosazi.ir/images/1.jpg
حال می خواهم در ادامه دستور طوری نوشته شود که نتیجه هر ستون در انتهای ستون نوشته شود تصویر شماره 2
http://etaksan.thrnosazi.ir/images/2.jpg
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
from VIEW_CROSSCITY t1
Where t1.code='0100' and t1.TAR ='9011'
UNION
SELECT t2.TAR,t2.b1,t2.b2,t2.b3,t2.b4,t2.b5
from VIEW_CROSSCITY t2
Where t2.code='0100' and t2.TAR ='9012'
esiprogrammer
یک شنبه 20 فروردین 1391, 01:12 صبح
شما میتونید از Temptable استفاده کنید. نتیجه query اول رو توی temptable بریزید
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
into #temp1
from VIEW_CROSSCITY t1
Where t1.code='0100' and t1.TAR ='9011'
بعد روی جدور temp محاسبات رو انجام دهید
سپس جدول temp رو باجدول اصلیتان Union کنید
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
into #temp1
from VIEW_CROSSCITY t1
Where t1.code='0100' and t1.TAR ='9011'
union
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
from #temp1
Where t1.code='0100' and t1.TAR ='9011'
khorsandreza
یک شنبه 20 فروردین 1391, 08:31 صبح
شما میتونید از Temptable استفاده کنید. نتیجه query اول رو توی temptable بریزید
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
into #temp1
from VIEW_CROSSCITY t1
Where t1.code='0100' and t1.TAR ='9011'
بعد روی جدور temp محاسبات رو انجام دهید
سپس جدول temp رو باجدول اصلیتان Union کنید
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
into #temp1
from VIEW_CROSSCITY t1
Where t1.code='0100' and t1.TAR ='9011'
union
SELECT t1.TAR,t1.b1,t1.b2,t1.b3,t1.b4,t1.b5
from #temp1
Where t1.code='0100' and t1.TAR ='9011'
با تشکر که پاسخ دادید ولی عمل تفریق در ستون ها را من متوجه نشدم کجا انجام می گیرد؟
یوسف زالی
یک شنبه 20 فروردین 1391, 19:37 عصر
سلام.
نیازی به استفاده از جداول موقت ندارید.
with CTE as (select (row_number() over(order by getdate()) -1.5) * 2 Row, TAR, B1, B2, B3, B4, B5 from TBL) -- common table
select Row, TAR, B1, B2, B3, B4, B5 form CTE
union
select 2, 'SUM', sum(B1 * -Row), sum(B2 * -Row), Sum(B3 * -Row), Sum(B4 * -Row), sum(B5 * -Row) from CTE
order by Row
توصیه می کنم حتی اگر از این روش استفاده نمی کنید کمی باهاش کار کنید.
نکته زیاد داره.
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.