简体   繁体   English

当公共列具有不同的名称但两个表中的信息相同时,如何连接两个sql表

[英]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.

相关问题 如何连接两个表中列名相同但行值不同的两个 SELECT 查询 - How to Join two SELECT queries having same column names but different row values in both tables SQL 如何连接SQL中列名相同但数据不同的两个表 - SQL how to join two tables with same column names but different data in SQL 使用SQL Server 2008,如何连接具有相同列名但具有不同数据类型的两个表 - Using SQL Server 2008, how to join two tables with same column names, but different datatypes 两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同) - two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2) 如何在一个列上连接两个表,这两个表在两个表中具有相同的名称? - How do I join two tables ON a column, that has the same name in both tables? 连接具有不同列名和相同行的两个表 - Join two tables with different column names and same rows Linq Join 2表,列名称不同,但两个表中列的值相同 - Linq Join 2 tables, the column names are different but the values in the column are the same in both tables 取消列名相同的两个表之间的联接 - sql - Unpivot a join between two tables with the same column names SQL内连接两个具有相同列名的表 - SQL inner join two tables with the same column names SQL连接两个表,在Python上具有相同的列名 - SQL join for two tables, with same column names on Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM