SQL Server select distinct hell

梦想的初衷 提交于 2020-01-13 20:29:27

问题


I'm stuck on a problem with sql. I have a table with many duplicate entries with column names like:-

eventnumber housenumber value1 value2

None of these column names are the primary key as there are many duplicates. What I would like to do is select into another table by distinct housenumber but I seem to get the whole table copied across, I'm using:-

Select * into temp_table from schedule_temp where housenumber in (select distinct housenumerb from schedule_temp)

Now I broke it down a bit and if I do:-

Select distinct housenumber into temp from schedule_temp group by housenumber

I get a table with the unique housenumbers... but then how could I use this unique table to drive another select that picks housenumbers from temp and only gets one instance of the housenumber from schedule_temp? Hope that makes sense.

Beers are on me if you can save my sanity.


回答1:


First I'd get the data into a table with an auto increment id

So create a table with id,eventnumber,housenumber,value1, value2 where id is an auto number.

Then

Insert Into NewTemp(eventnumber,housenumber,value1, value2)
Select eventnumber,housenumber,value1, value2 From schedule_temp

Then this query should guve you 1 row per house #

Select nt.* From NewTemp nt
Join (select max(id) as id, housenumber from NewTemp Group By housenumber) t on t.id=nt.id



回答2:


You don't want DISTINCT records (as in, each record has at least one column different than all other records). You want a winner from each group (or partition) of records.

SELECT *
FROM
(
  SELECT
    EventNumber, HouseNumber, Value1, Value2,
    ROW_NUMBER()
      OVER(PARTITION BY HouseNumber ORDER BY HouseNumber) as rowNum
  FROM ServiceAddr
) sub
WHERE sub.rowNum = 1


来源:https://stackoverflow.com/questions/2247401/sql-server-select-distinct-hell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!