简体   繁体   中英

How to get the values of the same column aganist the distinct values

I am trying to get the summary for one column in sql. The below is the code but i am unable to achieve my output. I Want the count of account number Beside the distinct account number column.

Select distinct (Account_number), count(Distinct Account_number) As number_of_acc
From txn
Group by Account_number 

The output should be like:

Account_number number_of_acc
Xx1.               2
Xx3.               1
Xx6.               4

Since the account number repeats as per the counts mentioned in the column number_of_acc.

Remove the DISTINCT keyword from the query since it is already taken care by GROUP BY statement.

SELECT Account_number, COUNT(Account_number) AS number_of_acc
FROM txn
GROUP BY Account_number

just apply the group by clause on the account number

select Account_number,count(1) as number_of_acc from txn group by Account_number

Using ROW NUMBER

SELECT S.ACCOUNT_NUMBER,COUNT(*) FROM(
SELECT ACCOUNT_NUMBER, ROW_NUMBER()OVER(PARTITION BY ACCOUNT_NUMBER ORDER BY ACCOUNT_NUMBER) AS RW
FROM TXN) S
WHERE S.RW=1
GROUP BY S.ACCOUNT_NUMBER

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