5

SQL 查询相对较新。我可以从一个简单的查询中成功获得结果,该查询显示了客户编号和开票的总美元金额,从最高美元金额到最低金额。我还想显示客户名称。客户名称[Name]与客户编号一起在另一个表中,但客户编号的列名称不同,即。Table 1[Bill-to Customer No_]Table 2只是[No_]。如何获取信息Table 2以与客户编号显示在同一行?

4

3 回答 3

4
SELECT [Bill-to Customer No_], [Invoice Amount] AS amt, [Name]
FROM Table1 t1 JOIN Table2 t2
ON t1.[Bill-to Customer No_] = t2.[No_]
ORDER BY amt DESC;

我还没有掌握你的列名,但希望你明白。

编辑:(根据您的新查询)

SELECT [Sell-to Customer No_], [Name], SUM([Amount]) as "Total Dollars Spent" 
FROM [Table 1 - LIVE$Sales Invoice Line] a JOIN [Table 2 - LIVE$Customer] b
ON a.[Sell-to Customer No_] = b.[No_]
WHERE [Source Code] = 'RENTAL' and [Sell-to Customer No_] != 'GOLF' 
GROUP BY [Sell-to Customer No_], [Name]
ORDER BY SUM([Amount]) DESC;

您还需要添加[Name]GROUP BY子句中。请记住SELECTGROUP BY除非它正在由一个组函数[Amount]处理,例如正在由SUM().

于 2013-09-13T20:04:35.057 回答
3
SELECT 
       [bill-to Customer No_]
       ,customer_name 
FROM table1 AS a 
INNER JOIN table2 AS b on a.[bill-to Customer No_]=b.No_
于 2013-09-13T19:58:21.353 回答
1
select Bill-to, CustomerNo_ ,customer_name 
from Table1 a 
join Table2 b on a.CustomerNo_ = b.No_
于 2013-09-13T20:02:10.563 回答