This query:
SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count >1
Returns about 1500 (the number I'm looking for) results with only the count field. How could I also return the sum of all count fields? When I try
SELECT COUNT(source) AS count,
SUM(count) as total
FROM call_details
GROUP BY source
HAVING count >1
I get an 'Unknown column 'count' in 'field list' error.
And
SELECT COUNT(source) AS count,
SUM(COUNT(source)) as total
FROM call_details
GROUP BY source
HAVING count >1
gives me an 'Invalid use of group function'
Any ideas? I can do a mysql_num_rows($result) of the first set (to get the info I need) but I really want to do it through MySQL.
SELECT COUNT(count) FROM (SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count > 1) as A
You can't get a global total in a row-context. At the time the the COUNT() completes on any particular row, there's nothing to SUM, because the other rows haven't been calculated yet.
You'd have to run the SUM query first to get your individual stats, then sum manually in your script, or re-run the query with a surrounding SUM clause:
SELECT SUM(count) FROM (
SELECT original query here...
)
Try this
select mycount, sum(mycount) as sumcount
from
(SELECT COUNT(source) AS mycount FROM call_details GROUP BY source HAVING mycount >1) counttable
Assuming you are going to fetch all the results in the application anyway, I think the most efficient way would be to just sum it up in the application code.
Just simply remove the 'Group by' clause in the select query that counts
# first, get your counts by source
SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count >1
# then, get the overall total
SELECT COUNT(source) AS count
FROM call_details
HAVING count >1
Related
How can I select group by and without group by in one select query? I have a query like this
SELECT date, sum(total) as quotation_total
FROM suspended_bills
WHERE type='quotation'
GROUP BY YEAR(date), MONTH(date)
Here it is taking a group by year and month but I want a sum(total) without group by year and month. So I am able to get full sum(total) without a division of month and year.
This is not a duplicate entry as rollup is generating extra rows that I dont need, I only need the total of sum, so when I run this query with rollup it gave me more rows that is not desired. So all is wrong with roll up query.
In MySQL, the easiest way is a join or subquery:
select YEAR(sb.date), MONTH(sb.date), sum(sb.total) as quotation_total,
t.total_total
from suspended_bills sb cross join
(select sum(total) as total_totla
from suspended_bills
where type = 'quotation'
) t
where sb.type = 'quotation'
group by YEAR(sb.date), MONTH(sb.date), t.total_total;
I am running the following query on the table with multiple records having different quantity fields but the same id.
SELECT MIN( quantity )
FROM ( SELECT *
FROM `ready_for_delivery`
WHERE joborderid LIKE 00065
ORDER BY joborderid DESC ) a
GROUP BY quantity
It is returning all the values and not the minimum value. Any ideas why? Thanks in anticipation.
I think this is what you looking for:
SELECT MIN( quantity ) as 'Min' FROM ( SELECT * FROM `ready_for_delivery` WHERE joborderid LIKE 00065 ORDER BY joborderid DESC )a
If you are only after a single value, you should not be using a group by.
Definition for Group by:
A GROUP BY clause works on the rows returned by a query by summarizing identical rows into a single/distinct group and returns a single row with the summary for each group, by using appropriate Aggregate function in the
SELECT list, like
COUNT()
SUM()
MIN()
MAX()
AVG().
This is my code from tables
i want to select from it the first two and the last two rows as a result. is that possible?
in other words i want to select min and max of a column after it was summed. prefer two of each if possible
thanks a lot
`select A.prod_id, SUM(quantity) Total
from charging A
group by prod_id
order by total`
You can use a subquery. Because you didn't bother to include your query in the question, I am not going to re-type it. For just the values:
select min(Total), max(Total)
from (<your query here>) s
For four rows with both values:
(select t.*
from (<your query here>) t
order by Total asc
limit 2
) union all
(select t.*
from (<your query here>) t
order by Total desc
limit 2
)
In
select sum(orders) as total, count(*) as c, c / total as average
It gives an "Error Code: 1054. Unknown column..."
Is there any way to achieve this backreferencing without the very annoying work of wrapping the select in another select?
I imagine this is a duplicate but I haven't been able to find it, so, here's how I would ask it.
The aliased columns don't exist until the query is completed, so you can't use them in the same query.
Just repeat the expressions in the calculation:
select
sum(orders) as total,
count(*) as c,
count(*) / sum(orders) as average
from
yourtable
Or do the select and then the calculation:
select
s.total, s.c, s.c / s.total as average
from
(select
sum(orders) as total,
count(*) as c
from <yourtable>) s
Use the avg function:
select sum(orders) as total, count(*) as c, avg(orders) as average
Based on the column name, I assume you really want the average and not "1 / average" as expressed in your question.
I need a MySql query to do the following:
Select all the items in a table, group them by a type, order them by count type and also sum all the count.
This is what I've done so far:
select type, count(*) as cnt from $tbl_name group by type order by count(*) desc;
This only gives me the count for each group. What should I add so that this code will also show the total count (sum the counts from every group).
select * from (
select type as type,
count(*) as cnt
from $tbl_name
group by type WITH ROLLUP) as inner_table
order by cnt desc;
Note that the first row will be the rolled up total sum.
ROLLUP reference
Try this:
select type, SUM(count(*)) as cnt from $tbl_name group by type order by count(*) desc;