Sql query with limited account

I'm not sure how to structure a SQL query to return the number of rows where the column is equal to specific values.

For instance,

In the table myTable, how can I return the count of all rows, where myColumn = "xyz"and where myColumn = "abc"? Is this possible with a single request?

To clarify, let's say there are 10 lines, where myColumn = "xyx"and 7 lines, where myColumn = "abc", the query will return something like:

firstCountResult: 10
secondCountResult: 7
+3
source share
2 answers

How??

SELECT 
    COUNT(*),
    myColumn
FROM myTable
WHERE myColumn IN ('xyz', 'abc')
GROUP BY myColumn

This approach also works with other aggregate functions, such as MIN, MAX, AVG, SUM ... You get the final result by the value of the grouped column compared to all rows.

--min myDate per myColumn value
SELECT
    MIN(myDate),
    myColumn
FROM myTable
WHERE myColumn IN ('xyz', 'abc')
GROUP BY myColumn

--sum of myNumericCol per myColumn value
SELECT
    SUM(myNumericCol),
    myColumn
FROM myTable
WHERE myColumn IN ('xyz', 'abc')
GROUP BY myColumn
+3
source

mycolumn,

select 
         myColumn, count(*)
from  
         myTable 
where 
         myColumn in ('xyz','abc')
group by 
         myColumn
+2

Source: https://habr.com/ru/post/1781297/


All Articles