I have a table tb_FirstName
with one field FirstName
. The table has 100 million non null records with lots of repetitions e.g. John occurs 2 million times. The distinct count of FirstName
is over 2 million.
How do I select 1000 distinct names as quickly as possible using standard sql?
I'm currently using the following but this is
Maybe not as efficient as it could be.
SELECT x.FirstName
FROM (
SELECT FirstName,
rnk = RANK() OVER (ORDER BY Firstname)
FROM WHData.dbo.tb_DimUserAccount A
GROUP BY FirstName
) x
WHERE rnk <=1000
To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.
In SQL multiple fields may also be added with DISTINCT clause. DISTINCT will eliminate those rows where all the selected fields are identical.
Seems like you could use TOP 1000
with DISTINCT
:
SELECT DISINCT TOP 1000 FirstName
FROM WHData.dbo.tb_DimUserAccount
ORDER BY FirstName
Condensed SQL Fiddle Demo
Try this
SELECT TOP 1000 FirstName FROM
(SELECT
ROW_NUMBER() OVER(PARTITION BY FirstName ORDER BY FirstName) NO,
FirstName FROM WHData.dbo.tb_DimUserAccount )
AS T1 WHERE no =1
or
SELECT DISINCT TOP 1000 FirstName
FROM WHData.dbo.tb_DimUserAccount ORDER BY FirstName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With