SQLSERVER去除某一列的重复值并显示所有数据\\DISTINCT去重\\ISNULL()求SUM()\\NOT EXISTS的使用
进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: 1 select t.* from [PeopleCount] as t where t.procedureID='8334' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单纯的查询 GX 这一列用 distinct 是没有任何问题的 1 select distinct t.GX from [PeopleCount] as t where t.procedureID='8334' 但是如果我们加上表中其它数据的话,我们来看看效果: 1 select distinct t.GX ,t.* from [PeopleCount] as t where t.procedureID='8334' 很显然,结果发现不是我们想要的数据。 3.这个时候我们既想要去重,又想要去重后的数据,我们可以这样: 1 select t.* from [PeopleCount] as t where t.procedureID='8334' and not exists 2 (select 1 from [PeopleCount] where GX=t.GX and countID>t.countID) 这就是将GX过滤去重后查询到的所有数据了。