I've been trying to figure out how to count all column values and add them to each other.
What i want to achieve is that my code will calculate all the price values, where check = 1 and add them to eachother as $total.
My goal : So i can divide the total result where check = 1, and check is 0, and subtract them from eachother.
I apologize for my paint skills.
Try this:
SELECT sum(price) as Total from YOUR_TABLE where `check` = 1;
You can try the following:
Step 1: Query all results from the table.
Step 2: Loop through the result.
Step 3: Create two array array1 and array2
Step 4: Inside the loop check condition if (check == 1) stored price value in array1 else if (check == 0) store in array2
Step 5: sum values of array1 and array2 in different variables and finally subtract from each other.
or try the following query:
SELECT SUM(CASE WHEN check='0' THEN price END) as zerossum,
SUM(CASE WHEN check='1' THEN price END) as onessum
FROM tablename
You can get the complete answer using just SQL if you want
SELECT ones - zeros as theTotal
FROM
(
SELECT SUM(CASE WHEN `check`=0 THEN price END) as zeros,
SUM(CASE WHEN `check`=1 THEN price END) as ones
FROM test
) test1;
Related
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
I've table named 'detail_transaksi'. I want to SUM all of the data in "jumlah" column where "debit_kredit" = 'D' which is grouped in the same "kode_akun" data. Also, I want to SUM all of the data in "jumlah" column where "debit_kredit" = 'K' which is grouped in the same "kode_akun" data. Then, I want to SUBSTRACT both of them. How can I do it in SQL or maybe in PHP?
Here's columns in my table:
id
kode_transaksi
kode_akun
debit_kredit
jumlah
You can use conditional aggregation:
select kode_akun,
sum(case when debit_kredit = 'K' then jumlah
when debit_kredit = 'D' then - jumlah
end)
from detail_transaksi
group by kode_akun;
You can use GROUP BY debit_kredit to grouping SUM of records by debit_kredit value
and after that, you can do subtract or whatever you want on your data.
SELECT debit_kredit , SUM(jumlah) AS total_sum FROM detail_transaksi GROUP BY debit_kredit
You need to sum if for both K and D in debit_kredit
The following should achieve that.
SELECT
kode_akun,
SUM(CASE WHEN debit_kredit='K' then jumlah else 0 end) as jumlah_k,
SUM(CASE WHEN debit_kredit='D' then jumlah else 0 end) as jumlah_d,
FROM detail_transaksi GROUP BY kode_akun
As for the difference between jumlah_k and jumlah_d you would have to subtract the sum expressions,this is rather inefficient, I would advice you to do that in 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;
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;
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.