总是返回一个伯爵? MySQL的

时间:2018-02-23 16:54:43

标签: mysql sql

说我有这样的数据

1 Red 2018-02-01
2 Red 2018-02-01
3 Red 2018-02-01
4 Blue 2018-02-01
5 Red 2018-02-02
5 Blue 2018-02-03

我想获得每种颜色每天的总计数,这样就可以了。

Select Count(Color) as count, color, myDate from colors
where color in ('red', 'blue') 
group by myDate , color

这应该让我有点像

3 red 2018-02-01
1 blue 2018-02-01
1 red 2018-02-02
1 blue 2018-02-03

我真正想要的是这个

3 red 2018-02-01
1 blue 2018-02-01
1 red 2018-02-02
0 blue 2018-02-02 <---- I want to know there was 0 blue on this day
1 blue 2018-02-03
0 red 2018-02-03 <----- I want to know there was 0 red on this day.

是否有一种简单的方法可以获得此结果?我总是需要查看in子句中的任何内容。如果我添加其他东西,我需要每天看3种颜色。

编辑

希望这会让它更清晰

Id (int) - Pk
UserId (int)
Activity (varchar(20))
dDate (datetime)

数据

1 123 'Visited Home Page' 2018-02-23 05:15:14
2 123 'Visited Page 1' 2018-02-23 04:15:15
3 456 'Visited Home Page' 2018-02-23 04:15:16
4 456 'Visited Page 1' 2018-02-23 04:15:15
5 456 'Visited Page 1' 2018-02-24 04:15:15

预期结果

Count, Activity, dDate
2 'Visited Home Page' 2018-02-23
2 'Visited Page 1' 2018-02-23
1 'Visited Page 1' 2018-02-24
0 'Visited Home Page' 2018-02-24

SELECT Count(Activity) as Count, Activity,  Date(dDate) as d FROM myTable
WHERE Activity IN('Visited Home Page', 'Visited Page 1') AND 
DATE(dDateTime) BETWEEN '2018-01-01' AND '2018-01-31'
GROUP BY Date(dDate), Activity

Current Result

    Count, Activity, dDate
    2 'Visited Home Page' 2018-02-23
    2 'Visited Page 1' 2018-02-23
    1 'Visited Page 1' 2018-02-24

由于没有用户访问主页,因此未记录任何内容。但是我需要总是得到一切结果 我放入了我的where子句。

如果我添加了另一个'Visted Page 2',那么结果应为

Count, Activity, dDate
2 'Visited Home Page' 2018-02-23
2 'Visited Page 1' 2018-02-23
0 'Visited Page 2' 2018-02-23

1 'Visited Page 1' 2018-02-24
0 'Visited Home Page' 2018-02-24
0 'Visited Page 2' 2018-02-24

如果当天什么都没发生,我可能会因为没有带回任何结果而逃脱。虽然我想看看它会如何完成。

2 个答案:

答案 0 :(得分:1)

如果必须仅针对2种颜色进行此操作,最简单的方法是使用Select sum(Color='red') as count, 'red' as color, myDate from colors group by myDate union all Select sum(Color='blue') as count, 'blue' as color, myDate from colors group by myDate 进行条件聚合。

Select count(t.color), c.color, d.myDate
from (select distinct color from colors) c
cross join (select distinct myDate from colors) d
left join colors t on t.color=c.color and t.myDate=d.myDate 
group by c.color, d.myDate

另一种选择是使用交叉连接和计数为每个日期生成所有颜色。

select sum(color='red') as red,sum(color='blue') as blue,myDate
from colors
group by myDate

如果可以将所有计数放在一行上,请使用条件聚合。

select a.activity,d.dDate,count(t.dDate)
from (select distinct activity from t) a
cross join (select distinct date(dDate) as dDate from t) d
left join t on t.activity=a.activity and date(t.dDate)=d.dDate 
and d.dDate >= '2018-02-01' and d.dDate < '2018-03-01'
group by a.activity,d.dDate

编辑:基于OP的问题更新

from time import time
a = time()

for i in range(1000000):
    26 == 25
print(time() - a)
## 0.040122032165527344
a = time()

for i in range(1000000):
    26 + 25
print(time() - a)
## 0.031081438064575195

答案 1 :(得分:1)

你可以简单地将颜色,myDate结果与计数结果连接起来,例如:

select distinct a.color, a.myDate , ifnull(t.num_color,0)
from colors a
left join ( 
  select color, myDate, count(*) num_color
  from colors
  group by color, myDate
) t on t.color= a.color AND t.myDate = a.myDate 

例如,你可以

之间的条款
select distinct a.color, a.myDate , ifnull(t.num_color,0)
from colors a
left join ( 
  select color, myDate, count(*) num_color
  from colors
  group by color, myDate
) t on t.color= a.color AND t.myDate = a.myDate 
        and date(a.myDate) between ( '2017-01-01') and ('2018-02-23')

并查看您更新的问题示例

你可以使用带有计数的sublect来加入不同的活动和ddate

select distinct m.activity, date(m.dDate)
from my_table m
left join  (
  select activity,  date(dDate), count(*) as my_count 
  from my_table 
  group by activity,  date(dDate) as my_date
) t on t.activity = m.activity and date(m.dDate) = t.my_date