[英]How to join two sql tables when the common column has different names but information is the same in both tables
Relatively new to SQL querying. SQL查询相对较新。 I can successfully get results from a simple query that shows a customer number and a total dollars invoiced sorted highest dollar amount to lowest. 我可以成功地从一个简单的查询中获得结果,该查询显示客户编号和发票总额,将最高美元金额排序到最低。 I want to also display the customer name. 我还要显示客户名称。 The customer name, [Name]
, is in another table along with the customer number but the column name for customer number is different, ie. 客户名称[Name]
与客户编号一起位于另一个表中,但客户编号的列名称不同,即。 Table 1
is [Bill-to Customer No_]
and Table 2
is just [No_]
. Table 1
是[Bill-to Customer No_]
, Table 2
仅为[No_]
。 How would I get the information from Table 2
to display in the same row with the customer number? 如何从Table 2
获取与客户编号显示在同一行中的信息?
SELECT
[bill-to Customer No_]
,customer_name
FROM table1 AS a
INNER JOIN table2 AS b on a.[bill-to Customer No_]=b.No_
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;
I haven't grasped your column names yet, but hope you get the idea. 我还没有掌握你的专栏名称,但希望你能得到这个想法。
EDIT : (as per your new query) 编辑 :(根据您的新查询)
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;
You need to add [Name]
to the GROUP BY
clause as well. 您还需要将[Name]
添加到GROUP BY
子句中。 Remember you cannot SELECT
a column that's not a part of GROUP BY
unless it's being processed by a group function like [Amount]
is being processed by SUM()
. 请记住,除非正由SUM()
处理[Amount]
等组函数处理它,否则不能SELECT
不属于GROUP BY
的列。
select Bill-to, CustomerNo_ ,customer_name
from Table1 a
join Table2 b on a.CustomerNo_ = b.No_
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.