我有一张表格列出了所有供应商以及我付款的时间。我想估算每个供应商支付的总金额。目前,特定供应商可能有25行,在不同日期支付不同金额。我只想为每个vendor_code,供应商名称以及支付给他们的总金额显示一行。
到目前为止,我有这个:
function addDefinition(object, payload) {
var newObject = Object.assign({}, object);
if(!newObject[payload.guid]){
newObject[payload.guid] = [];
}
newObject[payload.guid].push(payload);
return newObject;
}
目前,我收到此错误:select s.vendor_code,
a.address_name,
a.addr2, a.addr3,
a.city, a.state,
a.postal_code, a.vend_class_code,
date_applied, SUM(amt_net) as 'Total'
from appyhdr_all s, apmaster a
where s.vendor_code = a.vendor_code
and s.pay_to_code = a.pay_to_code
and void_flag=0
and s.payment_type <> 4
答案 0 :(得分:2)
这应该有效:
select s.vendor_code
, a.address_name
, a.addr2
, a.addr3
, a.city
, a.state
, a.postal_code
, a.vend_class_code
, SUM(amt_net) as 'Total'
from appyhdr_all s
join apmaster a
on s.vendor_code = a.vendor_code
where 1 = 1
and s.pay_to_code = a.pay_to_code
and void_flag=0
and s.payment_type <> 4
group
by s.vendor_code
, a.address_name
, a.addr2
, a.addr3
, a.city
, a.state
, a.postal_code
, a.vend_class_code
答案 1 :(得分:0)
是的,您应该包含group by
子句并将所有选定的非聚合属性放在那里:
select s.vendor_code,
a.address_name,
a.addr2, a.addr3,
a.city, a.state,
a.postal_code, a.vend_class_code,
date_applied, SUM(amt_net) as 'Total'
from appyhdr_all s, apmaster a
where s.vendor_code = a.vendor_code
and s.pay_to_code = a.pay_to_code
and void_flag=0
and s.payment_type <> 4
group by s.vendor_code, --<--here it is
a.address_name,
a.addr2, a.addr3,
a.city, a.state,
a.postal_code, a.vend_class_code,
date_applied
顺便说一下,您可能想要移动最新的连接语法:
...
from appyhdr_all s
inner join apmaster a
on s.vendor_code = a.vendor_code
and s.pay_to_code = a.pay_to_code
where void_flag=0
...
已编辑到期OP评论:
“当我实现它时,我仍然会获得相同供应商代码的多行。”
这意味着每个供应商的apmaster上有几行。您必须为每个供应商决定一行apmaster
行。这是每个供应商的总数:
with total_by_vendor as (
select s.vendor_code, SUM(amt_net) as 'Total'
from appyhdr_all s inner join apmaster a
on s.vendor_code = a.vendor_code
and s.pay_to_code = a.pay_to_code
where void_flag=0
and s.payment_type <> 4
group by s.vendor_code
)
select *
from total_by_vendor;
只需加入apmaster的最新数据:
with total_by_vendor as (
select s.vendor_code, SUM(amt_net) as 'Total'
from appyhdr_all s inner join apmaster a
on s.vendor_code = a.vendor_code
and s.pay_to_code = a.pay_to_code
where void_flag=0
and s.payment_type <> 4
group by s.vendor_code
),
recent_apmaster as (
select vendor_code, a.address_name,
a.addr2, a.addr3,
a.city, a.state,
a.postal_code, a.vend_class_code,
row_number() OVER ( PARTITION BY vendor_code
ORDER BY a.address_name desc ) --put here criteria
as n
from apmaster a
)
select *
from total_by_vendor T inner join recent_apmaster a
on T.vendor_code = a.vendor_code
where n = 1;
答案 2 :(得分:0)
当您使用聚合函数时:
SUM(amt_net)为'总计'
确保列出现在GROUP BY子句中
答案 3 :(得分:0)
先尝试一些简单的事情。如果appyhdr_all s
是供应商,这应该有用。
select s.vendor_code,
SUM(amt_net) as 'Total'
from appyhdr_all s
inner join apmaster a
on s.vendor_code = a.vendor_code
and s.pay_to_code = a.pay_to_code
where void_flag=0
and s.payment_type <> 4
group by s.vendor_code