我试图找到一个查询,该查询将为我提供在同一个月内与2个不同实体进行交易的客户数量。换句话说,在同一个月内与company_a和company_b进行交易的customer_ids。以下是我到目前为止的情况:
SELECT Extract(year FROM company_a_customers.transaction_date)
|| Extract(month FROM company_a_customers.transaction_date) AS
payment_month,
Count(UNIQUE(company_a_customers.customer_id))
FROM (SELECT *
FROM my_table
WHERE ( merchant_name LIKE '%company_a%' )) AS company_a_customers
INNER JOIN (SELECT *
FROM my_table
WHERE ( merchant_name = 'company_b' )) AS
company_b_customers
ON company_a_customers.customer_id =
company_b_customers.customer_id
GROUP BY Extract(year FROM company_a_customers.transaction_date)
|| Extract(month FROM company_a_customers.transaction_date)
问题在于,这给了我一个月与A公司交易的所有客户的总数,这些客户也曾与公司B进行过交易。
如果我将它减少到特定月份,它显然会给我正确的重叠,因为查询只获取该月的ID:
SELECT Extract(year FROM company_a_customers.transaction_date)
|| Extract(month FROM company_a_customers.transaction_date) AS
payment_month,
Count(UNIQUE(company_a_customers.customer_id))
FROM (SELECT *
FROM my_table
WHERE ( merchant_name LIKE '%company_a%' )
AND transaction_date >= '2017-06-01'
AND transaction_date <= '2017-06-30') AS company_a_customers
INNER JOIN (SELECT *
FROM my_table
WHERE ( merchant_name = 'company_b' )
AND transaction_date >= '2017-06-01'
AND transaction_date <= '2017-06-30') AS
company_b_customers
ON company_a_customers.customer_id =
company_b_customers.customer_id
GROUP BY Extract(year FROM company_a_customers.transaction_date)
|| Extract(month FROM company_a_customers.transaction_date)
如何在一个查询中执行此操作,以获取在给定月份内与两家公司进行交易的客户的每月总计?
期望的结果:输出第二个查询,但是对于数据库中的每个月。换句话说:
2017年1月:xx,xxx重叠客户 2017年2月:xx,xxx重叠客户 2017年3月:xx,xxx重叠客户
非常感谢。
答案 0 :(得分:1)
您应该只需计算每个客户ID每月的merchant_names数量,就可以在一个查询中获得所需的结果。使用HAVING&gt; 1将仅向您显示具有两者的交易的客户(如果有更多匹配的'%company_a%',则更多)。
public class Test{
public static void main(String[] args){
object a = new object(3);
object b = new object(0);
a = b;
System.out.println(a.var);
}
}
public class object{
protected int var;
public object(int x){
var = x;
}
}
答案 1 :(得分:1)
您可以简单地计算两者的年/月,然后将其添加为连接条件,但这不是非常有效,因为它可能会产生巨大的中间结果。
如果两个商家都使用条件汇总进行交易,您最好检查每个月/客户。然后按月计算:
SELECT payment_month, count(*)
FROM
( SELECT Extract(year FROM transaction_date)
|| Extract(month FROM transaction_date) AS payment_month,
customer_id
FROM my_table
WHERE ( merchant_name LIKE '%company_a%' )
OR ( merchant_name = 'company_b' )
GROUP BY payment_month,
customer_id
-- both merchants within the same months
HAVING SUM(CASE WHEN merchant_name LIKE '%company_a%' THEN 1 ELSE 0 END) > 0
AND SUM(CASE WHEN merchant_name = 'company_b' THEN 1 ELSE 0 END) > 0
) AS dt
GROUP BY 1
你的payment_month
计算是复杂的(并且返回的字符串格式不正确)。
以字符串形式获取年/月:
TO_CHAR(transaction_date, 'YYYYMM')
为数字:
EXTRACT(YEAR FROM transaction_date) * 100
+ EXTRACT(MONTH FROM transaction_date)
或计算月份的第一天:
TRUNC(transaction_date, 'mon')