2015-07-21 100 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