简体   繁体   中英

How to count all distinct values in each column of table

I want to create a query that satisfies these criteria:

  1. It counts the number of distinct values for each column.
  2. It selects the top three columns that have the greatest number of distinct values.
  3. It counts the number of distinct values contained in the power set of those top three columns.

This is what I've got so far:

SELECT TOP(3) COUNT(DISTINCT column_name) AS some_alias FROM tablename 
    GROUP BY ~ ORDER BY count(*) asc

What is wrong with my query?

use distinct value inside subquery then use count in select query like below.

SELECT columnName,
   COUNT(col1) AS Col,
   COUNT(col2) AS col2

FROM
(
    SELECT DISTINCT
           columnName,
           Col1,
           col2
    FROM table
)

GROUP BY columnName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM