在2个表中查找不同的值不起作用

时间:2019-06-27 03:12:40

标签: sql sql-server join distinct dbeaver

我有2个表(我只显示了选择字段):

表A:

    <LOCATION CODE>
    ACT   
    NSWNS    
    OSUSA
    Parra
    Parra
    Parra
    ACT
    ACT

表B(查找表):

<CODE>    <DESCRIPTION>
ACT        Australian Capital Territory
NSWNS      New South Whales - North South
OSUSA      Overseas - USA
Parra      Parramatta

“描述”的类型为“文本”。 “代码”的类型为“ varchar”。 “位置”是t_code_key的一种。

我试图在这两个表上执行联接,以便从表A接收唯一的“代码”项列表,并在表B中获得完整的描述名称。

我想得到这样的东西:

<CODE>   <DESCRIPTION>
ACT      Australian Capital Territory
Parra    Parramatta
NSW      New South Whales

我尝试了以下两个查询,但是我并没有碰到任何运气错误:

select mp.location, ct.description
from 
    (select DISTINCT CAST(description AS VARCHAR(MAX)) AS ct FROM code_table)
    full outer JOIN
    (select DISTINCT location from mailout_profile) as mp 
        on ct.code = mp.location;

select mp.location, ct.description
from 
    (select DISTINCT convert(VARCHAR(MAX), description) AS ct FROM code_table)
    full outer JOIN
    (select DISTINCT location from mailout_profile) as mp 
        on ct.code = mp.location;

我遇到的错误是:

SQL Error [156] [S0001]: Incorrect syntax near the keyword 'full'.
  Incorrect syntax near the keyword 'full'.
  Incorrect syntax near the keyword 'full'.

感谢帮助,谢谢!! :)

2 个答案:

答案 0 :(得分:1)

请尝试以下操作:

WITH r1 (loc) AS
    (SELECT DISTINCT LOCATION FROM [mailout_profile])
SELECT Code, Description FROM r1
FULL OUTER JOIN [code_table] ct ON r1.loc =  ct.Code

enter image description here

答案 1 :(得分:1)

您可以使用以下查询获得所需的结果-

样本数据

const reply1 = { type: ActivityTypes.Message };
const reply2 = { type: ActivityTypes.Message };

const yesBtn = { type: ActionTypes.PostBack, title: 'Yes', value: 'Yes' };
const noBtn = { type: ActionTypes.PostBack, title: 'No', value: 'No' };

const card1 = CardFactory.heroCard(
  'Do you want to continue',
  undefined,
  [yesBtn]
);
const card2 = CardFactory.heroCard(
  undefined,
  undefined,
  [noBtn]
);

reply1.attachments = [card1];
reply2.attachments = [card2];

await stepContext.context.sendActivities([reply1, reply2])

查询

Declare @TableA table
(LocationCode varchar(20))

Insert into @TableA
values

    ('ACT'   ),
    ('NSWNS' ),   
    ('OSUSA'    ),
    ('Parra'    ),
    ('Parra'    ),
    ('Parra'    ),
    ('ACT'  ),
    ('ACT'  )


Declare @TableB table
(Code varchar(20), Description varchar(100))

Insert into @TableB
values
('ACT'    ,  'Australian Capital Territory'   ),
('NSWNS'  ,  'New South Whales - North South' ),
('OSUSA'  ,  'Overseas - USA'                 ),
('Parra'  ,  'Parramatta'                     )