我是sql的新手,我使用MS SQL Server,我有这个选择并加入
SELECT t1.number
,t2.site1
,t2.Events_W
,t3.site1
,t3.Events_W_D
,t4.site1
,t4.Events_NW
,t5.site1
,t5.Events_NW_D
FROM [TEST].[ak].[test3] AS t1
LEFT JOIN [TEST].[ak].[test2] AS T2
ON t1.number=t2.number AND t1.Events_W = t2.Events_W
LEFT JOIN [TEST].[ak].[test2] AS t3
ON t1.number=t3.number AND t1.Events_W_D=t3.Events_W_D
LEFT JOIN [TEST].[ak].[test2] as t4
ON t1.number=t4.number AND t1.Events_NW = t4.Events_NW
LEFT JOIN [TEST].[ak].[test2] t5
ON t1.number=t5.number AND t1.Events_NW_D=t5.Events_NW_D
结果如下:
Number Site |event_w |site |event_w_d |site |Event_nt |site |event_nw_d
anum |2014 |116 |2014 |99 |2358 |163 |2087 |129
bnum |1570 |17 |1570 |76 |1196 |99 |1196 |577
bnum |1570 |17 |1905 |111 |1508 |26 |1508 |427
bnum |1196 |3 |1475 |2 |1508 |1 |8263 |5
cnum |2087 |15 |NULL |NULL |2087 |92 |NULL |NULL
dnum |1071 |14 |1905 |106 |1071 |21 |1426 |206
gnum |2087 |36 |2087 |97 |2087 |92 |2087 |93
rnum |123 |23 |123 |23 |NULL |NULL |NULL |NULL
正确的结果必须是:
Number |Site |event_w |site |event_w_d |site |Event_nt |site |event_nw_d
anum |2014 |116 |2014 |99 |2358 |163 |2087 |129
bnum |1196 |3 |1475 |2 |1508 |1 |8263 |5
cnum |2087 |15 |NULL |NULL |2087 |92 |NULL |NULL
dnum |1071 |14 |1905 |106 |1071 |21 |1426 |206
gnum |2087 |36 |2087 |97 |2087 |92 |2087 |93
我需要通过数字区分,但不使用'区别'。
请帮忙!
答案 0 :(得分:0)
Declare @Tep Table
(
name varchar(100), City Varchar(500), Contct1 bigInt)
INSERT INTO @Tep
select 'R', 'Delhi', 7827492235
Union All
select 'S', 'Noida', 8827492235
Union All
select 'P', 'Noida', 7827492235
Union All
select 't', 'r', 9827492235
--Get Unique Record Without Distinct
SELECT * FROM (
SELECT ROW_NUMBER() OVER(PARTITION BY Contct1 ORDER BY Contct1) As RowID,
name,
City , Contct1
FROM @Tep
)T WHERE RowID=1
答案 1 :(得分:0)
另请尝试下面的approch获得无与伦比的独特记录 SELECT * FROM( SELECT ROW_NUMBER()OVER(Contect1 ORDER BY Contct1的分区)作为RowID, 名称, 城市,Contct1 来自@Tep )T WHERE Contct1 NOT In(
SELECT Contct1 FROM( SELECT ROW_NUMBER()OVER(Contect1 ORDER BY Contct1的分区)作为RowID, 名称, 城市,Contct1 来自@Tep )T其中RowID = 2 )
也尝试这种方式