我有以下使用 sql server 2008 的场景
**** Original Result ****
================================================
Year month Category Count_days
================================================
2001 09 Leave 03
2001 09 Worked Below 8hrs 18
2001 09 Worked Above 8hrs 05
2001 09 Present 0 <----- current value
2001 10 Leave 01
2001 10 Worked Below 8hrs 10
2001 10 Worked Above 8hrs 09
2001 10 Present 0 <------ current value
以下是标准
criteria
===========
Present Count of 'x'th Month = SUM(Worked Below 8hrs count of 'x'th month) +
SUM(Worked Above 8hrs count of 'x'th month )
;where x is the month
我想要满足上述条件的结果
**** Expected Result ****
===============================================
Year month Category Count_days
================================================
2001 09 Leave 03
2001 09 Worked Below 8hrs 18
2001 09 Worked Above 8hrs 05
2001 09 Present 23 <-----(expecting sum 18+05 =23)
2001 10 Leave 01
2001 10 Worked Below 8hrs 10
2001 10 Worked Above 8hrs 09
2001 10 Present 19 <-----(expecting sum 10+09 = 19)
问题是原始结果是由非常复杂的查询生成的,因此不能再次调用相同的集合,即不能使用它(这会妨碍我的应用程序的性能。)=================
select * from original (some join) select * from original
可能需要使用单个查询,也可以是子查询,使用聚合函数等。
期待任何聚合技巧来产生我的预期结果????
请大家帮帮我......