我需要查询的结果是:
Company 1 | SUM of amount_invoice
Company 2 | SUM of amount_invoice
...
我得到86,000行-每个发票金额分别显示。如果不必先将结果转换为excel即可按我的需要格式化结果,那么填充预设图表将非常方便。
我已经查看了数据透视表的帮助字段,但只是没有使它起作用。
SELECT company,
CAST(SUM(amount_invoice) AS DECIMAL(18,2))
FROM invoices
inner join debtors on debtor_id=debtors.id
WHERE invoice_date BETWEEN '2019-07-01 00:00:00.000' AND '2019-07-31 00:00:00.000'
非常感谢任何愿意帮助我添加正确格式化所需内容的人。
答案 0 :(得分:2)
使用分组依据
SELECT company,
CAST(SUM(amount_invoice) AS DECIMAL(18,2))
FROM invoices
inner join debtors on debtor_id=debtors.id
WHERE invoice_date BETWEEN '2019-07-01 00:00:00.000' AND '2019-07-31 00:00:00.000'
GROUP BY company
答案 1 :(得分:0)
一些建议:
BETWEEN
用于日期/时间比较。我建议:
SELECT d.company,
CAST(SUM(i.amount_invoice) AS DECIMAL(18,2))
FROM invoices i INNER JOIN
debtors d
ON i.debtor_id = d.id
WHERE i.invoice_date >= '2019-07-01' AND
i.invoice_date < '2019-08-01';
亚伦·伯特兰(Aaron Bertrand)的blog很好,解释了为什么不想在两者之间使用。