在 SQL 查询中应用 if 逻辑?
Applying if logic in SQL query?
我的数据库table格式如下:
当前年份 = 2017
ID Bought Year
1 A 2016
1 A 2015
2 A 2013
2 B 2015
2 B 2014
3 A 2014
4 A 2014
4 A 2015
4 A 2016
列出过去两年购买产品“A”的客户(在 T-1 和 T-2 年均购买)
答案 - ID : 1 和 4
如何使用
在 R 中制定查询
lasttwoyearcustlist=DBgetQuery(conn, "Query")
一种方法是根据一个In计算不同年份的数量
select id
from my_table
where bought = 'A'
and year in ( 2016, 2015)
group by id
having count(distinct year) =2
我的数据库table格式如下:
当前年份 = 2017
ID Bought Year
1 A 2016
1 A 2015
2 A 2013
2 B 2015
2 B 2014
3 A 2014
4 A 2014
4 A 2015
4 A 2016
列出过去两年购买产品“A”的客户(在 T-1 和 T-2 年均购买)
答案 - ID : 1 和 4
如何使用
在 R 中制定查询lasttwoyearcustlist=DBgetQuery(conn, "Query")
一种方法是根据一个In计算不同年份的数量
select id
from my_table
where bought = 'A'
and year in ( 2016, 2015)
group by id
having count(distinct year) =2