SQL count values from same column - php

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;

Related

PHP SQL Case when value like

I am trying to use case when to count the total when the column 'Name' has the value 'apple' in it.
So for example, I have 3 values in my column 'Name'.
hallo (apple), hallo today, hallo (apple)
I am trying to count the result which contains apple and I do not want it in the where part of the statement.
I am trying to accomplish this with the following statement, but I get a SQL error around the like operator. Is what I want possible and if so how do I do it?
SELECT
COUNT(CASE WHEN Name = LIKE '%(apple)%' THEN 1 ELSE null end) AS TotalApples
FROM account WHERE id IN (0,$in)
I expect the result of this query to be 2 as the (apple) is 2 times present in my example.
If you use the LIKE operator, you do not need the = operator
Hence, for your case please change
SELECT
COUNT(CASE WHEN Name = LIKE '%(apple)%' THEN 1 ELSE null end) AS TotalApples
FROM account WHERE id IN (0,$in)
to
SELECT
COUNT(CASE WHEN Name LIKE '%(apple)%' THEN 1 ELSE null end) AS TotalApples
FROM account WHERE id IN (0,$in)
Note: The clause WHERE id IN (0,$in) will only retrieve data where id is 0 or equal to $in. If this is not the case you want then please amend this where clause

how can i select data and sub query group by

When i select data only show first lot data . but i need all lot data.
Here is my query:
SELECT lot,
(select count(pass) FROM pass_fail_result where pass=0) toatl_fail,
(select count(pass) FROM pass_fail_result where pass=1) toatl_pass FROM pass_fail_result group by lot;
I want to show all pass result like pass=10 and fail=2
The easiest way to do this is via conditional aggregation, where we count or sum CASE expressions which target the failing or passing records:
SELECT
lot,
COUNT(CASE WHEN pass = 0 THEN 1 END) AS toatl_fail,
COUNT(CASE WHEN pass = 1 THEN 1 END) AS toatl_pass
FROM pass_fail_result
GROUP BY
lot;

Average of a Count in Select Statement

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;

mysql - select a column based on a combination of values from another column

hi i need to get the plistid based on a given combination of avid. For example i submitted a combination of 1 and 4 avid, it should return a plistid of 1. Another example is when i submitted a combination of 2 and 5 avid it should return a plistid of 5. And if i submitted 1 and 3 it should return nothing since they are of the same attributeid.
How can i generate it in mysql
here is my table
NOTE: avid can be a combination of 1,2,3 or 4 numbers depending on the number of avid's submitted.
First, find all the matches of avid by plistid and then check that they have different attributeid. You can do this with aggregation, as a variation of a set-within-sets query:
select plistid
from t
group by plistid
having sum(avid = 1) > 0 and
sum(avid = 4) > 0 and
count(distinct case when avid in (1, 4) then attributeid end) = 2
The first two conditions in the having clause are saying what avids you want. They are counting the number of times that avid appears with one of the values, each returns true only when at least one value is found. The last is saying that you need distinct attribute ids on the rows.
Try:
select plistid
from t
where avid in (1,4)
group by plistid
having count(distinct avid) =2
What about this?
SELECT t1.plistid
FROM t t1
JOIN t t2
ON t1.attributeid != t2.attributteid
AND t1.plistid = t2.plistid
WHERE
t1.avid = 1
AND t2.avid = 4
try this one:-
select a.plistid
from your_table a, your_table b
where a.plistid = b.plistid
and a.avid = <first param>
and b.avid = <second param>
and a.attributeid <> b.attributeid;

counting rows matching criteria in mysql join , union >= and <=?

Select * FROM table WHERE a<=9 AND a>=4 AND b<=20 AND b>=16 AND c<=30 AND c>=26
Now I want to end up selecting from query 1 the results in which at least two of the following criteria are met.
Select * FROM table WHERE a<=7 AND a>=5 AND B<=19 AND B>=17 AND C<=29 AND c>=27
Numbers could be anything, although on query 2 these are lower for the less than eqaul and higher for the bigger than equal.
I want to be able to set the number of minimum coincidences to be met at query 2 with a number.
For example 5 coincidences must be met at query 2 to end up making a selection in query 1.
This is about range and matches count, if there is an easier way to achieve this great.
i think this kind of query will do your job
Select
*,
(IF(a<=7 AND a>=5,1,0) + IF(B<=19 AND B>=17,1,0) + IF(C<=29 AND c>=27,1,0))
as totalmatch
FROM table
HAVING totalmatch >=2
Try to use native MySQL function BEETWEN:
SELECT * FROM t1 WHERE key_col BETWEEN '2' AND '3';
Think it is something like this you want for the second query:-
Select *, CASE WHEN a<=7 THEN 1 ELSE 0 END + CASE WHEN a>=5 THEN 1 ELSE 0 END + CASE WHEN B<=19 THEN 1 ELSE 0 END + CASE WHEN B>=17 THEN 1 ELSE 0 END + CASE WHEN C<=29 THEN 1 ELSE 0 END + CASE WHEN c>=27 THEN 1 ELSE 0 END AS MatchCount
FROM table
WHERE a<=7
OR a>=5
OR B<=19
OR B>=17
OR C<=29
OR c>=27
HAVING MatchCount >= 2
Probably using the first query as a subselect instead of table.
EDIT - reading your latest response I think I misunderstood your requirement. However you could possibly find the differences between each values, get the absolute value of each difference and add them together to see how close / far the differences are.

Categories