我正在使用一个存储员工班次信息的表格:
我的目标是创建一个显示信息摘要的新视图或查询:
我想我在这里需要某种类型的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 :(得分: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