来点函数
MAX()
MIN()
AVG()
SUM()
COUNT()
选择invoice_total表内面值最大的发票
SELECT MAX(invoice_total)
FROM invoices
SELECT
MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total) AS total,
COUNT(invoice_total) AS number_of_invoices,
COUNT(payment_date) AS count_of_payments, # 返回非空payment dates记录数
COUNT(*) AS total_records
FROM invoices
选择下半年发票
SELECT
MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total) AS total,
COUNT(invoice_total) AS number_of_invoices,
COUNT(payment_date) AS count_of_payments, # 返回非空payment dates记录数
COUNT(*) AS total_records
FROM invoices
WHERE invoice_date > ‘2019-07-01’
选择下半年发票,且client_id不重复
SELECT
MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total) AS total,
COUNT(invoice_total) AS number_of_invoices,
COUNT(payment_date) AS count_of_payments, # 返回非空payment dates记录数
COUNT(DISTINCT client_id) AS total_records
FROM invoices
WHERE invoice_date > ‘2019-07-01’
SELECT
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_except
‘First half of 2019’ AS date_range
FROM invoices
WHERE date
BETWEEN ‘2019-01-01’ AND ‘2019-06-30’
UNION
SELECT
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_except
‘Second half of 2019’ AS date_range
FROM invoices
UNION
WHERE date
BETWEEN ‘2019-07-01’ AND ‘2019-12-31’
UNION
SELECT
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_except
‘total’ AS date_range
FROM invoices
WHERE date
BETWEEN ‘2019-01-01’ AND ‘2019-12-31’
