COUNT() doesn't work with GROUP BY? - sql

SELECT COUNT(*) FROM table GROUP BY column
I get the total number of rows from table, not the number of rows after GROUP BY. Why?

Because that is how group by works. It returns one row for each identified group of rows in the source data. In this case, it will give the count for each of those groups.
To get what you want:
select count(distinct column)
from table;
EDIT:
As a slight note, if column can be NULL, then the real equivalent is:
select (count(distinct column) +
max(case when column is null then 1 else 0 end)
)
from table;

Try this:
SELECT COUNT(*), column
FROM table
GROUP BY column

Related

Can I Select DISTINCT on 2 columns and Sum grouped by 1 column in one query?

Is it possible to write one query, where I would group by 2 columns in a table to get the count of total members plus get a sum of one column in that same table, but grouped by one column?
For example, the data looks like this
I want to get a count on distinct combinations of columns "OHID" and "MemID" and get the SUM of the "Amount" column grouped by OHID. The result is supposed to look like this
I was able to get the count correct using this query below
SELECT count(*) as TotCount
from (Select DISTINCT OHID, MemID
from #temp) AS TotMembers
However, when I try to use this query below to get all the results together, I am getting a count of 15 and a totally different total sum.
SELECT t.OHID,
count(TotMembers.MemID) as TotCount,
sum(t.Amount) as TotalAmount
from (Select DISTINCT OHID, MemID
from #temp) AS TotMembers
join #temp t on t.OHID = TotMembers .OHID
GROUP by t.OHID
If I understand correctly, you want to consider NULL as a valid value. The rest is just aggregation:
select t.ohid,
(count(distinct t.memid) +
(case when count(*) <> count(t.memid) then 1 else 0 end)
) as num_memid,
sum(t.amount) as total_amount
from #temp t
group by t.ohid,
The case logic might be a bit off-putting. It is just adding 1 if any values are NULL.
You might find this easier to follow with two levels of aggregation:
select t.ohid, count(*), sum(amount)
from (select t.ohid, t.memid, sum(t.amount) as amount
from #temp t
group by t.ohid, t.memid
) t
group by t.ohid

How to Compare Column to Count in a Query

I'm not too sophisticated with SQL, I don't know how to compare a count back to a column I'm selecting in a Query in Access. One column is generated as a count that counts IDs, but I want to verify it's correct (compare the column GroupCount with a SQL Count clause)
Here is what I have at the moment:
SELECT GroupID, GroupCount, COUNT(*) as A
FROM Table1 GROUP BY GroupID, GroupCount
How can I add a where clause that will compare "A" to GroupCount?
You should be able to use a having in your current query.
SELECT GroupID
,GroupCount
,COUNT(*) AS A
FROM Table1
GROUP BY GroupID
,GroupCount
HAVING COUNT(*) = GroupCount

Using sql, how can I simultaneously count the filtered and unfiltered occurrences of an attribute?

So this is probably a simple question, but here goes. I have some filtered data that get via a query like this:
SELECT DISTINCT account_id, count(*) as filtered_count
FROM my_table
WHERE attribute LIKE '%filter%'
GROUP BY account_id
ORDER BY account_id
This gives me an output table with two columns.
I'd like to add a third column,
count(*) as total_count
that counts the total number of occurrences of each account_id in the entire table (ignoring the filter).
How can I write the query for this three column table?
You can put a case expression inside the count function, then remove your where clause:
SELECT account_id,
count(case when attribute LIKE '%filter%' then 1 end) as filtered_count,
count(*) as total_count
FROM my_table
GROUP BY account_id
ORDER BY account_id;
Using DISTINCT although not actually harmful to your query, was redundant due to the grouping, so I have removed it.
You'll have to use a case statement for counting with your filter:
SELECT DISTINCT account_id,
count(case when attribute LIKE '%filter%' then 1 else null end) as filtered_count,
count (*)
FROM my_table
GROUP BY account_id
ORDER BY account_id

COUNT of DISTINCT items in a column

Here's the SQL that works (strangely) but still just returns the COUNT of all items, not the COUNT of DISTINCT items in the column.
SELECT DISTINCT(COUNT(columnName)) FROM tableName;
SELECT COUNT(*) FROM tableName
counts all rows in the table,
SELECT COUNT(columnName) FROM tableName
counts all the rows in the table where columnName is not null, and
SELECT (DISTINCT COUNT(columnName)) FROM tableName
counts all the rows in the table where columnName is both not null and distinct (i.e. no two the same)
SELECT DISTINCT(COUNT(columnName)) FROM tableName
Is the second query (returning, say, 42), and the distinct gets applied after the rows are counted.
You need
SELECT COUNT(DISTINCT columnName) AS Cnt
FROM tableName;
The query in your question gets the COUNT (i.e. a result set with one row) then applies Distinct to that single row result which obviously has no effect.
SELECT COUNT(*) FROM (SELECT DISTINCT columnName FROM tableName);

unique count of the columns?

i want to get a unique count of the of multiple columns containing the similar or different data...i am using sql server 2005...for one column i am able to take the unique count... but to take a count of multiple columns at a time, what's the query ?
You can run the following selected, getting the data from a derived table:
select count(*) from (select distinct c1, c2, from t1) dt
To get the count of combined unique column values, use
SELECT COUNT(*) FROM TableName GROUP BY UniqueColumn1, UniqueColumn2
To get the unique counts of multiple individual columns, use
SELECT COUNT(DISTINCT Column1), COUNT(DISTINCT Column2)
FROM TableName
Your question is not clear what exactly you want to achieve.
I think what you're getting at is individual SUMS from two unique columns in one query. I was able to accomplish this be using
SELECT FiscalYear, SUM(Col1) AS Col1Total, SUM(Col2) AS Col2Total
FROM TableName
GROUP BY FiscalYear
If your data is not numerical in nature, you can use CASE statements
SELECT FiscalYear, SUM(CASE WHEN ColA = 'abc' THEN 1 ELSE 0 END) AS ColATotal,
SUM(CASE WHEN ColB = 'xyz' THEN 1 ELSE 0 END) AS ColBTotal
FROM TableName
GROUP BY FiscalYear
Hope this helps!