W SQL Server jestem w stanie utworzyć zapytanie, które używa zarówno Top, jak i Distinct w klauzuli Select, takiej jak ta:
Select Distinct Top 10 program_name
From sampleTable
Czy baza danych zwróci różne wartości z 10 najlepszych wyników, czy też zwróci 10 najlepszych wyników z różnych wartości? Czy to zachowanie jest spójne w SQL, czy jest zależne od bazy danych?
Odpowiedzi:
4 dla odpowiedzi № 1TOP
jest wykonywany jako ostatni, więc twój DISTINCT
uruchamia się najpierw, a następnie TOP
0 dla odpowiedzi nr 2
Posługiwać się
Select Top 10 program_name
From sampleTable group by program_name;
Zwróci ci górę 10 różnych nazw programów.
Twoje zapytanie zwróci również odrębną 10 nazwa_programu.
0 dla odpowiedzi № 3
Spróbuj tego:
select distinct top 10 c from
(
select 1 c union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 2
) as T
order by c
Porównaj ten wynik z następującymi zapytaniami:
select distinct c from (
select top 10 c from
(
select 1 c union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 2
) as T
order by c
) as T2
select top 10 c from (
select distinct c from
(
select 1 c union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 2
) as T
) as T2
order by c