Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过MYSQL查询获得每天每次悬停的销售额总和?_Mysql_Sql Server - Fatal编程技术网

如何通过MYSQL查询获得每天每次悬停的销售额总和?

如何通过MYSQL查询获得每天每次悬停的销售额总和?,mysql,sql-server,Mysql,Sql Server,我需要得到一天内每次悬停销售的总和。 我的数据库中的日期为(mm/dd/yyyy hh:mm) 我有一张桌子 我需要这样的东西: 创建一个可以容纳小时数的表可能很有价值,如下所示: create table hours (hr int primary key); insert into hours values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12), (13),(14),(15),(16),(17),(18),(19),(20)

我需要得到一天内每次悬停销售的总和。 我的数据库中的日期为(mm/dd/yyyy hh:mm) 我有一张桌子

我需要这样的东西:

创建一个可以容纳小时数的表可能很有价值,如下所示:

create table hours (hr int primary key);

insert into hours values
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24);
让我们假设您的表在MySQL中是这样的:

create table test (
    sale_no int auto_increment primary key,
    entry_date timestamp,
    sales_done int
);
create table test (
    sale_no int identity primary key,
    entry_date datetime,
    sales_done int
);
或者如果它是SQL Server:

create table test (
    sale_no int auto_increment primary key,
    entry_date timestamp,
    sales_done int
);
create table test (
    sale_no int identity primary key,
    entry_date datetime,
    sales_done int
);
数据如下所示:

insert into test (entry_date, sales_done) values 
('2017-03-06 01:27:00', 5), ('2017-03-06 01:26:00', 7),
('2017-03-06 02:25:00', 7), ('2017-03-06 02:23:00', 6),
('2017-03-06 02:18:00', 5), ('2017-03-06 02:07:00', 4),
('2017-03-06 03:26:00', 5), ('2017-03-06 03:25:00', 7),
('2017-03-06 04:23:00', 7), ('2017-03-06 04:18:00', 8);
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select hour(entry_date) as hr, sum(sales_done) as count
    from test
    group by hour(entry_date)
) t on t.hr = h.hr
order by h.hr;
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select datepart(hour, entry_date) as hr, sum(sales_done) as count
    from test
    group by datepart(hour, entry_date)
) t on t.hr = h.hr
order by h.hr;
+----+-------+
| hr | count |
+----+-------+
|  1 |    12 |
|  2 |    22 |
|  3 |    12 |
|  4 |    15 |
|  5 |     0 |
| .. |    .. |
| 24 |     0 |
+----+-------+
您在MySQL中编写的查询是:

insert into test (entry_date, sales_done) values 
('2017-03-06 01:27:00', 5), ('2017-03-06 01:26:00', 7),
('2017-03-06 02:25:00', 7), ('2017-03-06 02:23:00', 6),
('2017-03-06 02:18:00', 5), ('2017-03-06 02:07:00', 4),
('2017-03-06 03:26:00', 5), ('2017-03-06 03:25:00', 7),
('2017-03-06 04:23:00', 7), ('2017-03-06 04:18:00', 8);
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select hour(entry_date) as hr, sum(sales_done) as count
    from test
    group by hour(entry_date)
) t on t.hr = h.hr
order by h.hr;
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select datepart(hour, entry_date) as hr, sum(sales_done) as count
    from test
    group by datepart(hour, entry_date)
) t on t.hr = h.hr
order by h.hr;
+----+-------+
| hr | count |
+----+-------+
|  1 |    12 |
|  2 |    22 |
|  3 |    12 |
|  4 |    15 |
|  5 |     0 |
| .. |    .. |
| 24 |     0 |
+----+-------+
在SQL Server中编写的查询:

insert into test (entry_date, sales_done) values 
('2017-03-06 01:27:00', 5), ('2017-03-06 01:26:00', 7),
('2017-03-06 02:25:00', 7), ('2017-03-06 02:23:00', 6),
('2017-03-06 02:18:00', 5), ('2017-03-06 02:07:00', 4),
('2017-03-06 03:26:00', 5), ('2017-03-06 03:25:00', 7),
('2017-03-06 04:23:00', 7), ('2017-03-06 04:18:00', 8);
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select hour(entry_date) as hr, sum(sales_done) as count
    from test
    group by hour(entry_date)
) t on t.hr = h.hr
order by h.hr;
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select datepart(hour, entry_date) as hr, sum(sales_done) as count
    from test
    group by datepart(hour, entry_date)
) t on t.hr = h.hr
order by h.hr;
+----+-------+
| hr | count |
+----+-------+
|  1 |    12 |
|  2 |    22 |
|  3 |    12 |
|  4 |    15 |
|  5 |     0 |
| .. |    .. |
| 24 |     0 |
+----+-------+
结果:

insert into test (entry_date, sales_done) values 
('2017-03-06 01:27:00', 5), ('2017-03-06 01:26:00', 7),
('2017-03-06 02:25:00', 7), ('2017-03-06 02:23:00', 6),
('2017-03-06 02:18:00', 5), ('2017-03-06 02:07:00', 4),
('2017-03-06 03:26:00', 5), ('2017-03-06 03:25:00', 7),
('2017-03-06 04:23:00', 7), ('2017-03-06 04:18:00', 8);
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select hour(entry_date) as hr, sum(sales_done) as count
    from test
    group by hour(entry_date)
) t on t.hr = h.hr
order by h.hr;
select h.hr, coalesce(t.count, 0) as count 
from hours h
left join (
    select datepart(hour, entry_date) as hr, sum(sales_done) as count
    from test
    group by datepart(hour, entry_date)
) t on t.hr = h.hr
order by h.hr;
+----+-------+
| hr | count |
+----+-------+
|  1 |    12 |
|  2 |    22 |
|  3 |    12 |
|  4 |    15 |
|  5 |     0 |
| .. |    .. |
| 24 |     0 |
+----+-------+
MySQL示例:


SQL Server示例:

您觉得下面的答案有用吗?如果有用的话,你能将其标记为已接受以结束你的问题吗?