我正在使用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
的任何建议。
提前致谢, 路易斯
答案 0 :(得分:1)
我认为Tbl_Customer_Test插入子句中的错误 - 你介意H17933而不是117933吗?
如果是这种情况,那么你必须像这样编写smth
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,'_'),'')