我该怎么做这个SQL连接?

时间:2016-07-26 10:56:54

标签: sql sql-server

我有一个视图,它将数据保存在一个表中,而一个表将数据保存在另一个表中。

该视图包含此代码

SELECT  Underwriter, 
        LTRIM(Agent) AS Agent, 
        DATEADD(m, DATEDIFF(m, 0, DateCreated), 0) AS DateCreated, 
        SUM([Unique]) AS Claims 
FROM [C3 ODS].MagentaClaims_Daily_Prefix_VIEW
WHERE (LEFT(Claim_Number, 1) <> 'Z') AND (Claims = 'Y')
GROUP BY DATEADD(m, DATEDIFF(m, 0, DateCreated), 0), Agent, Underwriter, DateCreated
ORDER BY DATEADD(m, DATEDIFF(m, 0, DateCreated), 0)

虽然表格包含以下四列:InsurerAgentDayBudgeted Forecast

我需要进行连接,以便生成一个最终表格,将保险公司与保险公司,代理人和代理人日期连接到日期,然后是索赔数字,然后是预算金额的最后一列。

我将如何进行加入?

视图中有39290条记录,表格中有10,000条记录

我尝试过这样的外连接:

SELECT TOP (100) PERCENT 
        c3.Underwriter, 
        LTRIM(c3.Agent) AS Agent, 
        DATEADD(m,DATEDIFF(m, 0, c3.DateCreated), 0) AS DateCreated, 
        SUM(c3.[Unique]) AS Claims, 
        c3_f.Budgeted_Amount
FROM [C3 ODS].MagentaClaims_Daily_Prefix_VIEW c3
RIGHT OUTER JOIN [C3 Forecast].Budget_Data_Dan  c3_f
    ON c3.Underwriter = c3_f.Insurer_Underwriter AND 
        c3.Agent = c3_f.Agent AND 
        c3.DateCreated = c3_f.Day
WHERE (LEFT(c3.Claim_Number, 1) <> 'Z') AND 
        (c3.Claims = 'Y')
GROUP BY DATEADD(m, DATEDIFF(m, 0, c3.DateCreated), 0), 
        c3.Agent, 
        c3.Underwriter, 
        c3.DateCreated, 
        c3_f.Budgeted_Amount
ORDER BY DateCreated

但它只返回了161条记录,理想情况下我需要10,000条记录,这些记录将包括承销商,代理商,DateCreated,索赔和budgeted_amount

视图中的39121条记录有很多重复项,因此分组将只提供10,000条记录,这些记录将具有两个竞争价值 - claim列和budgeted_Amount

这是一个示例

Insurer1 - Agent1   1/6/2016    15  12,
Insurer2 - Agent2   1/6/2016    17  26

因此布局正确但连接没有返回足够的值

任何指针都会非常有用。

由于 丹

0 个答案:

没有答案