2016-06-30 17 views
0

こんにちは私は同じdate.Iの別のテーブルからの無駄な値の合計を取得しようとしている以下のクエリから望ましい出力を達成することができますが、事前にquery.Thanksを縮小する他の方法はあり.Isこのユニオンのクエリを書くための良い方法があります

select SUM(WasteValue) as WasteValue from(
select sum(im.Pur_Rate *wd.Item_Qty) as WasteValue from [Item Master] im 
inner join [waste Details] wd on wd.Item_Code=im.Item_Code 
inner join [waste Master] wm on wd.Waste_No=wm.Waste_No 
and CONVERT(date,wm.Waste_Date,120)BETWEEN '2016-06-30' 
and '2016-06-30' and im.Type_Code=1 and im.Branch_Code=0 
union 
select sum(im.Pur_Rate *wd.Item_Qty) as WasteValue from [Item Master] im 
inner join [Counter Waste Details] wd on wd.Item_Code=im.Item_Code 
inner join [Counter Waste Master] wm on wd.Waste_No=wm.Waste_No 
and CONVERT(date,wm.Waste_Date,120)BETWEEN '2016-06-30' 
and '2016-06-30' and im.Type_Code=1 and im.Branch_Code=0)Wastage 
+0

2つの集計値の組合?.. –

+0

しかし、2つの独立したテーブルからです。 – Venkatvasan

+0

あなたは私を手に入れませんでした:そして、あなたはそれをどうしますか?いいえ、2つの数字だけです。 –

答えて

0

このデータ構造の多くを期待してはいけない、しかし、あなたは、少なくともこのような括弧の外の共通部分を取得することができます:

select SUM(Pur_Rate * Item_Qty) as WasteValue from (
select im.Pur_Rate, wd.Item_Qty, wm.Waste_Date, im.Type_Code, im.Branch_Code 
from [Item Master] im 
inner join [waste Details] wd on wd.Item_Code=im.Item_Code 
inner join [waste Master] wm on wd.Waste_No=wm.Waste_No 
union 
select im.Pur_Rate, wd.Item_Qty, wm.Waste_Date, im.Type_Code, im.Branch_Code 
from [Item Master] im 
inner join [Counter Waste Details] wd on wd.Item_Code=im.Item_Code 
inner join [Counter Waste Master] wm on wd.Waste_No=wm.Waste_No) Wastage 
where CONVERT(date,Waste_Date,120) BETWEEN '2016-06-30' and '2016-06-30' 
and Type_Code=1 and Branch_Code=0 

私があなただったら、私は似たような目的でテーブルを組み合わせることに目を向けるだろう(Waste Maste rとCounter Waste Masterなど)、両者を区別するためのフラグが付いています。

関連する問題