想要确定运行SQL teradata查询的方法,我可以通过cust_id,acct_num获得构成客户设备计数的最高组合,然后是每个组合总数的平均千瓦数。
所以客户可以拥有1台设备,我需要列出设备和平均千瓦,然后我想要下一个最高的客户设备组合,并需要列出“内部”和“车库”的组合然后获取该组合总数的平均字节数。
源数据样本:
Electric_device Total_Kilowatts CUST_ID ACCT_NUM DEVICE_TYPE
2 75 11556632 1 In House
1 60 11556632 1 In garage
5 25 11556632 2 Outside
6 1155 11556632 2 GPS
结果集将超过100万客户,如下所示:
Count_of_Cust_id Combinations Average_Kilowatts
800,000 In House 75
100,000 In House, In Garage 68
答案 0 :(得分:0)
以下查询按步骤计算各种值:
1对于Cust_ID和Acct_Num
的每个组合,连接设备类型2总计_Kilowatts总结为Cust_ID和Acct_Num的每个组合
3数据按Device_Type组合分组,并获得Cust_ID和Average_Kilowatts的计数。
WITH RECURSIVE device_types (cust_id, acct_num, devices, lvl)
AS
(
SELECT cust_id, acct_num, MIN(device_type), 1
FROM customers
GROUP BY cust_id, acct_num, 1
UNION ALL
SELECT cust_id, acct_num, TRIM(device_type) || ', ' || devices, lvl+1
FROM customers c INNER JOIN device_types dt
ON c.cust_id = dt.cust_id AND c.acct_num = dt.acct_num AND device_type > devices
),
grouped_devices AS
(
SELECT cust_id, acct_num, devices
FROM device_types
QUALIFY RANK() OVER(PARTITION BY cust_id, acct_num ORDER BY lvl DESC) = 1;
),
summed_kilowatts AS
(
SELECT cust_id acct_num, sum(total_kilowatts) total_kilowatts
FROM customers
GROUP BY cust_id acct_num
)
SELECT gd.devices Combinations, COUNT(DISTINCT gd.cust_id) Count_of_Cust_id, SUM(sk.total_kilowatts)/COUNT(DISTINCT gd.cust_id) Average_Kilowatts
FROM grouped_devices gd
INNER JOIN summed_kilowatts sk ON gd.cust_id = sk.cust_id AND gd.acct_num = sk.acct_num
GROUP BY gd.devices
ORDER BY gd.devices;
<强>参考强>:
Concat rows as a single column in Teradata on The Simple Programmer blog