8b9ea8ee
tangwang
feat(recommendati...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
"""
测试修改后的SQL - 验证country_code和customer_type是否能正确获取
"""
import pandas as pd
from db_service import create_db_connection
from config.offline_config import DB_CONFIG
# 创建数据库连接
engine = create_db_connection(
DB_CONFIG['host'],
DB_CONFIG['port'],
DB_CONFIG['database'],
DB_CONFIG['username'],
DB_CONFIG['password']
)
print("="*80)
print("测试修改后的SQL查询")
print("="*80)
# 测试SQL - 使用修改后的简化版本
test_sql = """
SELECT
se.anonymous_id AS user_id,
se.item_id,
se.event AS event_type,
se.create_time,
se.login_id,
se.business_platform AS platform,
bb.countries AS country_code,
bb.sell_channel AS customer_type
FROM
sensors_events se
LEFT JOIN buy_account ba ON ba.id = se.login_id
LEFT JOIN buy_buyer bb ON bb.id = ba.buyer_id
WHERE
se.event IN ('click', 'purchase')
AND se.create_time >= DATE_SUB(NOW(), INTERVAL 7 DAY)
AND se.login_id IS NOT NULL
LIMIT 20
"""
print("\n执行测试SQL...")
df = pd.read_sql(test_sql, engine)
print(f"\n获取到 {len(df)} 条记录")
print("\n" + "="*80)
print("数据预览:")
print("="*80)
# 显示前10条记录
print(df[['user_id', 'platform', 'login_id', 'country_code', 'customer_type']].head(10))
print("\n" + "="*80)
print("数据统计:")
print("="*80)
# 统计country_code和customer_type的非空情况
total_count = len(df)
has_country = df['country_code'].notna().sum()
has_customer = df['customer_type'].notna().sum()
print(f"\n总记录数: {total_count}")
print(f"有country_code的记录: {has_country} ({has_country/total_count*100:.2f}%)")
print(f"有customer_type的记录: {has_customer} ({has_customer/total_count*100:.2f}%)")
print("\n" + "="*80)
print("country_code分布(前10个):")
print("="*80)
country_counts = df['country_code'].value_counts().head(10)
print(country_counts)
print("\n" + "="*80)
print("customer_type分布(前10个):")
print("="*80)
customer_counts = df['customer_type'].value_counts().head(10)
print(customer_counts)
print("\n" + "="*80)
print("按平台统计:")
print("="*80)
for platform in df['platform'].unique():
platform_df = df[df['platform'] == platform]
print(f"\n{platform}:")
print(f" 总记录: {len(platform_df)}")
print(f" 有country_code: {platform_df['country_code'].notna().sum()} ({platform_df['country_code'].notna().sum()/len(platform_df)*100:.2f}%)")
print(f" 有customer_type: {platform_df['customer_type'].notna().sum()} ({platform_df['customer_type'].notna().sum()/len(platform_df)*100:.2f}%)")
print("\n" + "="*80)
print("测试完成!")
print("="*80)
if has_country > 0 and has_customer > 0:
print("\n✅ SQL修改成功!能够正确获取country_code和customer_type")
else:
print("\n❌ 仍然存在问题,无法获取country_code或customer_type")
print("\n可能原因:")
print("1. sensors_events.login_id与buy_account.id无法关联")
print("2. buy_account.buyer_id与buy_buyer.id无法关联")
print("3. buy_buyer.countries或buy_buyer.sell_channel字段为NULL")
|