2013-03-01 86 views
0

我已經嘗試了許多選項來結合SQL Server數據,其中一些我可以工作,但數據是不正確的。結合SQL Server表數據

我有用戶,統計和結果表。我已經從前兩張表中獲得數據並將它們組合起來,信息很好;

create table #statsTable(
s_fullname char(45),    <--Here 
s_dialdate smalldatetime, 
s_campaign char(3), 
s_calls int, 
s_holdtime int, 
s_talktime int, 
s_wrapuptime int , 
s_dialtime int , 
s_pitches int , 
s_agent char(3) ,     <-- to here, insert fine 
s_samount decimal(20,2),   <--Here 
s_damount decimal(10,2) , 
s_upamount numeric (10,0), 
s_mdamount decimal (12,2))  <--to here, uses a separate query, not so much 

我已經嘗試使用連接等,但沒有什麼工作,什麼似乎發生是最後四個值似乎是結合林不知道,但他們現在是正確的。以下插入上表的第一部分;

INSERT INTO #statsTable (s_fullname, s_agent, s_calls, 
      s_holdtime, s_talktime, s_wrapuptime, s_pitches, s_dialtime, s_campaign, 
      s_dialdate) 
     SELECT 
     agent.name, agent.code, calls, holdtime, 
     talktime, wrapuptime, pitches, dialtime, campaign, 
     dialdate 
     FROM stats, agent 
     WHERE 
     agent.code LIKE stats.agent 
     AND dialdate = '02-27-2013' 

它的下一個部分是麻煩就來了,不管我嘗試加入或使用插入或更新查詢,最後四場拿到混亂。

我從中拉取數據的3張表看起來像這樣;

agent 
     name (full name) 
     code (3 char ID) 

    stats 
     dialdate 
     agent  (3 char ID) 
     campaign (3 char ID) 
     calls  (number of calls) 
     holdtime 
     talktime 
     wrapuptime 
     dialtime 
     pitches 

    results 
     lcdate (date last call was made) 
     campaginid (3 char ID) 
     sale  (overall sale amount) 
     donation (donation amount) 
     up_sale  (up-sale amount) 
     md_amount (not sure its purpose, but a decimal none the less) 

每個表中都有明顯更多的數據,但這是唯一與最終輸出相關的相關數據。

在此先感謝

+0

您需要包括正在更新SQL #statsTable中的最後4列。 – David 2013-03-01 21:52:07

+1

那個返回錯誤結果的單獨查詢在哪裏?你可以發佈嗎?還有什麼錯誤的結果看起來像? – 2013-03-01 21:56:58

+0

[不良習慣踢:使用舊式聯接](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 使用ANSI - ** 92 ** SQL標準(** 20年前**!)廢止舊式*逗號分隔的表*樣式列表。 ***請***停止使用它 – 2013-03-02 08:31:08

回答

1

能否通過三個表加入或做你重複的記錄,如果你做這樣的事?......

INSERT INTO #statsTable 
      (
      s_fullname, 
      s_agent, 
      s_calls, 
      s_holdtime, 
      s_talktime, 
      s_wrapuptime, 
      s_pitches, 
      s_dialtime, 
      s_campaign, 
      s_dialdate, 
      s_upamount --<<new 
      ) 
SELECT  agent.name, 
      agent.code, 
      calls, 
      holdtime, 
      talktime, 
      wrapuptime, 
      pitches, 
      dialtime, 
      campaign, 
      dialdate, 
      r.up_sale  --<<like this? 
FROM "stats" s 
      INNER JOIN agent a 
       ON s.agent = a.code 
      INNER JOIN results r 
       ON s.campagin = r.campaginid 
WHERE dialdate = '02-27-2013'; 


SELECT * 
FROM #statsTable; 
+0

@PM 77-1,謝謝你的幫助對不起,這裏是插入查詢; UPDATE SET #statsTable = s_dialdate results_NFP.lcdate,s_samount = results_NFP.s__amount, s_damount = results_NFP.d_amount,s_upamount = results_NFP.up_amount, s_mdamount = results_NFP.md_amount FROM results_NFP,#statsTable WHERE s_dialdate = results_NFP.lcdate AND lcdate = '02 -27-2013' – Iz3k34l 2013-03-03 02:50:00

+0

如果您使用任何其他信息修改原始帖子 – whytheq 2013-03-03 08:37:49