【发布时间】:2015-09-17 08:24:59
【问题描述】:
我有两张桌子:
一个是通话记录列表table1,另一个是预测代理数量列表table2。
SELECT
datepart(ISOWK, convert(datetime, row_date, 102)) as week,
MAX(avg_positioned_staff) as aps,
(fcst01 + fcst02 + fcst03 + fcst04 + fcst05 + fcst06 + fcst07 + fcst08 + fcst09 + fcst10 + fcst11 + fcst12 + fcst13 + fcst14 + fcst15 + fcst16 + fcst17 + fcst18 + fcst19 + fcst20 + fcst21 + fcst22 + fcst23 + fcst24 + fcst25 + fcst26 + fcst27 + fcst28 + fcst29 + fcst30 + fcst31 + fcst32 + fcst33 + fcst34 + fcst35 + fcst36 + fcst37 + fcst38 + fcst39 + fcst40 + fcst41 + fcst42 + fcst43 + fcst44 + fcst45 + fcst46 + fcst47 + fcst48 + fcst49 + fcst50 + fcst51 + cst52 + fcst53 + fcst54 + fcst55 + fcst56 + fcst57 + fcst58 + fcst59 + fcst60 + fcst61 + fcst62 + fcst63 + fcst64 + fcst65 + fcst66) as sfcst,
SUM(acd_calls) + SUM(aban_calls) as snco,
SUM(acd_calls) as snch,
SUM(acd_calls_60secs) as nchwsl,
SUM(aban_calls) as snca,
SUM(acd_calls) as sans,
SUM(aban_calls) as saban,
SUM(tsa) as sasa,
SUM(acd_calls) as sacd,
MAX(max_delay) as mdly,
SUM(acd_time) as satt,
SUM(hold_time) as saoht,
SUM(held_calls) as sheld,
SUM(acw_time) as sacw,
SUM(handling_time) saht,
SUM(acd_calls_60secs) as ssl,
(SUM(acd_calls) + SUM(aban_calls)) as sAnsAbanSl,
SUM(handling_time) as socc,
SUM(avail_time) as savailtym,
SUM(staffed_time) as sstaffdtym,
fcst01, fcst02, fcst03, fcst04, fcst05, fcst06, fcst07, fcst08, fcst09, fcst10, fcst11, fcst12, fcst13, fcst14, fcst15, fcst16, fcst17, fcst18, fcst19, fcst20, fcst21, fcst22, fcst23, fcst24, fcst25, fcst26, fcst27, fcst28, fcst29, fcst30, fcst31, fcst32, fcst33, fcst34, fcst35, fcst36, fcst37, fcst38, fcst39, fcst40, fcst41, fcst42, fcst43, fcst44, fcst45, fcst46, fcst47, fcst48, fcst49, fcst50, fcst51, fcst52, fcst53, fcst54, fcst55, fcst56, fcst57, fcst58, fcst59, fcst60, fcst61, fcst62, fcst63, fcst64, fcst65, fcst66
FROM table1
LEFT JOIN table2
on table1.row_date = table2.date
GROUP BY
datepart(ISOWK, convert(datetime, row_date, 102)),
fcst01, fcst02, fcst03, fcst04, fcst05, fcst06, fcst07, fcst08, fcst09, fcst10, fcst11, fcst12, fcst13, fcst14, fcst15, fcst16, fcst17, fcst18, fcst19, fcst20, fcst21, fcst22, fcst23, fcst24, fcst25, fcst26, fcst27, fcst28, fcst29, fcst30, fcst31, fcst32, fcst33, fcst34, fcst35, fcst36, fcst37, fcst38, fcst39, fcst40, fcst41, fcst42, fcst43, fcst44, fcst45, fcst46, fcst47, fcst48, fcst49, fcst50, fcst51, fcst52, fcst53, fcst54, fcst55, fcst56, fcst57, fcst58, fcst59, fcst60, fcst61, fcst62, fcst63, fcst64, fcst65, fcst66
ORDER BY
datepart(ISOWK, convert(datetime, row_date, 102))
您可能首先注意到,我有一长串fcst,因为:
表 1 是从系统自动生成的
Table2由用户输入,Table2的表结构为
并且每次更新都会垂直填充。
现在,table1 和 table2 的关系就像在 table1 的每条记录中都需要 table2 的列。第 1 行与 fcst01 配对,第 2 行与 fcst02 配对,依此类推,使用密钥 row_date = date。
table1 将在一个日期下有许多行(66 行),而 table2 每个日期只有 1 行。
我想按week 对它进行分组,但似乎fsct 搞砸了,因为它需要包含在分组中。
我希望它是...
周aps sfcst ... 27 sum(aps) [一周内所有 fcst 的总和] ... 28 sum(aps) [一周内所有 fcst 的总和] ... 29 sum(aps) [一周内所有 fcst 的总和] ... 30 sum(aps) [一周内所有 fcst 的总和] ...希望我是有道理的, 有什么可能的解决方法?
【问题讨论】:
标签: sql sql-server sql-server-2008