我正在尝试确定两类客户之间每日平均的金额:亚洲和非亚洲客户。有3个单独的表(附加的图像文件显示样本),
user_description
account_description
transaction_description
包含有关客户类型的数据,无论他们是亚洲人还是非亚洲人,以及他们在特定交易日期支付的金额。长话短说,我有正确的结果,但我的输出不太理想。让我们举个例子,示例模式(下面是填充此示例的脚本,以及图像文件。)具体来说,我的所需输出是:
transaction_date Average Amount per Asian Customer Average amount per Non-Asian Customer
10/12/1998 31.66 35
10/31/2013 0 30
因此,例如,此输出表明,对于10/31/1998日期,亚洲客户支付的平均金额为31.66美元,而非亚洲客户的平均金额为35美元。
我的当前输出显示正确的信息,但它也会按日期拆分平均数量!所以,我得到的输出如下所示:
transaction_date Average Amount per Asian Customer Average amount per Non-Asian Customer
10/12/1998 31.66 0
10/12/1998 0 35
10/31/2013 0 30
我的问题是:如果我的代码已经有双连接,我如何格式化结果输出以显示类似于所需的输出?我是否需要完全重写代码才能获得所需的视图,或者有没有办法在没有DDL的情况下(创建临时视图等)?
我已经尝试过在桌面上进行双重连接或转移,但没有运气。我正在使用SQLfiddle作为我的SQL编辑器btw,用于测试。enter image description here
非常感谢任何帮助。
附录
代码/ SQL查询
select transaction_date,
(case when (asian_status = 'No')
then sum(amount)/count(distinct(transactions.customer_id))
else 0
end) as "Average Amount per Non-Asian Customer",
(case when (asian_status = 'Yes')
then sum(amount)/count(distinct(transactions.customer_id))
else 0
end) as "Average Amount per Asian Customer"
from
(user_description join account_description on
user_description.account_id = account_description.account_id) join
transactions on transactions.customer_id = user_description.customer_id
group by transaction_date, asian_status
order by transaction_date;
人口守则:
CREATE TABLE user_description (customer_id INTEGER, account_id INTEGER);
CREATE TABLE account_description (account_id INTEGER, credit_source TEXT, asian_status TEXT);
CREATE TABLE transactions (transaction_date DATE, customer_id INTEGER, service_type TEXT, amount INTEGER);
INSERT INTO user_description (customer_id, account_id) VALUES (1, 15);
INSERT INTO user_description (customer_id, account_id) VALUES (2, 17);
INSERT INTO user_description (customer_id, account_id) VALUES (3, 23);
INSERT INTO user_description (customer_id, account_id) VALUES (4, 28);
INSERT INTO user_description (customer_id, account_id) VALUES (5, 28);
INSERT INTO user_description (customer_id, account_id) VALUES (6, 23);
INSERT INTO account_description (account_id, credit_source, asian_status) VALUES (15, 'Savings', 'Yes');
INSERT INTO account_description (account_id, credit_source, asian_status) VALUES (17, 'Checkings', 'No');
INSERT INTO account_description (account_id, credit_source, asian_status) VALUES (23, 'Savings', 'No');
INSERT INTO account_description (account_id, credit_source, asian_status) VALUES (28, 'Debit', 'Yes');
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('2011-10-14', 2, 'ATM', 30);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 2, 'bank', 25);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 1, 'online', 45);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 1, 'online', 10);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 3, 'ATM', 20);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 4, 'bank', 15);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 5, 'ATM', 25);
INSERT INTO transactions (transaction_date, customer_id, service_type, amount) VALUES ('1998-10-12', 6, 'bank', 60);