表1
id mobile uid uniqueid time
1 9999 1232 200100 10/6/15
2 9999 1232 200100 11/5/14
3 8888 1232 200100 18/4/16
4 9999 1232 200100 07/7/15
5 9999 1232 200100 4/11/16
表2
id Qid account
1 200100 123456
2 200100 123456
3 200100 123456
我只想根据手机号码重复数据
我使用像
这样的查询SELECT R.Id,R.MobileNo,R.UId,R.UniqueId,R.Time,
ISNULL(C.account,'Not Present'),
FROM table1 R LEFT JOIN table2 C
ON R.uniqueId=C.QId where
and R.deleteStatus='Y' and C.accountNos ='123456'
欲望输出
mobile uid uniqueid time account
9999 1232 200100 4/11/16 123456
8888 1232 200100 18/4/16 123456
我尝试了sql server中的所有东西但是无法得到欲望输出请帮帮我!!!!!
答案 0 :(得分:0)
请试试这个:
with cte as (
SELECT
R.Id,
R.MobileNo,
R.UId,
R.UniqueId,
R.Time,
ISNULL(C.account,'Not Present'),
Row_Number() Over(Partition by R.MobileNo order by [time] Desc) as RN
FROM table1 R
LEFT JOIN table2 C
ON R.uniqueId=C.QId
where
R.deleteStatus='Y' and C.accountNos ='123456'
)
Select * from cte
Where rn=1
答案 1 :(得分:0)
CREATE TABLE #TABLE1
(
ID INT, MOBILE INT, UID INT, UNIQUEID INT, TIME DATE
)
INSERT INTO #TABLE1 VALUES(1,9999,1232,200100,'6-10-15')
INSERT INTO #TABLE1 VALUES(2,9999,1232,200100,'5-11-14')
INSERT INTO #TABLE1 VALUES(3,8888,1232,200100,'4-18-16')
INSERT INTO #TABLE1 VALUES(4,9999,1232,200100,'7-7-15')
INSERT INTO #TABLE1 VALUES(5,9999,1232,200100,'11-4-16')
SELECT * FROM #TABLE1
CREATE TABLE #B
(
ID INT, QID INT, ACCOUNT INT)
INSERT INTO #B VALUES
(1,200100,123456),
(2,200100,123456),
(3,200100,123456)
;WITH CTE AS
(
SELECT DISTINCT T1.MOBILE,UID,UNIQUEID,TIME,ACCOUNT,
ROW_NUMBER() OVER(PARTITION BY MOBILE ORDER BY [TIME] DESC) AS RN
FROM #TABLE1 T1 JOIN #B T2 ON T1.ID=T1.ID
AND T1.UNIQUEID=T2.QID
)
SELECT MOBILE,UID,UNIQUEID,TIME,ACCOUNT
FROM CTE
WHERE RN=1
答案 2 :(得分:0)
试试这个:这将提供表格中的最新数据
SELECT R.Id,R.MobileNo,R.UId,R.UniqueId,MAX(R.Time),ISNULL(C.account,'Not Present'),
FROM table1 R
LEFT JOIN table2 C ON R.uniqueId=C.QId AND C.accountNos ='123456'
WHERE R.deleteStatus='Y'
GROUP BY R.Id,R.MobileNo,R.UId,R.UniqueId,ISNULL(C.account,'Not Present')