如果在A列中存在值,如果它存在于另一个表中,则如何放置,否则,放在B列中

时间:2015-09-29 20:35:21

标签: sql tsql

我有一个有效的美国州缩写和源表的查找列表表:

分别

状态查找和来源

+-------+    +-----------------+
|States |    |ID | Location    |
+=======+    +=================+
| AK    |    | 1 | Madrid      |
---------    -------------------
| AL    |    | 2 | AK          |
---------    -------------------
| AR    |    | 3 | AR          |
---------    -------------------
| ...   |    | ..| ...         |
---------    -------------------

如何在目标表中创建INSERT语句,这样如果Location是有效状态,它将被放置在State列中,如果是&# 39;不是,它会放在Other Locale栏中吗?

预期目标表格输出

+------------+-----------------
|ID | State  | Other Locale   |
+============+=================
| 1 |        | Madrid         |
-------------------------------
| 2 | AK     |                |
-------------------------------
| 3 | AR     |                |
-------------------------------
| ..| ...    | ...            |
-------------------------------

6 个答案:

答案 0 :(得分:2)

执行LEFT JOIN并检查由于不匹配状态而导致的NULL:

insert into target
select source.id, states.state,
   case when states.state is null then location end as Other_Locale
from source left join states 
  on source.location = states.state

答案 1 :(得分:2)

由于没有人建议使用NULLIF,我可能会把它扔进去。

DECLARE @Location TABLE (Id INT, Location VARCHAR(MAX))
DECLARE @States TABLE (States VARCHAR(MAX))
INSERT INTO @Location VALUES
(1, 'Madrid'), (2, 'AK'), (3, 'AR')

INSERT INTO @States VALUES
('AK'), ('AL'), ('AR')


SELECT  l.Id,
        s.States [State],
        NULLIF(l.Location,s.States) [Other Locale]
FROM    @Location l
        LEFT JOIN @States s ON l.Location = s.States

答案 2 :(得分:0)

您可以使用左连接并根据匹配选择列值:

SELECT so.ID, CASE WHEN States.States IS NOT NULL THEN so.Location END,
       CASE WHEN States.States IS NULL THEN so.Location END
FROM Source so LEFT JOIN States
  ON States.States = so.Location

请参阅SQLFiddle

当然,您可以将其插入到您想要的任何表格中: - )

答案 3 :(得分:0)

您想使用案例陈述;像这样(大致)

INSERT  INTO dbo.ResultsTable
        SELECT  CASE WHEN EXISTS ( SELECT   1
                                   FROM     StatesTable
                                   WHERE    States = l.Location ) THEN l.Location
                     ELSE NULL
                END AS [State] ,
                CASE WHEN NOT EXISTS ( SELECT   1
                                       FROM     StatesTable
                                       WHERE    States = l.Location ) THEN l.Location
                     ELSE NULL
                END AS [Other Locale]
        FROM    dbo.LocationsTable l

答案 4 :(得分:0)

我会做这样的事情

INSERT INTO Target (State , Other Locale) 
SELECT Location , ""  FROM Source Where Location IN 
(SELECT States FROM States) UNION
SELECT "" , Location FROM Source Where Location NOT IN 
(SELECT States FROM States) ;

根据数据库或配置的不同,您可能需要使用AS命名列,如:

INSERT INTO Target (State , Other Locale) 
    SELECT Location AS "State" , "" AS " Other Locale"
 FROM Source Where Location IN (SELECT States FROM States) UNION
    SELECT "" AS "State", Location AS " Other Locale" 
FROM Source Where Location NOT IN (SELECT States FROM States) ;

答案 5 :(得分:0)

这可能是漫长的道路,但是对于我来说,阅读和阅读更容易。解决:

CREATE PROCEDURE spLocations (@inLocation varchar(255))
AS
-- declare local variables
declare @col_name nvarchar(255) = ''
declare @location nvarchar(255) = ''

SELECT @location = [colState] from tblStates where [colState] = @inLocation;

SET @col_name = CASE @location 
                    WHEN Null     
                        THEN 'State'
                    ELSE 'Other Locale'
                END;
INSERT INTO targetTBL ([' + @col_name + ']) VALUES (@inLocation);
GO;