您好我正在使用SQL SERVER 2008R2并且具有生成以下输出的表
**Usertable**
USERID Name LINKID
1 abc NULL
2 pqr NULL
3 str NULL
4 xyz NULL
**PhoneTable**
USERID PhoneNumber
1 123
2 123
3 12345
4 34567
**EmailTable**
USERID EmailID
1 abc@gmail.com
2 pqr@gmail.com
3 abc@gmail.com
4 xyz@gmail.com
我的requiremnet是,如果 PhoneTable 中的记录具有相同的PhoneNumber
,那么应该为 USERTABLE LINKID更新相同的USERID
>
如果用户在 EmailTable 中具有相同的EmailID
,则应在 USERTABLE <中为LINKID
更新相同的USERID
/ p>
我对UserTable的预期输出是,
USERID Name LINKID
1 abc 1
2 pqr 1
3 str 1
4 xyz NULL
用户ID 4不具有相同的phonenNumber或EmailID,因此它应保持为空。 建议我使用查询或程序来获取结果。谢谢。
答案 0 :(得分:2)
First you get user id for same phone number and email id with help of CTE
and update usertable based on CTE.
CREATE TABLE #Usertable(USERID INT, Name VARCHAR(100), LINKID INT)
CREATE TABLE #PhoneTable(USERID INT, PH VARCHAR(100))
CREATE TABLE #EmailTable(USERID INT, Email VARCHAR(100))
INSERT INTO #Usertable (USERID , Name , LINKID )
SELECT 1,'abc',NULL UNION ALL
SELECT 2,'pqr',NULL UNION ALL
SELECT 3,'str',NULL UNION ALL
SELECT 4,'xyz',NULL
INSERT INTO #PhoneTable (USERID , PH )
SELECT 1,'123' UNION ALL
SELECT 2,'123' UNION ALL
SELECT 3,'12345' UNION ALL
SELECT 4,'34567'
INSERT INTO #EmailTable (USERID , Email)
SELECT 1,'abc@gmail.com' UNION ALL
SELECT 2,'pqr@gmail.com' UNION ALL
SELECT 3,'abc@gmail.com' UNION ALL
SELECT 4,'xyz@gmail.com'
;WITH CTE AS
(
SELECT userid FROM #PhoneTable P1 WHERE
EXISTS(SELECT 1 FROM #PhoneTable P2 WHERE P1.PH = P2.PH AND P1.USERID <>
P2.USERID)
UNION ALL
SELECT userid FROM #EmailTable P1 WHERE
EXISTS(SELECT 1 FROM #EmailTable P2 WHERE P1.Email = P2.Email AND
P1.USERID <> P2.USERID)
)
UPDATE #Usertable SET LINKID = A.userid
FROM
(
SELECT MIN(userid) userid
FROM CTE
) A
WHERE EXISTS(SELECT 1 FROM CTE B WHERE B.userid = #Usertable.USERID)
**FOR UNIQUE LINKID**
UPDATE #Usertable SET LINKID = NEWID()
FROM CTE B WHERE B.userid = #Usertable.USERID
SELECT * FROM #Usertable
答案 1 :(得分:0)
怎么样:
update usertable u
set linkid = (select min(userid) from
(select userid from phonetable p
where p.phonenumber = (select phonenumber
from phonetable p1
where p1.userid = u.userid)
union all
select userid from emailtable e
where e.emailid = (select emailid
from emailtable e1
where e1.userid = u.userid)
)
);
答案 2 :(得分:0)
上述案例的构建模式
CREATE TABLE #USER (USERID INT, NAME VARCHAR(20), LINKID INT)
INSERT INTO #USER
SELECT 1 AS USERID, 'abc' AS NAME, NULL AS LINKID
UNION ALL
SELECT 2 , 'pqr' , NULL
UNION ALL
SELECT 3 , 'str', NULL
UNION ALL
SELECT 4 , 'xyz', NULL
UNION ALL
SELECT 5 , 'jkl', NULL
UNION ALL
SELECT 6 , 'mno', NULL
CREATE TABLE #PhoneTable (USERID INT, PhoneNumber VARCHAR(20) )
INSERT INTO #PhoneTable
SELECT 1 AS USERID, '123' PhoneNumber
UNION ALL
SELECT 2 AS USERID, '123'
UNION ALL
SELECT 3 AS USERID, '12345'
UNION ALL
SELECT 4 AS USERID, '34567'
UNION ALL
SELECT 5 AS USERID, '15233'
UNION ALL
SELECT 6 AS USERID, '15233'
CREATE TABLE #EmailTable (USERID INT, EmailID VARCHAR(50) )
INSERT INTO #EmailTable
SELECT 1 AS USERID, 'abc@gmail.com' EmailID
UNION ALL
SELECT 2 AS USERID, 'pqr@gmail.com'
UNION ALL
SELECT 3 AS USERID, 'abc@gmail.com'
UNION ALL
SELECT 4 AS USERID, 'xyz@gmail.com'
现在单独更新它们,如
--updating with Checking #PhoneTable
;WITH CTE AS (
SELECT PhoneNumber
, ROW_NUMBER() OVER(ORDER BY (SELECT 1))+(SELECT ISNULL(MAX(LINKID),0) FROM #USER) LINKID
FROM #PhoneTable
GROUP BY PhoneNumber HAVING COUNT(DISTINCT USERID)>1
)
UPDATE U SET U.LINKID = C.LINKID FROM #USER U
INNER JOIN #PhoneTable P ON U.USERID = P.USERID
INNER JOIN CTE C ON P.PhoneNumber = C.PhoneNumber
--updating with Checking #EmailTable
;WITH CTE AS (
SELECT EmailID
, ROW_NUMBER() OVER(ORDER BY (SELECT 1))+(SELECT ISNULL(MAX(LINKID),0) FROM #USER) LINKID
FROM #EmailTable
GROUP BY EmailID HAVING COUNT(DISTINCT USERID)>1
)
UPDATE U SET U.LINKID = C.LINKID FROM #USER U
INNER JOIN #EmailTable E ON U.USERID = E.USERID
INNER JOIN CTE C ON E.EmailID = C.EmailID
对于唯一LINKID生成,我已将ISNULL(MAX(LINKID),0)
添加到ROW_NUMBER