我正在使用 SQL Server,我需要查询方面的帮助。
这是场景:
我有 2 张桌子Roster
和Customer
.
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
.
在此先感谢,路易斯