左连接子查询

时间:2015-07-01 00:20:26

标签: sql tsql join subquery

有人可以帮我解决这个问题吗?在子查询P中,存在一个会计年度,其中BudgetActivityDetailCurrentBiennium中不存在同一年。我需要此查询来显示该年度金额的空值。目前那一年根本没有出现。

SELECT
    P.FiscalYear
   ,P.BudgetNbr
   ,SUM(sec.BudgetActivityDetailCurrentBiennium.TranAmount) AS Amount
FROM 

(SELECT
  sec.BudgetIndexCurrentBiennium.BudgetNbr
  ,AVG(CAST(sec.BudgetIndexCurrentBiennium.BienniumYear AS INT)+1) AS FiscalYear
FROM
  sec.BudgetIndexCurrentBiennium
GROUP BY
  sec.BudgetIndexCurrentBiennium.BudgetNbr

UNION ALL

SELECT
  sec.BudgetIndexCurrentBiennium.BudgetNbr
  ,AVG(CAST(sec.BudgetIndexCurrentBiennium.BienniumYear AS INT)+2) AS FiscalYear
FROM
  sec.BudgetIndexCurrentBiennium 
GROUP BY
 sec.BudgetIndexCurrentBiennium.BudgetNbr) AS P

LEFT JOIN sec.BudgetActivityDetailCurrentBiennium
   ON
    P.BudgetNbr = sec.BudgetActivityDetailCurrentBiennium.BudgetNbr
AND P.FiscalYear = sec.BudgetActivityDetailCurrentBiennium.FiscalYear
WHERE sec.BudgetActivityDetailCurrentBiennium.BudgetNbr = '076036'
GROUP BY 
P.FiscalYear
,P.BudgetNbr

1 个答案:

答案 0 :(得分:5)

我认为问题是你的cell.textLabel?.text = ToDoItems[indexPath.row] 条款,

WHERE

适用于WHERE sec.BudgetActivityDetailCurrentBiennium.BudgetNbr = '076036' 作为一个整体,而不仅仅是(我猜您打算)左连接。

SELECT更改为WHERE。这应该可以解决问题。