2

我有一张房屋清单。我想每个城市最多保留 10 个列表。(大多数城市的房源少于 10 个)。

当我执行此查询时:

 select city, count(city) as cityCount from tREaltyTrac group by city

SQL 返回:

Acampo  1
Acton   1
Adelanto    20
Agua Dulce  1
Aguanga 1
Akron   19
Albany  12
Albion  3
Alexandria  14
Algonac 1
Alhambra    5

因此,必须将 Adelanto、Akron、Albany 和 Alexandria 减少到只有 10 个按最近日期时间字段“creation”(按创建顺序排序)排序的列表。

任何人都可以想出一个程序来减少所描述的列表吗?

4

6 回答 6

4

DON'T LOOP!

I prefer marking th rows with a status, but this will do as you want and delete them...

try this (sql server)

set up table

create table tREaltyTrac (city varchar(20),creation datetime)
insert into tREaltyTrac values ('Acampo'      ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Acton'       ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Adelanto'    ,getdate()) --20
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Agua Dulce'  ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Aguanga'     ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Akron'       ,getdate()) --19
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albany'      ,getdate()) --12
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albion'      ,getdate()) --3
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albion'      ,getdate()) --3
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Albion'      ,getdate()) --3
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alexandria'  ,getdate()) --14
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Algonac'     ,getdate()) --1
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5
waitfor delay '00:00:01'
insert into tREaltyTrac values ('Alhambra'    ,getdate()) --5

display table values

select city,count(*) from tREaltyTrac group by city
select * from tREaltyTrac

delete the rows you don't want

delete from tREaltyTrac
    from tREaltyTrac
        inner join (select
                        city,creation, row_number() over(partition by city order by city) AS RankValue
                        from tREaltyTrac
                   ) dt on tREaltyTrac.city=dt.city AND tREaltyTrac.creation=dt.creation
    where dt. RankValue>10

show the rows you have left

select * from tREaltyTrac
select city,count(*) from tREaltyTrac group by city
于 2009-05-01T21:39:45.480 回答
1

Something like this should take care of it for you, but deleting records automatically isn't a great idea. You'd be better off using an active flag.

DECLARE @CityName VARCHAR(30)
DECLARE CitiesOver10 CURSOR FOR select city from tREaltyTrac group by city having count(city)>10 

OPEN CitiesOver10
FETCH NEXT FROM CitiesOver10 INTO @CityName

WHILE @@FETCH_STATUS = 0
    BEGIN
        DELETE FROM 
            dbo.tREaltyTrac 
        WHERE 
            ID NOT IN (SELECT TOP 10 ID FROM dbo.tREaltyTrac WHERE city = @CityName ORDER BY Creation DESC)
            AND City = @CityName

        FETCH NEXT FROM CitiesOver10 INTO @CityName
    END

CLOSE CitiesOver10
DEALLOCATE CitiesOver10
于 2009-05-01T21:19:33.623 回答
0

您不应该从数据库中删除记录。这不是 100% 完美的,我敢肯定有更好的方法来做到这一点,但你去吧。

declare @cities (CityName nvarchar(50), ID int identity(1,1))
declare @returns (CityName nvarchar(50), Blah nvarchar(50))
declare @cityname nvarchar(50)
declare @count int
declare @i int


insert into @cities (CityName)
select distinct CityName
from tblCities

select @count = count(*) from @cities
set @i=1

while (@i<=@count)
begin

    select @cityname = CityName from @cities where ID=@i

    select top 10 *
    from tblCities
    where CityName=@cityname
    order by Creation desc

    set @i=@i+1
end
于 2009-05-01T21:12:21.443 回答
0

我现在没有时间写出实际的代码,但是像这样的东西怎么样......(假设是 PHP)

  1. 订购您的初始查询 - 添加创建 DESC
  2. 循环遍历结果,获取第 10 个结果的创建日期
  3. Run another query that deletes everything less than the creation value for the 10th result? - i.e. DELETE WHERE Creation < $creationDateOfTenthResult

Hope that makes sense...

于 2009-05-01T21:13:52.640 回答
0

I would select the top ten, throw them in a temp table, delete the original and populate it with the temp table.

于 2009-05-01T21:14:54.290 回答
0

Depends on your version of sql. Something similar to this might work

SELECT r.City, b.* FROM tREaltyTrac r join tREaltyTrac b on b.Id in ( select top 10 Id from tREaltyTrac where Id = r.Id order by Id Desc ) group by r.City

于 2009-05-01T21:33:47.537 回答