Is there a way in mysql to achieve
SELECT COUNT( SUM(field2) ) FROM TABLE WHERE field1='value' GROUP BY field2
Returning one row with the total sum of the counted field 2.
Field 1 && Field 2 are both not unique.
If there is a need for more info , just let me know.
Edit :
I was not thinking clear.
I need the COUNT of The COUNT.
so all the counted values from field 2.
this can be achieved by getting the number of rows , but I chose for this :
SELECT COUNT( first_count ) total_count FROM
( SELECT COUNT( field2 ) as first_count FROM TABLE
WHERE field1='value' GROUP BY field2 )t1
Or is there a faster query?
Cheers !
Your update
SELECT COUNT( first_count ) total_count FROM
(
SELECT COUNT( field2 ) as first_count FROM TABLE
WHERE field1='value' GROUP BY field2 )t1
)
Is a count of the COUNTs for each field2, which is the same as a count of unique field2s;
SELECT COUNT(DISTINCT field2) FROM TABLE WHERE field1='value'
I think you mean:
SELECT field2
, field2 * COUNT(*)
FROM TABLE
WHERE field1 = 'value'
GROUP BY field2
Also:
There is no GROUPED BY. There is: GROUP BY
After your update and clarification, I think what your query shows is the same with:
SELECT COUNT( DISTINCT field2 ) AS total_count
FROM TABLE
WHERE field1 = 'value'
Related
I've been reading through the solutions of similar problems posted here, but none seem to resolve my particular issue.
I currently have a table (CT_JOINED) that includes three columns: an identifer column (TUMOURID), a date column (AVCT_DATE) and another date column (OPDATE).
As an example, the columns for two IDs look as follows:
ID, AVCT_DATE, OPDATE
1, 06-APR-13, 06-APR-13
1, 06-APR-13, 14-JUN-13
1, 06-APR-13, 22-JUN-13
2, 03-APR-14, 10-DEC-15
2, 03-APR-14, 31-DEC-15
What I'm attempting to do is create a column that is equal to the number of unique dates per ID. So the result for ID 1 would be 3 and the result for ID 2 would be 3.
I have attempted a count of distinct values across the two columns, but this does not provide the answers above (it instead reports values of 3 and 2 respectively):
select TUMOURID, COUNT(DISTINCT(AVCT_DATE || OPDATE)) AS COUNT
FROM CT_JOINED
GROUP BY TUMOURID;
The same thing happens if I try and do the same in a new table:
CREATE TABLE CT_DISTINCT AS (
SELECT TUMOURID, COUNT(*) AS COUNT
FROM (
SELECT DISTINCT TUMOURID, AVCT_DATE, OPDATE
FROM CT_JOINED)
GROUP BY TUMOURID
);
I'm at a loss. Is it possible?
You could use:
SELECT TUMOURID, COUNT(DISTINCT d) AS cnt
FROM (select TUMOURID, AVCT_DATE AS d
FROM CT_JOINED
UNION ALL
SELECT TUMOURID, OPDATE AS d) sub
GROUP BY TUMOURID;
Unpivot the data and then use count(distinct) or remove duplicates along the way:
select tumourid, count(*)
from ((select tumourid, avct_date as dte
from ct_joined
) union -- intentional to remove duplicates
(select tumourid, opdate as dte
from ct_joined
)
) t
group by tumourid;
Use UNION to avoid duplicate date & just use count(*) :
SELECT tumourid, COUNT(date)
FROM ((SELECT tumourid, avct_date AS date
FROM ct_joined
) UNION
(SELECT tumourid, opdate
FROM ct_joined
)
) t
GROUP BY tumourid;
All of the answers below work like a charm with a few tweaks to also account for rows with null values. For instance:
SELECT TUMOURID, COUNT(*)
FROM ((SELECT TUMOURID, AVCT_DATE AS DTE
FROM CT_JOINED
WHERE AVCT_DATE IS NOT NULL
) UNION
(SELECT TUMOURID, OPDATE AS DTE
FROM CT_JOINED
WHERE OPDATE IS NOT NULL
)
) T
GROUP BY TUMOURID;
Many thanks.
I want to count the distinct occurrences of some column grouped and non-grouped by two boolean columns
select count(Distinct some_column) as Uniques, sum(some_other_col)
from myTable T
where T.created_at>'2016-09-15'
group by T.col1, T.col2
This query gives four values
uniques when col1=true and col2=true
uniques when col1=true and col2=false
uniques when col1=false and col2=true
uniques when col1=false and col2=false
Is it possible to change the same query and to get these three values?
I can't get that info combining the first 4 values
uniques (all)
uniques when col1=true
uniques when col2=true
UPDATE
Actually I want to keep the group by because there are some other values that I get the sum.
Use conditional aggregation:
select count(Distinct some_column) as Uniques,
count(distinct case when t.col1 then some_column end) as Uniques_col1_true,
count(distinct case when t.col2 then some_column end) as Uniques_col2_true
from myTable t
where t.created_at > '2016-09-15';
Try this
SELECT SUM(CASE WHEN col1 = TRUE
AND col2 = TRUE THEN 1 ELSE 0 END) AS opt1,
SUM(CASE WHEN col1 = TRUE
AND col2 = FALSE THEN 1 ELSE 0 END) AS opt2,
FROM
(SELECT DISTINCT col1,
col2,
somecolumn
FROM TABLE T
WHERE ...) AS TB
I have one table and i want to check that for one column all value are same.
following is the entry in my table.
two column
rid,value
(1,1)
(1,1)
(2,1)
(2,0)
(2,0)
(3,0)
(3,0)
(3,0)
I want query which gives me rid 1 because all of its value is 1. all record for rid 1 has value 1 and rid 2 and 3 does not has all value as 1 so they should not be selected.
Using group by and having can get what you want:
SELECT rid, value
FROM my_table
GROUP BY rid
HAVING COUNT( distinct value) = 1
UPDATE
According to the comment, filter the value will get the result:
SELECT *
FROM
(
SELECT rid, value
FROM my_table
GROUP BY rid
HAVING COUNT( distinct value) = 1
) AS T1
WHERE value = 1
If the values would only be 1 or 0, then you could do this trick:
SELECT rid, value
FROM my_table
GROUP BY rid
HAVING COUNT( * ) = SUM(value)
You can do like this:
CREATE TABLE my_table (
id varchar(255),
col_value varchar(255)
);
INSERT INTO my_table
VALUES
('1','1'),
('1','1'),
('2','1'),
('2','1'),
('2','1'),
('2','4'),
('3','1'),
('3','1');
Query for selection:
SELECT src.* FROM
(
SELECT DISTINCT t1.* FROM my_table AS t1
) AS src
WHERE src.id NOT IN(
SELECT test.id
FROM
(
SELECT DISTINCT t1.* FROM my_table AS t1
) AS test
GROUP BY test.id
HAVING COUNT(*) > 1
)
fiddle here.
I pretty much forgot how to do this with SQL actually, the thing is I have an SQL select statement such as:
SELECT COUNT(*) NAME FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field`
Which does return a table with one field and many records containing numbers. What I want is to get the a single value of the maximum number among those records.
it should help you. just use MAX() function
SELECT MAX(COUNT(*)) FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field`
Just add LIMIT:
SELECT COUNT(`NAME`) AS `NUM` FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field` ORDER BY `NUM` DESC LIMIT 1
select max(name) from
(
SELECT COUNT(*) NAME FROM `SomeTable` WHERE `SomeID` = xxx GROUP BY `Field`)a
SELECT COUNT(*) NAME
FROM `SomeTable`
WHERE `SomeID` = xxx
GROUP BY `Field`
order by NAME desc
limit 1
I want is to get the a single value of the maximum number among those
records.
Try this:
SELECT t1.*
FROM SomeTable t1
(
SELECT SomeID, Max(Field) MaxField
FROM SomeTable
GROUP BY SomeID
) t2 ON t1.SomeID = t2.SomeID AND t1.Field = t2.MaxField
WHERE t1.SomeID = xxx
The syntax for the MAX function is:
SELECT MAX(expression )
FROM tables
WHERE predicates;
Example
SELECT MAX(salary) as "Highest salary"
FROM employees;
If you just want a single result of the highest value against a filter, just use Max:
SELECT MAX(MyField) NAME
FROM `SomeTable`
WHERE `SomeID` = xxx;
Alternatively, if you want an aggregate grouped by another column, the syntax is like so:
SELECT Name, MAX(MyField) as MaxOfMyField -- Or COUNT(MyField) if you want the Count
FROM `SomeTable`
WHERE `SomeID` = xxx
GROUP By Name;
I'm trying to make a statistics page in my php script. in order to select the count from each table I need more than 30 Queries like this
SELECT COUNT(order_id) as `uncompleted_orders` FROM `orders` WHERE `order_status` != 0
and then I need to run another query like this:
SELECT COUNT(order_id) as `completed_orders` FROM `orders` WHERE `order_status` = 1
I've tried this approach, but it didn't work:
SELECT COUNT(order_id) as `uncompleted_orders` FROM `sd_orders` WHERE `order_status` != 4;
SELECT COUNT(order_id) as `completed_orders` FROM `sd_orders` WHERE `order_status` = 4;
Is there any way to creat a new temp table in MySQL contains the count for other tables?
You could try something like this:
SELECT
(
SELECT COUNT(order_id) FROM `sd_orders` WHERE `order_status` != 4
) as `uncompleted_orders`,
(
SELECT COUNT(order_id) FROM `sd_orders` WHERE `order_status` = 4
) as `completed_orders`
You will have a result set with one row and a field for each count.
Without more information it's impossible to generalise, but there are many constructs that can help you here.
First, your example is actually from one table, and not two. This means that you can do the following...
SELECT
COUNT(CASE WHEN order_status = 4 THEN order_id END) AS complete_orders,
COUNT(CASE WHEN order_status <> 4 THEN order_id END) AS incomplete_orders
FROM
sd_orders
This works because COUNT(<something>) doesn't include an NULLs in the results. And by not including an ELSE clause, anything that doesn't match returns NULL. Another way people accomplish the same result is SUM(CASE WHEN ? THEN 1 ELSE 0 END).
Second, where you do actually have multiple tables, you can combine the results in several different ways...
-- Where you want one value from each table...
--------------------------------------------------------------------------------
SELECT
(SELECT COUNT(*) FROM table1 WHERE fieldx = ?) AS value1,
(SELECT COUNT(*) FROM table2 WHERE fieldy = ?) AS value2
-- Where you want one row of values from each table...
--------------------------------------------------------------------------------
SELECT
table1_summary.value1 AS table1_value1,
table1_summary.value2 AS table1_value2,
table2_summary.value1 AS table2_value1,
table2_summary.value2 AS table2_value2
FROM
(
SELECT
COUNT(CASE WHEN fieldx = ? THEN id END) AS value1,
COUNT(CASE WHEN fieldx <> ? THEN id END) AS value2
FROM
table1
)
AS table1_summary
CROSS JOIN
(
SELECT
COUNT(CASE WHEN fieldy = ? THEN id END) AS value1,
COUNT(CASE WHEN fieldy <> ? THEN id END) AS value2
FROM
table2
)
AS table2_summary
-- Where you want many rows, but of the same fields, from each table...
--------------------------------------------------------------------------------
SELECT
*
FROM
(
SELECT
'Table1' AS source_table,
fielda AS some_grouping,
COUNT(CASE WHEN fieldx = ? THEN id END) AS value1,
COUNT(CASE WHEN fieldx <> ? THEN id END) AS value2
FROM
table1
GROUP BY
fielda
UNION ALL
SELECT
'Table2' AS source_table,
fieldb AS some_grouping,
COUNT(CASE WHEN fieldy = ? THEN id END) AS value1,
COUNT(CASE WHEN fieldy <> ? THEN id END) AS value2
FROM
table2
GROUP BY
fieldb
)
AS summary
ORDER BY
source_table,
some_grouping,
value1,
value2
As you can see, there are a lot of ways to do this. How you approach it totally depends on your data and your needs.