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由于LEFT JOIN不成功而产生的 a 。您不能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 回答