SQL查询帮助获取字段“custname”而不是“custnmbr”

时间:2012-10-26 00:53:38

标签: sql

我一直在试图弄清楚如何修改我的查询以获得CUSTNAME和' SALES TOTAL'要显示的列。以下是当前的查询。

select c.CUSTNMBR, SUM(c.ortrxamt) as 'Sales Total'
from rm30101 c
inner join
(
select a.CUSTNMBR, b.CUSTNAME
from RM30101 a
inner join 
RM00101 b on b.CUSTNMBR = a.CUSTNMBR
where b.ZIP = '99502' and (a.DOCDATE between '2011-01-01' and '2012-10-25')
group by a.CUSTNMBR,b.CUSTNAME
)
x on c.CUSTNMBR = x.CUSTNMBR
group by c.CUSTNMBR
order by [Sales Total] desc

我不知道如何将列RM00101.custname输入到第一个选择行而不会收到错误。我试过更换“c.custnmbr”'与' x.custname'并按x.custname进行分组,但销售总计结果是关闭的。 任何帮助将不胜感激!谢谢!

-Jae

1 个答案:

答案 0 :(得分:1)

select a.CUSTNMBR, b.CUSTNAME, SUM(a.ortrxamt) as 'Sales Total'
from RM30101 a
inner join RM00101 b on b.CUSTNMBR = a.CUSTNMBR
where b.ZIP = '99502' 
   and (a.DOCDATE between '2011-01-01' and '2012-10-25')
group by a.CUSTNMBR,b.CUSTNAME
order by [Sales Total] desc