如何在SQL中获取唯一数据
我已经尝试了如何在SQL中获取唯一数据,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我已经尝试了distinct,但它在我的情况下不起作用 我的数据如下表所示: qincId ID lc1 lc2 Time SP ------------------------------------------------------------------------ 963 544 22.3000526428 73.1743087769 2019-03-31 17:00:46.00
distinct
,但它在我的情况下不起作用
我的数据如下表所示:
qincId ID lc1 lc2 Time SP
------------------------------------------------------------------------
963 544 22.3000526428 73.1743087769 2019-03-31 17:00:46.000 15
965 544 22.2998828888 73.1746368408 2019-03-31 17:01:07.000 2
968 544 22.2998828888 73.1746368408 2019-03-31 17:01:40.000 2
997 544 22.3010215759 73.1744003296 2019-03-31 17:06:11.000 15
998 544 22.3011436462 73.1747131348 2019-03-31 17:06:21.000 17
1010 544 22.3034667969 73.1747512817 2019-03-31 17:08:04.000 0
1011 544 22.3032741547 73.1747512817 2019-03-31 17:08:03.000 0
1012 544 22.3032741547 73.1747512817 2019-03-31 17:08:04.000 0
1028 544 22.3032741547 73.1747512817 2019-03-31 17:11:04.000 0
1563 544 22.3032741547 73.1747512817 2019-03-31 18:45:27.000 0
1564 544 22.3032741547 73.1747512817 2019-03-31 18:45:28.000 0
1565 544 22.3032035828 73.1748123169 2019-03-31 18:45:26.000 0
1567 544 22.3032035828 73.1748123169 2019-03-31 18:45:28.000 0
1571 544 22.3028964996 73.1748123169 2019-03-31 18:46:03.000 16
1573 544 22.3023796082 73.1747131348 2019-03-31 18:46:21.000 15
1575 544 22.3021774292 73.1746444702 2019-03-31 18:46:37.000 0
1577 544 22.3019657135 73.1747665405 2019-03-31 18:46:50.000 15
1586 544 22.3009243011 73.1742477417 2019-03-31 18:47:33.000 5
1591 544 22.2998828888 73.1745300293 2019-03-31 18:48:19.000 5
1592 544 22.2998828888 73.1745300293 2019-03-31 18:48:28.000 5
1593 544 22.2998981476 73.1746063232 2019-03-31 18:48:29.000 4
1597 544 22.3000450134 73.1744232178 2019-03-31 18:49:08.000 0
1611 544 22.3000450134 73.1744232178 2019-03-31 18:51:28.000 0
1616 544 22.3000450134 73.1744232178 2019-03-31 18:52:22.000 0
1677 544 22.3000450134 73.1744232178 2019-03-31 19:03:28.000 0
现在,我希望所有记录的sp>0
和第一条记录的speed=0
具有相同的lc1和lc2
因此,基本上我不想要lc1和lc2在sp=0的情况下重复的数据
上述记录的预期产出:
qincId ID lc1 lc2 Time SP
-------------------------------------------------------------------------
963 544 22.3000526428 73.1743087769 2019-03-31 17:00:46.000 15
965 544 22.2998828888 73.1746368408 2019-03-31 17:01:07.000 2
968 544 22.2998828888 73.1746368408 2019-03-31 17:01:40.000 2
997 544 22.3010215759 73.1744003296 2019-03-31 17:06:11.000 15
998 544 22.3011436462 73.1747131348 2019-03-31 17:06:21.000 17
1010 544 22.3034667969 73.1747512817 2019-03-31 17:08:04.000 0
1011 544 22.3032741547 73.1747512817 2019-03-31 17:08:03.000 0
1565 544 22.3032035828 73.1748123169 2019-03-31 18:45:26.000 0
1571 544 22.3028964996 73.1748123169 2019-03-31 18:46:03.000 16
1573 544 22.3023796082 73.1747131348 2019-03-31 18:46:21.000 15
1575 544 22.3021774292 73.1746444702 2019-03-31 18:46:37.000 0
1577 544 22.3019657135 73.1747665405 2019-03-31 18:46:50.000 15
1586 544 22.3009243011 73.1742477417 2019-03-31 18:47:33.000 5
1591 544 22.2998828888 73.1745300293 2019-03-31 18:48:19.000 5
1592 544 22.2998828888 73.1745300293 2019-03-31 18:48:28.000 5
1593 544 22.2998981476 73.1746063232 2019-03-31 18:48:29.000 4
1597 544 22.3000450134 73.1744232178 2019-03-31 18:49:08.000 0
1677 544 22.3000450134 73.1744232178 2019-03-31 19:03:28.000 0
我也尝试过使用distinct和group by,但我无法获得输出
如何获得预期的输出?使用union all和row_number()尝试如下操作
使用union all和row_number()试试下面的方法
嗨,谢谢你的解决方案。它正在按预期工作。你能在现有查询中简要解释一下这个问题吗?@siddharth这部分是基于lc1和lc2的最小时间行,顺便说一句,如果它有助于标记为已接受,我可以再请求一个帮助吗?@siddharth如果有问题,你可以发布另一个问题,但同时如果需要更多信息,你也可以在这里询问是的,我正在发布另一个问题这个问题你好,谢谢你的解答。它正在按预期工作。你能在现有查询中简要解释一下这个问题吗?@siddharth这部分是基于lc1和lc2的最小时间行,顺便说一句,如果它有助于标记为已接受,我可以再请求一个帮助吗?@siddharth如果有问题,你可以发布另一个问题,但同时如果需要更多信息,你也可以在这里询问是的,我正在发布另一个问题这个问题
select qincId,ID,lc1,lc2,time,sp from
(select qincId,ID,lc1,lc2,time,sp,
row_number()over(partition by lc1,lc2 order by time) rn
from table_name where sp>0
) t where t.rn=1
union all
select qincId,ID,lc1,lc2,time,sp from
(
select *,row_number()over(partition by lc1,lc2 order by time ) rn
from table_name where sp=0
) a where a.rn=1