문제

주택 목록 테이블이 있습니다. 도시 당 최대 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는 가장 최근의 Datetime 필드에서 'Creation'(Creation By Creation DESC)에서 주문한 10 개의 목록으로 만 줄여야합니다.

누구든지 설명대로 목록을 다시 줄이는 절차를 생각할 수 있습니까?

도움이 되었습니까?

해결책

고리하지 마세요!

나는 상태가있는 줄을 표시하는 것을 선호하지만, 이것은 당신이 원하는대로 수행하고 삭제합니다 ...

이것 (SQL Server) 시도

테이블을 설정하십시오

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

표시 테이블 값

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

원하지 않는 행을 삭제하십시오

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

남은 줄을 보여주십시오

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

다른 팁

이와 같은 것이 당신을 위해 그것을 돌봐야하지만, 레코드를 자동으로 삭제하는 것은 좋은 생각이 아닙니다. 활성 깃발을 사용하는 것이 좋습니다.

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

데이터베이스에서 레코드를 삭제해서는 안됩니다. 이것은 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

나는 지금 실제 코드를 작성할 시간이 없지만 이와 같은 것은 어떻습니까 .... (PHP를 가정)

  1. 초기 쿼리를 주문하십시오 - 추가 창조 desc
  2. 결과를 통과하고 10 번째 결과의 생성 날짜를 얻으십시오.
  3. 10 번째 결과의 생성 값보다 적은 모든 것을 삭제하는 다른 쿼리를 실행합니까? - 즉 Creation <$ creationDateOftEthResult를 삭제하십시오

그것이 말이되기를 바랍니다 ...

상단 10을 선택하고 온도 테이블에 던지고 원본을 삭제 한 다음 온도 테이블로 채 웁니다.

SQL 버전에 따라 다릅니다. 이것과 비슷한 것이 효과가있을 수 있습니다

R.City를 선택하십시오

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top