我已经尝试了很多选项来组合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 :(得分: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;