我有这样的:两种不同的分组层次选择
SELECT
invoice_number, invoice_year, invoice_month, invoice_amount,
payment_year, payment_month, payment_amount
FROM payments_table
结果:
所以我有4张发票。从2015/01两张发票的发票金额合计为900,并从2015/02 2张发票加起来950
我想这样的结果:
所以我想求和invoice_amount
由invoice_year
和invoice_month
仅使用invoice_number
一次。我想总结payment_amount
invoice_year
,invoice_month
,payment_year
和payment_month
。
如果我使用GROUP BY invoice_year, invoice_month, payment_year, payment_month
我获得SUM(payment_amount)
的正确金额,但我收到的金额为SUM(invoice_amount)
。
有什么建议吗?
你的结果对我没有意义。为什么第4个月的付款包含这两个月的发票? –
如果您的数据与您显示的样本类似,则可以只执行SUM(DISTINCT invoice_amount)。 – Munir
@munochtractor这是危险的,你假设两张不同的发票不能有相同的发票金额 – Rabbit