0

次のクエリは、内部残高があり、毎日のデータが必要な場合の残高数量を表示しません。

SELECT I.Date as Date, 
P.ResourceName as ItemName,
P.ResourceRate as Rate,
i.TotalInQty as InwardsQuantity,
s.TotalOutQty as OutwardsQuantity,
i.TotalInQty-S.TotalOutQty as Balance,
O.UnitSymbol As ItemUnit
FROM UnitMaster O
INNER JOIN Resource_Master P
 ON O.UnitCode = P.Unitcode
LEFT JOIN
(
  select COALESCE(sum(i.Qty), 0) AS TotalInQty, Nameofitem, Min(I.Date) AS Date
  from Inwards_Master I
  group by Nameofitem
) I
ON I.Nameofitem= P.ResourceName
LEFT JOIN
(
  select COALESCE(sum(s.qty), 0) AS TotalOutQty, Nameofitem, Min(S.Date) AS Date
  from Outwards_Master S 
  group by s.Nameofitem
) S
ON I.Date=S.Date 
AND I.Nameofitem =S.Nameofitem
WHERE P.TenderCode=1

これについて洞察を与える機関はありますか?

4

2 に答える 2

0

最初の問題はCOALESCE、間違ったレベルで使用していることだと思います。実際にやりたいことは、が失敗したNULLために生成された a を排除することです。その行セットには存在しないため、サブクエリでそれをLEFT JOIN排除することはできません。NULL

SELECT I.Date as Date, 
P.ResourceName as ItemName,
P.ResourceRate as Rate,
COALESCE(i.TotalInQty,0) as InwardsQuantity,
COALESCE(s.TotalOutQty,0) as OutwardsQuantity,
COALESCE(i.TotalInQty,0)-COALESCE(S.TotalOutQty,0) as Balance,
O.UnitSymbol As ItemUnit
FROM UnitMaster O
INNER JOIN Resource_Master P
 ON O.UnitCode = P.Unitcode
LEFT JOIN
(
  select sum(i.Qty) AS TotalInQty, Nameofitem, Min(I.Date) AS Date
  from Inwards_Master I
  group by Nameofitem
) I
ON I.Nameofitem= P.ResourceName
LEFT JOIN
(
  select sum(s.qty) AS TotalOutQty, Nameofitem, Min(S.Date) AS Date
  from Outwards_Master S 
  group by s.Nameofitem
) S
ON I.Date=S.Date 
AND I.Nameofitem =S.Nameofitem
WHERE P.TenderCode=1
于 2013-01-18T07:08:36.243 に答える
0

これを試してください:

SELECT I.Date as Date,
P.ResourceName as ItemName,
P.ResourceRate as Rate,
COALESCE(i.TotalInQty,0) as InwardsQuantity,
--s.TotalOutQty as OutwardsQuantity,
COALESCE(s.TotalOutQty,0) as OutwardsQuantity,
--i.TotalInQty-s.TotalOutQty as Balance,
COALESCE(i.TotalInQty,0)-COALESCE(S.TotalOutQty,0) as Balance,
O.UnitSymbol As ItemUnit 
FROM UnitMaster O 
INNER JOIN Resource_Master P 
    ON O.UnitCode = P.Unitcode 
    LEFT JOIN(select sum(i.Qty) AS TotalInQty,Nameofitem,Min(I.Date) AS Date  from Inwards_Master I where Tendercode=1 group by Nameofitem) I ON I.Nameofitem= P.ResourceName 
    LEFT JOIN(select sum(s.qty) AS TotalOutQty,Nameofitem, Min(S.Date) AS Date  from Outwards_Master S where Tendercode=1 group by s.Nameofitem) S ON I.Date=S.Date AND I.Nameofitem =S.Nameofitem 
WHERE P.Status=1 and P.TenderCode= 1
于 2013-04-08T06:14:46.253 に答える