根据另一个表中的选择查询,从另一个表中添加列

时间:2018-12-10 18:10:38

标签: sql sql-server tsql

在此处寻求一些帮助,以将数据列从另一个表添加到我的查询结果中。

所以两个表上的键都是customer_number

我有一个如下查询:

    SELECT [CUSTOMER_NUMBER]
          ,[CUSTOMER_NAME]
          ,count(*) 'YEARS'
    FROM [DB].[dbo].[tablefromxlsimport$]
    group by  [CUSTOMER_NUMBER]
             ,[CUSTOMER_NAME]

这返回我正在从tableformxlsimport$中寻找的信息,但是我想根据查询的结果从另一个表fullcustomerlist$添加一列数据,该查询的结果将追加一列数据即customer_Current_title

希望我能正确解释

1 个答案:

答案 0 :(得分:0)

请参见以下查询:

 SELECT
      A.[CUSTOMER_NUMBER]
      ,A.[CUSTOMER_NAME]
      ,A.YEARS
      B.customer_Current_title
 FROM
   ( SELECT 
      A.[CUSTOMER_NUMBER]
      ,A.[CUSTOMER_NAME]
      ,count(*) 'YEARS'

  FROM [DB].[dbo].[tablefromxlsimport$] AS A
  group by  [CUSTOMER_NUMBER]
      ,[CUSTOMER_NAME]
  ) AS A
INNER JOIN dbo.fullcustomerlist$ AS B on A.CUSTOMER_NUMBER = B.CUSTOMER_NUMBER