Query to find unique row and their count - sql

Say I have a table with rows like,
A
B
A
C
D
D
A
I want a table from that with unique row and count. So resultant table will be of two column like,
A 3
B 1
C 1
D 2
Having difficulty writing the query. Help?

SELECT column1, count(column1)
FROM [table]
GROUP BY column1

Related

How to display records in SQL Server that have same value in Column 2 but value in Column 1 should not be present in any other record?

I have a below table like this -
Policy Column1 Column2
A 4 100
B 4 100
C 3 100
D 3 100
E 2 100
F 5 100
The Output should be
Policy Column1 Column 2
E 2 100
F 5 100
Can someone please guide me.
Assuming you're after rows where the value in column1 is unique (i.e. no other row has that same value in column 1)
select max(Policy) Policy -- if we're just getting 1 row, the max is the only value
, Column1
, max(Column2) Column2 -- as above
from myTable
group by Column1 -- group by column 1 then
having count(1) = 1 -- count how many rows are in that group; if it's unique we get 1
Select * from policies
where Column1 in (select Column1
from policies
group by Column1
having count(*)=1);
DBFiddle demo is here
You want to divide the data into chunk via partition by. Then you can determine which of them has only a single row.
with data as (
select *, count(*) over (partition by Column1, Column2) as cnt
from T
)
select Policy, Column1, Column2 from data where cnt = 1;
This kind of query was harder to write twenty years ago. If you're learning SQL I'd encourage you to get a handle on some of the foundational concepts before diving into table expressions and analytic functions.

Top n distinct values of one column in Oracle

I'm using a query where a part of it gets the top 3 of a certain column.
It creates a distinct subquery of the column, limited by 3 number of rows, and then filters those rows to the main query to do the top 3.
WITH subquery AS (
SELECT col FROM (
SELECT DISTINCT col
FROM tbl
) WHERE ROWNUM <= 3
)
SELECT col
FROM tbl
WHERE tbl.col = subquery.col
So the original table is like this:
col
-----
a
a
a
b
b
b
c
d
d
e
f
f
f
f
And the query returns the top 3 of the column (not the top 3 rows which would only be a):
col
-----
a
a
a
b
b
b
c
I'm trying to learn if there is a more correct way of doing this as the real query is big and duplicating its size with a subquery that looks almost the same just to get the top 3 is hard to work with and understand/modify.
Is there a better way to do the top first 3 distinct values of one column in Oracle?
Yes, you can use dense_rank and avoid duplicated code:
select col
from (select col, dense_rank() over (order by col) rnk from tbl)
where rnk <= 3
demo

Query to show how many distinct records on column B for each record on Column A using MS Access

I am having a hard time to write the SQL query to do
something like, how many distinct record on column B for each record on Column A
Let's say:
Column A,,,,,,,,,,,,,,,,, Column B
A 1
A 1 would return 3 for A (3 distinct records 1,2,3)
A 2
A 3
B 4 Would return 2 for B (4,5)
B 4
B 5
C 6 Would return 1 for C
C 6
it will be imported to a datagridview to show the result
Column 1---------------Column 2
A 3
B 2
C 1
column 2 being the result of the query
I don't know if it is easy and I don't get it or complicated but I can't figure out.
I have tried Group By, Distinct etc... I get either not what I want or error like no value given....
Thank you.
After some research I found this query that seems to return the results expected.
SELECT ColA, COUNT(COLB) FROM (SELECT ColA, ColB FROM Table Where blablabla GROUP BY ColA,ColB) GROUP BY ColA"
You could try something like:
SELECT ColA, Count(ColB)
FROM MyTable (NOLOCK)
GROUPBY ColA
The sql query is select colA, colB, COUNT(DISTINCT colB) from t1 group by colA
You can access in SQL fiddle.
http://sqlfiddle.com/#!9/2c84d8/7
Actually, if what you're looking to partition the counts try: over (Partition by)
Partition Function COUNT() OVER possible using DISTINCT

How to retrieve only a row if row are same in web sql?

A
A
A
B
B
C
C
How do I retrieve only a single row if row values are the same in web SQL?
Result:
A
B
C
If your data really is just 1 column then use DISTINCT
SELECT DISTINCT yourcolumn
FROM yourtable

Counting occurrences of unique values in a column using sql

Is there a simple way for counting occurrences of unique values in a column using sql.
for e.g if my column is
a
a
b
a
b
c
d
d
a
Then the output should be
a 4
b 2
c 1
d 2
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
Use GROUP BY and COUNT
SELECT column, COUNT(*)
FROM table
GROUP BY column
After searching and giving some good tought here's the correct query :
SELECT SUM(uniqueValues)
FROM (
SELECT COUNT(DISTINCT values) as uniqueValues
FROM tablename GROUP BY values)