Average of a Count in Select Statement - php

I can't seem to solve this riddle. I have the following statement:
SELECT schedule_test.ID, LEFT(schedule_test.Course,2) AS 'Type', COUNT(*) AS
'Count'
FROM schedule_test
GROUP BY ID, Type
It echos the following:
What can I add to the Select statement to add a 4th column with the average of the count for each Type? In other words, what was the average count of Type "HS" across all employees? Tried adding AVG(COUNT(*)) but I get an error, Invalid use of group function.

You can try this:
SELECT .......,
AVG(Count) AS average,
AVG(Count WHEN Type = 'HS' THEN Count ELSE NULL END) AS conditionalAverage
FROM schedule_test
GROUP BY ID, Type;

Related

SUM 2 values in mysql to make leaderboard

I want make a leaderboard from my database, I already make a code but the sql says error
"SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun)
MySQL said: Documentation
#1242 - Subquery returns more than 1 row"
heres my sql query :
SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun);
SET #a2 = (SELECT SUM(j.nilai_jawaban)
FROM jawaban j
GROUP BY j.id_akun);
SET #hasil = #a1 + #a2;
SELECT #hasil
Thank you !
this is an example data in table jawaban:
this is an example data in table diskusi:
and my desired answer:
where tera123 have 135 TOTAL(d.poin_diskusi+j.nilai_jawaban) and david123 have 90 TOTAL(d.poin_diskusi+j.nilai_jawaban)
the point is to show TOTAL for every user
It looks to me (without looking at your images; if you want to show information, please provide it in text) like you want something like:
select id_akun, sum(point_value) as total_points from (
select id_akun, sum(poin_diskusi) as point_value
from diskusi group by id_akun
union all
select id_akun, sum(nilai_jawaban) as point_value
from jawaban group by id_akun
) as point_values
group by id_akun
The problem are the GROUP BY keywords. Inside the images you provided with example data, one can see that there are multiple entries for the columns jawaban.id_akun and diskusi.id_akun. Because of that, both SELECT will return multiple rows as result, for each different value of the id columns one summed up row. Try it without the GROUP BY clause.

SQL count values from same column

Hi I have mysql table named content where i have a column "status" which have 3 values, converted, negotiating and received. now i want to count how many have status received, negotiating, and converted for developing a chart.
here is what i used:
SELECT status,
SUM(CASE WHEN status = 'converted' = 1 THEN 1 ELSE 0 END) AS converted,
SUM(CASE WHEN status = 'negotiating' = 1 THEN 1 ELSE 0 END) AS negotiating,
SUM(CASE WHEN status = 'Received NA' = 1 THEN 1 ELSE 0 END) AS ReceivedNA
FROM content GROUP BY status;
It shows me the result but in a way that i can not use it.
to feed my chart i used this:
$data = array(
array('converted', $converted),
array('negotiating', $negotiating),
array('received', $received)
);
So i guess some thing like this table will solve my problem:
status result
--------------------------- --------
converted 1
negotiating 5
received 4
So can anyone suggest how can modify my sql to get the expected result?
thanks again
Use GROUP By. Try this -
SELECT status, count(status) result FROM content GROUP BY status
To get the distinct count use GROUP BY.
select status,count(1) as result from content GROUP BY status;
Instead of using sum, count is always a better and easier way
EDIT-to answer the comment
The parameter to the COUNT function is an expression that is to be evaluated for each row. The COUNT function returns the number of rows for which the expression evaluates to a non-null value. ( * is a special expression that is not evaluated, it simply returns the number of rows.)
There are two additional modifiers for the expression: ALL and DISTINCT. These determine whether duplicates are discarded. Since ALL is the default, your example is the same as count(ALL 1), which means that duplicates are retained.
Since the expression "1" evaluates to non-null for every row, and since you are not removing duplicates, COUNT(1) should always return the same number as COUNT(*).
Will this work for you?
SELECT status, count(status) FROM content GROUP BY status;

Get number os each duplicated items on table

I'm getting duplicate items of my table doing this:
SELECT * FROM table GROUP BY name HAVING COUNT(*) >1
It returns each duplicate item. But I would like to show:
"repeat X times".
How can I "echo" the "count" in this case?
You should show the count(), instead of *:
select name, count(*)
from table
group by name
having count(*) > 1;

Mysql Join Two Queries

I have two queries:
SELECT opr, COUNT(*) FROM table WHERE field = 'YES' GROUP BY opr
SELECT opr, MAX(category) FROM table WHERE field = 'NO' GROUP BY opr
So basically in the first query I am getting the number of transactions a user makes.
In the second query I am getting a category that all of those transactions fall under. I don't want to get all categories for each transaction they made, just the Max of the category field so that I have one entry per operator.
Until now, I have been catching both result sets in separate arrays and the looping through both arrays to get the complete opr->picks->category.
This doesn't always work it sometimes associates the wrong category to the wrong operator.
Is there a way to combine these two queries into one, so that I get the operator and picks, then the MAX(category)? The issue is that the conditions for each query are different on the field column.
SELECT opr,
COUNT(CASE WHEN field = 'YES' THEN 1 END) AS number_of_transactions,
MAX(CASE WHEN field = 'NO' THEN category END) AS category
FROM table
GROUP BY opr ;
The above logic can be attained in one query by using CASE
SELECT opr, COUNT(CASE WHEN `field` = 'YES' THEN opr END) as `count`,
MAX(CASE WHEN `field` = 'NO' THEN category END) as `max`
FROM table GROUP BY opr
Just add it in:
SELECT opr,count(*), MAX(category) FROM table WHERE field = 'YES' GROUP BY opr
You might also be able to drop it from 2 queries to 1 with this:
SELECT opr,count(*), MAX(category), field FROM table GROUP BY opr, field
even faster on the processor, use a sum bool:
select
opr,
sum(field='yes') as transactions,
max(if(field='no',category, null)) as category
from table
group by opr

SQL Server get number of rows

I have a table called replies and it has a column named topic_id.
What I want to do is count how many rows have the topic_id 1.
I tried
SELECT COUNT(*)
FROM [***].[replies]
WHERE [***].[topic_id] = '1'
But in this case I don't know if this is a valid query. If it is, how can I pull it back into a variable called $num_rows in my php code?
Note: * is actually a prefix I covered.
Something like this should work:
--declare the variable
DECLARE #num_rows INT
--get the total number of rows
SELECT #num_rows = COUNT(*) FROM replies WHERE topic_id = 1
--get a distinct count of rows (if for some reason you need it)
SELECT #num_rows = COUNT(DISTINCT reply_id) FROM replies WHERE topic_id = 1
--return the result set
SELECT #num_rows AS num_rows
If you just want to return a column called num_rows though, this would be a much more efficient approach:
SELECT COUNT(*) AS num_rows FROM replies WHERE topic_id = 1

Categories