2012-04-13 78 views
0

我有2个表,如下所示加盟条件检索不需要答案

select Event_ID,[Gross Salary] from tblEar where Event_ID=14 

结果:

Event_ID Gross Salary 
14  56128 
14  51984 
14  42028 

和:

select EventId, [Order Date],Amount from tblBudget where EventId=14 

结果:

EventId Order Date Amount 
14  10/10/2011 20000 
14  10/10/2011 20000 
14  20/03/2012 2500 
14  02/04/2012 -50000 

如果我在这两个表上写一个连接表达来获取它是检索重复的记录。我用Distinct但没有正面结果。

select DISTINCT tba.[Order Date],ISNULL(tba.Amount,0),ISNULL(te.[Gross Salary],0) from tblBudget tba 
        join 
         tblEar te on tba.EventId=te.Event_ID where tba.EventId=14 

我得到了以下答案:

Order Date (No column name) (No column name) 
2011-10-10 20000.00   42028.00 
2011-10-10 20000.00   51984.00 
2011-10-10 20000.00   56128.00 
2012-03-20 2500.00   42028.00 
2012-03-20 2500.00   51984.00 
2012-03-20 2500.00   56128.00 
2012-04-02 -50000.00  42028.00 
2012-04-02 -50000.00  51984.00 
2012-04-02 -50000.00  56128.00 

任何一个可以告诉你要组数据的方式来获得Accuarate数据

+0

我已经格式化了您的问题,以便查询和结果可以被读取(我还切换了前两个查询的顺序到结果,因为结果集似乎更好地匹配查询)。但是你没有告诉我们你期待的结果*。 – 2012-04-13 12:16:19

回答

0

我想和汇总金额:

SELECT tba.[Order Date], SUM(tba.Amount), SUM(te.[Gross Salary]) 
FROM tblBudget tba 
JOIN tblEar te on tba.EventId = te.Event_ID 
WHERE tba.EventId = 14 
GROUP BY tba.[Order Date]