我正在尝试根据“实际营业时间”推断出的“营业时间”来总结每日交易。
我有以下StoreTransaction数据:
StoreId DayOfWeek HourOfDay TransactionCount
123 1 8 30
123 1 9 45
123 1 10 48
.
.
.
123 1 16 42
什么是将其数据透视/汇总为以下内容的SQL?
StoreId DayOfWeek HourFirst HourLast TotalTransactionCount
123 1 8 16 165
HourFirst是StoreId / DayOfWeek的MIN(小时),HourLast是MAX(Hour),TotalTransactionCount是StoreId / DayOfWeek的SUM(TransactionCount)。
谢谢!
答案 0 :(得分:3)
使用聚合函数
select StoreId,
DayOfWeek,
min( HourOfDay ) as HourFirst,
max(HourOfDay ) as HourLast,
sum(TransactionCount) as TotalTransactionCount from yourtable
group by StoreId, DayOfWeek