0

我尝试了许多选项来组合 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)

每个表中显然都有更多数据,但这是与最终输出相关的唯一相关数据。

提前致谢

4

1 回答 1

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;
于 2013-03-01T22:11:39.763 回答