带有时间格式的每小时销售报告

时间:2014-06-04 04:28:56

标签: sql visual-studio sql-server-2008

这是我的代码:

select 
    SYSTEM_TIME as OR_TIME, 
    SUM (AMOUNT) as HOURLY_SALES 
from 
    T_SALES_detail
where 
    OUTLET_NO = 20193 and SYSTEM_DATE  = '6/2/2014' and VOID = 'N' 
group by 
    SYSTEM_TIME

输出是这样的:

OR_TIME   HOURLY_SALES
12:00   295
13:00   2122.86
14:00   2230
15:00   1800
16:00   3090
17:00   880
18:00   652.86

但我希望看到的输出是:

OR_TIME             HOURLY_SALES
12:00pm-12:59pm     295
1:00pmpm-1:59       2122.86
2:00pm-2:59pm           2230
3:00pm-3:59pm       1800
4:00pm-4:59pm       3090
5:00pm-5:59pm       880
6:00pm-6:59pm       652.86

我正在使用Visual Studio 2010和SQL Server 2008。

非常感谢你。

2 个答案:

答案 0 :(得分:1)

您可以使用DATEADD获取结束范围,并使用CONVERT将时间转换为12小时时间;

SELECT CONVERT(VARCHAR(7), SYSTEM_TIME, 100) + '-' +
       CONVERT(VARCHAR(7), DATEADD(s, 3599, SYSTEM_TIME), 100) OR_TIME,
       SUM (AMOUNT) as HOURLY_SALES from T_SALES_detail
WHERE OUTLET_NO = 20193 and SYSTEM_DATE  = '6/2/2014' and VOID = 'N' 
GROUP BY SYSTEM_TIME

答案 1 :(得分:0)

您可以使用DateAdd功能。

试试这个

Select Cast(SYSTEM_TIME As Varchar(100))+ '-' + Cast(DATEADD(MINUTE, 59 , SYSTEM_TIME)As Varchar(100))  As OR_TIME, SUM (AMOUNT) as HOURLY_SALES 
From T_SALES_detail
Where OUTLET_NO = 20193 and SYSTEM_DATE  = '6/2/2014' and VOID = 'N' 
Group By SYSTEM_TIME

<强> Fiddle Demo


O / P:

+-------------------+--------------------+
|    OR_TIME        |     HOURLY_SALES   |
+-------------------+--------------------+
| 12:00pm-12:59pm   |     295            |
| 1:00pm-1:59pm     |     2122.86        |
| 2:00pm-2:59pm     |     2230           |
| 3:00pm-3:59pm     |     1800           |
| 4:00pm-4:59pm     |     3090           |
| 5:00pm-5:59pm     |     880            |
| 6:00pm-6:59pm     |     652.86         |
+-------------------+--------------------+