1

我正在使用 SQL Server,我需要查询方面的帮助。

这是场景:

我有 2 张桌子RosterCustomer.

PK [ID]存在于两个表中。

CREATE  TABLE [dbo].[Tbl_Roster_Test](
    [CONCATENATE_NUMBER] [varchar](100) NOT NULL,
    [CONCATENATE_NAME] [varchar](100) NULL,
    [CUST_ID] [varchar](100) NULL,
    [CUST_NAME] [varchar](100) NULL,
    [ID] [varchar](100) NOT NULL,
    [ID_NAME] [varchar](100) NULL,
) ON [PRIMARY]

CREATE  TABLE [dbo].[Tbl_Customer_Test](
    [ID] [varchar](100) NOT NULL,
    [CUST_ID_1] [varchar](100) NULL,
    [CUST_ID_2] [varchar](100) NULL,
) ON [PRIMARY]

INSERT INTO [BOSSTest].[dbo].[Tbl_Roster_Test]
([CONCATENATE_NUMBER],[CONCATENATE_NAME],[CUST_ID],[CUST_NAME],[ID],[ID_NAME])
SELECT 'US193085','UNIVERSITY OF ARIZONA','','','US193085','UNIVERSITY OF ARIZONA'
UNION ALL
SELECT 'US193085117933','UNIVERSITY OF ARIZ','117933','UNIVERSITY OF ARIZ','US193085','UNIVERSITY OF ARIZONA'
UNION ALL
SELECT 'US193085T22346','UNIVERSITY OF AZ','T22346','UNIVERSITY OF AZ','US193085','UNIVERSITY OF ARIZONA'

INSERT into [BOSSTest].[dbo].[Tbl_Customer_Test]
([ID],[CUST_ID_1],[CUST_ID_2])
SELECT 'US193085','117933',''
UNION ALL
SELECT 'US193085','T22346',''
UNION ALL
SELECT 'US193085','T22346','117933'
UNION ALL
SELECT 'US193085','',''

逻辑...

In [Tbl_Roster_Test]
IF [CUST_ID] = ''  THEN [CONCATENATE_NAME] = [ID_Name]
ELSE
IF [CUST_ID] <> '' THEN [CONCATENATE_NAME] = [Cust_ID_Name]

知道了这一点,现在我正在尝试使用上述逻辑使用 [ID] 链接两个表。

如果有 [Cust_ID] 则选择 [Cust_ID_Name],如果没有则选择 [ID_Name]

有3种情况:

1. There is no data in [Cust_ID]
2. There is data in [CUST_ID_1]
3. There is no data in [CUST_ID_1] and There is data in [CUST_ID_2]

当我执行这个查询...

SELECT 
 C.[ID]
,C.[CUST_ID_1]
,C.[CUST_ID_2]
,(SELECT TOP 1 ISNULL([CONCATENATE_NAME],'') FROM BOSSTest.dbo.Tbl_Roster_Test R WHERE C.[ID] = R.[ID]) AS [Customer_Name]

FROM [BOSSTest].[dbo].[Tbl_Customer_Test] C

这就是我得到的。

+--------+------  -+---------+---------------------+
|      ID|CUST_ID_1|CUST_ID_2|        Customer_Name|
+--------+------  -+---------+---------------------+
|US193085|   117933|         |UNIVERSITY OF ARIZONA|
|US193085|   T22346|         |UNIVERSITY OF ARIZONA|
|US193085|   T22346|   117933|UNIVERSITY OF ARIZONA|   
|US193085|         |         |UNIVERSITY OF ARIZONA| 
+--------+---------+---------+---------------------+

然后我尝试使用子查询...

SELECT 
 [ID]
,[CUST_ID_1]
,[CUST_ID_2]
,CASE WHEN [CUST_ID_1] <> 
THEN (SELECT TOP 1 CONVERT(INT, CASE WHEN IsNumeric(CONVERT(VARCHAR(100),[CONCATENATE_NAME])) = 1 then CONVERT(VARCHAR(100),[CONCATENATE_NAME]) else 0 End) FROM BOSSTest.dbo.Tbl_Roster_Test R WHERE [CUST_ID_1] = R.[CUST_ID])
ELSE 0 END AS [Customer_Name]

FROM (

    SELECT 
     C.[ID]
    ,C.[CUST_ID_1]
    ,C.[CUST_ID_2]
    ,(SELECT TOP 1 ISNULL([CONCATENATE_NAME],'') FROM BOSSTest.dbo.Tbl_Roster_Test R WHERE C.[ID] = R.[ID]) AS [Customer_Name]

    FROM [BOSSTest].[dbo].[Tbl_Customer_Test] C

) AS TEST

这就是我得到的。

+--------+------  -+---------+-------------+
|      ID|CUST_ID_1|CUST_ID_2|Customer_Name|
+--------+------  -+---------+-------------+
|US193085|   117933|         |         NULL|
|US193085|   T22346|         |            0|
|US193085|   T22346|   117933|            0|   
|US193085|         |         |            0| 
+--------+---------+---------+-------------+

我曾经CONVERT避免出现以下错误Syntax error converting the varchar value 'UNIVERSITY OF AZ' to a column of data type int.

但最后,这是我真正需要的。

+--------+------  -+---------+---------------------+
|      ID|CUST_ID_1|CUST_ID_2|        Customer_Name|
+--------+------  -+---------+---------------------+
|US193085|   117933|         |   UNIVERSITY OF ARIZ|
|US193085|   T22346|         |     UNIVERSITY OF AZ|
|US193085|   T22346|   117933|     UNIVERSITY OF AZ|   
|US193085|         |         |UNIVERSITY OF ARIZONA| 
+--------+---------+---------+---------------------+

关于如何使用 3 个案例运行此逻辑并充分利用CONVERT.

在此先感谢,路易斯

4

1 回答 1

1

我认为这是 Tbl_Customer_Test 插入子句中的错误-您介意 H17933 而不是 117933 吗?

如果是这种情况,那么您必须像这样写

SELECT CT.*,RT.CONCATENATE_NAME
FROM [dbo].[Tbl_Customer_Test] CT
left  join  [dbo].[Tbl_Roster_Test] RT 
on RT.CONCATENATE_NUMBER = 
CT.[ID] + isnull(nullif(ct.[CUST_ID_1],''), ct.[CUST_ID_2])

答案是

 ID         CUST_ID_1   CUST_ID_2   CONCATENATE_NAME
 ---------- ----------- ----------- ---------------------
 US193085   H17933                  UNIVERSITY OF ARIZ
 US193085   T22346                  UNIVERSITY OF AZ
 US193085   T22346       H17933     UNIVERSITY OF AZ
 US193085                           UNIVERSITY OF ARIZONA

UPD

由于添加了评论(我们吃了第一个符号:)

SELECT CT.*,RT.CONCATENATE_NAME
FROM [dbo].[Tbl_Customer_Test] CT
join  [dbo].[Tbl_Roster_Test] RT 
on RT.CONCATENATE_NUMBER like
CT.[ID] + isnull(stuff(isnull(nullif(ct.[CUST_ID_1],''),ct[CUST_ID_2]),1,1,'_'),'')
于 2016-10-27T23:31:12.377 回答