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)
Related
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
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
If we have a table with values for a and b, is there a way to only add up the b's if its not a duplicate a? For example
a b
1 2
2 3
2 3
so we would get only 5 (instead of 8)
A sort of
select sum(b if unique a),
from table
where ...
The following query selects the lowest value of b for each group a
select min(b) min_b
from mytable
group by a
You can then sum those values by selecting the sum from a derived table
select sum(min_b) from (
select min(b) min_b
from mytable
group by a
) t
http://sqlfiddle.com/#!9/d82c5/1
You haven't specified your RDBMS, but if you are using a database which supporting window functions like SQL Server, you can query the unique rows first by using WITH clause and ROW_NUMBER() function and then get the SUM out of that.
;WITH C AS(
SELECT a, b,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY a) AS Rn
FROM Table1
)
SELECT SUM(b) FROM C
WHERE Rn = 1
SQL Fiddle
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
I want to get a subset of a table, here's the example:
1 A
2 A
3 B
4 B
5 C
6 D
7 D
8 D
I want to get the unique record, but with the smallest id:
1 A
3 B
5 C
6 D
How can I write the SQL in SQL Server? Thanks!
Use a common-table expression like this:
;WITH DataCTE AS
(
SELECT ID, OtherCol,
ROW_NUM() OVER(PARTITION BY OtherCol ORDER BY ID) 'RowNum'
FROM dbo.YourTable
)
SELECT *
FROM DataCTE
WHERE RowNum = 1
This "partitions" your data by the second column you have (A, B, C) and orders by the ID (1, 2, 3) - smallest ID first.
Therefore, for each "partition" (i.e. each value of your second column), the entry with RowNum = 1 is the one with the smallest ID for each value of the second column.
select min(id), othercol
from thetable
group by othercol
and maybe with
order by othercol
... at the end if thats important
Try this:
SELECT MIN(Id) AS Id, Name
FROM MyTable
GROUP BY Name
select min(id), column2
from table
group by column2
It helps if you provide the table information in the question - I've just guessed at the column names...