我是SQL和phpmyadmin的新手,所以如果不清楚我会道歉。我试图使用查询作为来自不同查询的数据源,但我从SELECT获得解析错误。这是我想要做的。
select CUS_CODE,
COUNT(LINE.INV_NUMBER) as "Number of Invoices",
AVG("Invoice Total") as "Average Invoice Amount",
MAX("Invoice Total") as "Max Invoice Amount",
MIN("Invoice Total") as "Min Invoice Amount",
Sum("Invoice Total") as "Total Customer Purchases"
from (
select CUS_CODE,
LINE.INV_NUMBER as INV_NUMBER,
Sum(LINE.LINE_UNITS * LINE.LINE_PRICE) as "Invoice Total"
from INVOICE,
LINE
where INVOICE.INV_NUMBER = LINE.INV_NUMBER
group by CUS_CODE,
LINE.INV_NUMBER
)
group by CUS_CODE;
答案 0 :(得分:0)
试试这个:
select cus_code,
count(inv_number) as number_of_invoices,
avg(invoice_total) as average_invoice_amount,
max(invoice_total) as max_invoice_amount,
min(invoice_total) as min_invoice_amount,
sum(invoice_total) as total_customer_purchases
from (
select cus_code,
l.inv_number,
sum(l.line_units * l.line_price) as invoice_total
from invoice i
join line l on i.inv_number = l.inv_number
group by cus_code,
l.inv_number
) t
group by cus_code;
很少有评论:
LINE.INV_NUMBER
在子查询外无效。直接使用INV_NUMBER
_