我有两张桌子:
一个是通话记录列表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是从系统自动生成的
表2由用户输入,表2的表结构是
| Date | fsct01 | fcst02 ........... fcst0N | -------------------------------------------------- | date | int | int ............. int |
并在每次更新时垂直填充。
现在,table1和table2的关系就像在table1的每个记录中一样,需要table2的一列。第1行与fcst01配对,第2行与fcst02配对,依此类推,使用键row_date
= date
。
table1在一个日期(66行)下有多个行,table2每个日期只有1行。
week aps sfcst ... 27 28 568 ... 27 26 889 ... 28 28 284 ... 28 37 948 ...
我希望按week
对此进行分组,但似乎fsct
正在弄乱它,因为它需要包含在分组中。
我希望它是......
week aps sfcst ... 27 sum(aps) [sum of all fcst within the week] ... 28 sum(aps) [sum of all fcst within the week] ... 29 sum(aps) [sum of all fcst within the week] ... 30 sum(aps) [sum of all fcst within the week] ...
希望我有意义, 可能有什么可以解决这个问题?
答案 0 :(得分:0)
很抱歉,但你的问题并不是那么清楚。您能否通过以下链接重新发帖:
Short, Self Contained, Correct (Compilable), Example
还要给你一些思考的食物,你可以看看选项:
答案 1 :(得分:0)
终于找到了正确的逻辑。只需将table2分组并将其与table1 ON table1.week = table2.week。
连接即可以下是代码:
SELECT
datepart(ISOWK, convert(datetime, row_date, 102)) as week,
MAX(avg_positioned_staff) as aps,
(sfcst01+ sfcst02+ sfcst03+ sfcst04+ sfcst05+ sfcst06+ sfcst07+ sfcst08+ sfcst09+ sfcst10+ sfcst11+ sfcst12+ sfcst13+ sfcst14+ sfcst15+ sfcst16+ sfcst17+ sfcst18+ sfcst19+ sfcst20+ sfcst21+ sfcst22+ sfcst23+ sfcst24+ sfcst25+ sfcst26+ sfcst27+ sfcst28+ sfcst29+ sfcst30+ sfcst31+ sfcst32+ sfcst33+ sfcst34+ sfcst35+ sfcst36+ sfcst37+ sfcst38+ sfcst39+ sfcst40+ sfcst41+ sfcst42+ sfcst43+ sfcst44+ sfcst45+ sfcst46+ sfcst47+ sfcst48+ sfcst49+ sfcst50+ sfcst51+ sfcst52+ sfcst53+ sfcst54+ sfcst55+ sfcst56+ sfcst57+ sfcst58+ sfcst59+ sfcst60+ sfcst61+ sfcst62+ sfcst63+ sfcst64+ sfcst65+ sfcst66) 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
FROM [AA].[dbo].[history]
left join
( select datepart(ISOWK, convert(datetime, date, 102) ) as week2, sum(fcst01) as sfcst01 , sum(fcst02) as sfcst02 , sum(fcst03) as sfcst03 , sum(fcst04) as sfcst04 , sum(fcst05) as sfcst05 , sum(fcst06) as sfcst06 , sum(fcst07) as sfcst07 , sum(fcst08) as sfcst08 , sum(fcst09) as sfcst09 , sum(fcst10) as sfcst10 , sum(fcst11) as sfcst11 , sum(fcst12) as sfcst12 , sum(fcst13) as sfcst13 , sum(fcst14) as sfcst14 , sum(fcst15) as sfcst15 , sum(fcst16) as sfcst16 , sum(fcst17) as sfcst17 , sum(fcst18) as sfcst18 , sum(fcst19) as sfcst19 , sum(fcst20) as sfcst20 , sum(fcst21) as sfcst21 , sum(fcst22) as sfcst22 , sum(fcst23) as sfcst23 , sum(fcst24) as sfcst24 , sum(fcst25) as sfcst25 , sum(fcst26) as sfcst26 , sum(fcst27) as sfcst27 , sum(fcst28) as sfcst28 , sum(fcst29) as sfcst29 , sum(fcst30) as sfcst30 , sum(fcst31) as sfcst31 , sum(fcst32) as sfcst32 , sum(fcst33) as sfcst33 , sum(fcst34) as sfcst34 , sum(fcst35) as sfcst35 , sum(fcst36) as sfcst36 , sum(fcst37) as sfcst37 , sum(fcst38) as sfcst38 , sum(fcst39) as sfcst39 , sum(fcst40) as sfcst40 , sum(fcst41) as sfcst41 , sum(fcst42) as sfcst42 , sum(fcst43) as sfcst43 , sum(fcst44) as sfcst44 , sum(fcst45) as sfcst45 , sum(fcst46) as sfcst46 , sum(fcst47) as sfcst47 , sum(fcst48) as sfcst48 , sum(fcst49) as sfcst49 , sum(fcst50) as sfcst50 , sum(fcst51) as sfcst51 , sum(fcst52) as sfcst52 , sum(fcst53) as sfcst53 , sum(fcst54) as sfcst54 , sum(fcst55) as sfcst55 , sum(fcst56) as sfcst56 , sum(fcst57) as sfcst57 , sum(fcst58) as sfcst58 , sum(fcst59) as sfcst59 , sum(fcst60) as sfcst60 , sum(fcst61) as sfcst61 , sum(fcst62) as sfcst62 , sum(fcst63) as sfcst63 , sum(fcst64) as sfcst64 , sum(fcst65) as sfcst65 , sum(fcst66) as sfcst66 from [AA].[dbo].[forcast] group by datepart(ISOWK, convert(datetime, date, 102) ) ) t2
on datepart(ISOWK, convert(datetime, row_date, 102)) = t2.week2
where split = 451 and time != ' 6:45AM - 7:00AM'
group by datepart(ISOWK, convert(datetime, row_date, 102)),sfcst01, sfcst02, sfcst03, sfcst04, sfcst05, sfcst06, sfcst07, sfcst08, sfcst09, sfcst10, sfcst11, sfcst12, sfcst13, sfcst14, sfcst15, sfcst16, sfcst17, sfcst18, sfcst19, sfcst20, sfcst21, sfcst22, sfcst23, sfcst24, sfcst25, sfcst26, sfcst27, sfcst28, sfcst29, sfcst30, sfcst31, sfcst32, sfcst33, sfcst34, sfcst35, sfcst36, sfcst37, sfcst38, sfcst39, sfcst40, sfcst41, sfcst42, sfcst43, sfcst44, sfcst45, sfcst46, sfcst47, sfcst48, sfcst49, sfcst50, sfcst51, sfcst52, sfcst53, sfcst54, sfcst55, sfcst56, sfcst57, sfcst58, sfcst59, sfcst60, sfcst61, sfcst62, sfcst63, sfcst64, sfcst65, sfcst66
order by datepart(ISOWK, convert(datetime, row_date, 102))
干杯!