0

我有一个查询,它使用不同的方法返回我正在寻找的数据,但是当我对该数据求和时,我的层次点“4-2-0-0-5-2”得到了错误的数量。4-2-0-0-5-2 有多行,所以当我总结它时,它加起来不正确。将 distinct 合并到 SUM 语句中的最佳方法是什么。任何帮助将不胜感激。谢谢。

第一个查询:

     Select distinct B.Proj_Nbr,c.proj_cc,h.proj_cc, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per, A.Amount
     from acct_bal a 
     inner join dim_proj b on a.dim_proj_id = b.dim_proj_id 
     inner join essbase_fcs.projects_hier_map c on c.proj_nbr = b.proj_nbr 
     inner join dim_per_mo d on d.dim_per_mo_id = a.dim_per_mo_id 
     Inner Join Dim_Acct F On A.Dim_Acct_Id = F.Dim_Acct_Id 
     Inner Join Dim_Org G On A.Dim_Org_Id = G.Dim_Org_Id 
     inner join essbase_fcs.projects_hier_map h on h.proj_cc = g.cost_ctr
     inner join dim_org g1 on c.proj_cc = g1.cost_ctr
     Where F.Fin_Lee_Nbr = 500 
     and c.proj_hier like '4-2-0-0-5-2%'
     And A.Dim_Scnro_Id = '45'
     And D.Fscl_Yr = '2014'
    And b.Proj_Nbr = '9005459'
    and fscl_per ='1'

结果 2 行:

9005459 0358080 0358080 4-2-0-0-5-2 Global Sales.com (iSell) 179777.09
9005459 0358080 0358057 4-2-0-0-5-5 Global Sales.com (iSell) 2257.3**

当我想对数据求和时,我使用下面的这个查询。这给了我正在寻找的两行,但 proj_hier 4-2-0-0-5-2 的数量错误,因为它有多行。

 Select  B.Proj_Nbr,c.proj_cc, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per, sum(A.Amount)
     from acct_bal a 
     inner join dim_proj b on a.dim_proj_id = b.dim_proj_id 
     inner join essbase_fcs.projects_hier_map c on c.proj_nbr = b.proj_nbr 
     inner join dim_per_mo d on d.dim_per_mo_id = a.dim_per_mo_id 
     Inner Join Dim_Acct F On A.Dim_Acct_Id = F.Dim_Acct_Id 
     Inner Join Dim_Org G On A.Dim_Org_Id = G.Dim_Org_Id 
     inner join essbase_fcs.projects_hier_map h on h.proj_cc = g.cost_ctr 
     inner join dim_org g1 on c.proj_cc = g1.cost_ctr
     Where F.Fin_Lee_Nbr = 500 
     and c.proj_hier like '4-2-0-0-5-2%'
     And A.Dim_Scnro_Id = '45'
     And D.Fscl_Yr = '2014'
    And b.Proj_Nbr = '9005459'
    and fscl_per ='1'
    group by B.Proj_Nbr,c.proj_cc,f.dim_acct_id, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per
                    having Sum(A.Amount) <> 0 
                   Order By  H.Proj_Hier,  B.Proj_Nbr, D.Fscl_Per
4

1 回答 1

0

请概括问题然后问,如果我理解你的问题这是解决方案:

一般查询:

从 your_table 中选择 sum(a.amountColumn)

按 agrrColumnName 分组;

如果我更改您的查询:

选择不同的 B.Proj_Nbr,c.proj_cc,h.proj_cc, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per, sum(A.Amount) from acct_bal a 内连接 dim_proj b on a.dim_proj_id = b.dim_proj_id 内连接 essbase_fcs .projects_hier_map c on c.proj_nbr = b.proj_nbr 内连接 dim_per_mod on d.dim_per_mo_id = a.dim_per_mo_id 内连接 Dim_Acct F On A.Dim_Acct_Id = F.Dim_Acct_Id 内连接 Dim_Org G On A.Dim_Org_Id = G.Dim_Org_Id 内连接 essbase_fcs .projects_hier_map h on h.proj_cc = g.cost_ctr inner join dim_org g1 on c.proj_cc = g1.cost_ctr where F.Fin_Lee_Nbr = 500 and c.proj_hier like '4-2-0-0-5-2%' And A .Dim_Scnro_Id = '45' And D.Fscl_Yr = '2014' and b.Proj_Nbr = '9005459' and fscl_per ='1' group by B.Proj_Nbr;

于 2013-08-09T16:13:57.837 回答