Left Outer Join Query返回太多行

时间:2015-04-22 19:11:49

标签: sql

我在尝试从4个表中获取数据时遇到问题。我尝试了4个左外连接,现在我正在尝试一个子选择。但是一旦我尝试获取AD.AccountDirectToCommitmentItemCategoryID字段,我的查询就会从所需的4行返回到16行。我正在尝试将该列添加到我的结果中,但它会为该列中的每个数据复制每一行。我应该如何以不同的方式编写此查询?

  SELECT DISTINCT  AD.AccountDirectToCommitmentItemCategoryID, cic.CommitmentItemCategoryName, cict.CommitmentItemCategoryTypeName, cic.CommitmentItemCategoryTypeID, cic.CommitmentItemCategoryID, vw.DollarsAllocated
  FROM [dbo].[tblCommitmentItemCategory] as cic
  left OUTER join [dbo].vw_ParentAccountDollarsAllocatedByCommitmentItemCategory] as vw
   on vw.CommitmentItemCategoryID = cic.CommitmentItemCategoryID
   left OUTER join [dbo].[tblCommitmentItemCategoryType] cict
   ON CIC.CommitmentItemCategoryTypeID = cict.CommitmentItemCategoryTypeID


   left OUTER join (
    SELECT DISTINCT AccountDirectToCommitmentItemCategoryID, AccountDirectParentID FROM [dbo].[tblAccountDirectToCommitmentItemCategory] ADTCIC
    WHERE ADTCIC.AccountDirectParentID = 6478) AD
   ON 
   /*(AD.AccountDirectToCommitmentItemCategoryID = VW.CommitmentItemCategoryID)   GETS 4 ROWS BUT FIRST COLUMN IS NULL */
   (AD.AccountDirectParentID = VW.AccountDirectParentID)  /*RETURNS 16 ROWS */
   where FiscalYear = 2015 and vw.AccountDirectParentID = 6478
   order by cic.CommitmentItemCategoryName

我不会发布结果图片,因为我是新手。我希望这有帮助...

AccountDirectToCommitmentItemCategoryID CommitmentItemCategoryName  CommitmentItemCategoryTypeName  CommitmentItemCategoryTypeID    CommitmentItemCategoryID    DollarsAllocated
6   23**    Contractual 1   2   1200.00
7   23**    Contractual 1   2   1200.00
8   23**    Contractual 1   2   1200.00
9   23**    Contractual 1   2   1200.00
6   25**    Contractual 1   1   1100.00
7   25**    Contractual 1   1   1100.00
8   25**    Contractual 1   1   1100.00
9   25**    Contractual 1   1   1100.00
6   26**    Supplies    3   3   1300.00
7   26**    Supplies    3   3   1300.00
8   26**    Supplies    3   3   1300.00
9   26**    Supplies    3   3   1300.00
6   31**    Equipment   2   5   500.00
7   31**    Equipment   2   5   500.00
8   31**    Equipment   2   5   500.00
9   31**    Equipment   2   5   500.00

LINQ SOLUTION:但是当没有类别匹配时,我现在没有得到0美元的行: 我能够找出有问题的列是需要在where子句中再次指定的parentID。我从中创建了一个LINQ查询。不幸的是,当没有像以前那样的CIC类别时,我没有得到0美元的条目。有什么想法吗?

var queryDollars = from cic in JOHNContext.tblCommitmentItemCategory
   join vw in JOHNContext.vw_ParentAccountDollarsAllocatedByCommitmentItemCategory
        on new { CommitmentItemCategory = cic.CommitmentItemCategoryID, FiscalYear = 2015, ParentAccountID = 6478 }
            equals new { CommitmentItemCategory = vw.CommitmentItemCategoryID, FiscalYear = vw.FiscalYear, ParentAccountID = vw.AccountDirectParentID }
   into t
   from vw in t.DefaultIfEmpty()
   join cict in JOHNContext.tblCommitmentItemCategoryType
   on cic.CommitmentItemCategoryTypeID equals cict.CommitmentItemCategoryTypeID
   join ad in JOHNContext.tblAccountDirectToCommitmentItemCategory
   on vw.CommitmentItemCategoryID equals ad.CommitmentItemCategoryID
   where ad.AccountDirectParentID == 6478
   let displayCategory = cic.CommitmentItemCategoryName + " ( " + cict.CommitmentItemCategoryTypeName + " )"
// Called CategoryType but it's actually the ID below
   let displayCategoryTypeID = "CategoryType_" + ad.AccountDirectToCommitmentItemCategoryID.ToString()
//   let displayCategoryTypeID = "CategoryType_" + cic.CommitmentItemCategoryID.ToString()
   let displayDollarsAllocated = vw.DollarsAllocated == null ? 0 : vw.DollarsAllocated
   orderby cic.CommitmentItemCategoryName
   select new
   {
       displayCategory,
       displayCategoryTypeID,
       cic.CommitmentItemCategoryID,
       displayDollarsAllocated
   };

0 个答案:

没有答案