如何选择每个月都有记录的所有客户? 在这里,将选择客户,因为他/她每个月都有记录。
+----------+--------+-------+
| customer | date | spend |
+----------+--------+-------+
| a | 201801 | 5.5 |
| b | 201801 | 16 |
| c | 201801 | 7 |
| a | 201802 | 3.2 |
| b | 201802 | 4.6 |
| a | 201803 | 3 |
| c | 201803 | 1.2 |
+----------+--------+-------+
所需的输出:
+----------+--------+-------+
| customer | date | spend |
+----------+--------+-------+
| a | 201801 | 5.5 |
| a | 201802 | 3.2 |
| a | 201803 | 3 |
+----------+--------+-------+
答案 0 :(得分:0)
我会这样:
/**
* @ORM\ManyToMany(targetEntity="Productgroup", inversedBy="fields")
* @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
*/
private $productgroup;
public function getProductgroup()
{
return $this->productgroup;
}
public function setProductgroup($productgroup): self
{
$this->productgroup = $productgroup;
return $this;
}
public function __construct()
{
$this->productgroup = new ArrayCollection();
}
答案 1 :(得分:0)
假设所有行都有日期为2018,
select customer from customers
group by customer
having count(distinct date) = 12
您可以获取2018年每个月都有行的所有客户。
因此,您需要将其加入主表:
select c.* from (
select customer from customers
group by customer
having count(distinct date) = 12
) t inner join customers c
on c.customer = t.customer
order by c.customer, c.date
如果还有其他年份的日期:
select c.* from (
select customer from customers
where substr(date, 1, 4) = '2018'
group by customer
having count(distinct date) = 12
) t inner join customers c
where substr(c.date, 1, 4) = '2018'
on c.customer = t.customer
order by c.customer, c.date
答案 2 :(得分:0)
假设每个客户每月只有一条记录:
select t.*
from (select t.*, count(*) over (partition by customer) as cnt
from t
) t cross join
(select count(distinct date) as cnt
from t
) tt
where t.cnt = tt.cnt;