2015-07-21 59 views
0

我有一个工作表,存储为员工转移信息:SQL表的摘要信息?

enter image description here

我的目标是创建一个显示信息的摘要一个新的视图或查询:

enter image description here

我想我需要一些类型的OLAP查询在这里,也许一组通过汇总...

我可以得到一些信息使用组像这样,但仍然需要一种方法将其与原始表结合起来,因此它仍然显示其他信息,如名称。使用SQL Server。

SELECT employee,[week], sum(regular), sum(overtime), 
     sum(regular_pay), sum(overtime_pay), sum(regular) + sum(overtime), 
     sum(regular_pay) + sum(overtime_pay) FROM shift_details GROUP BY employee, [week] 

回答

0

过去,当在SQL中为报表创建格式化表格时,我使用了基于新列的排序方法。

E.G.

SELECT employee, week, regular, overtime, reg_pay, ot_pay, regular + overtime AS total_hours, reg_pay + ot_pay AS total_pay, 1 AS sort_by FROM shift_details 
UNION ALL 
SELECT employee,[week], sum(regular), sum(overtime), 
     sum(regular_pay), sum(overtime_pay), sum(regular) + sum(overtime), 
     sum(regular_pay) + sum(overtime_pay), 2 AS sort_by FROM shift_details GROUP BY employee, [week] 
ORDER BY employee, week, sort_by 

...等...

如果你想省略 “sort_by”,只是包装这样的查询...

SELECT 
    employee, week, regular, overtime, reg_pay, ot_pay, total_hours, total_pay 
FROM 
    (
    ... query above without the "ORDER" statement. 
) AS inner_query 
ORDER BY employee, week, sort_by