MySQL sum on condition does not work in a view - mysql

In MySQL (v5.5), the value of numNotCast, numInFavor is always 0 when selecting all rows of the view. If the select statement is executed alone (not in the view), it works as expected returning the correct count of the the number of rows in the vote column equal to value 'notcast' and 'infavor'.
CREATE OR REPLACE
VIEW `Stats` AS
select
sum(case when `p`.`vote` = 'notcast' then 1 else 0 end) AS `numNotCast`,
sum(case when `p`.`vote` = 'infavor' then 1 else 0 end) AS `numInFavor`
from
(`Debate` `d`
join `Participant` `p` ON ((`d`.`debateId` = `p`.`debateId`)))
group by `d`.`debateId`
Is this a limitation on MySQL views? How do you accomplish this conditional summing function in the view?

Then your statement is correct as SQLFiddle confirms, so your problem is probably somewhere else
CREATE OR REPLACE VIEW Stats AS
select
Debate.debateId,
sum(case when Participant.vote='notcast' then 1 else 0 end) as numNotCast,
sum(case when Participant.vote='infavor' then 1 else 0 end) as numInFavor
from
Debate
inner join
Participant on Participant.debateId = Debate.debateId
group by
Debate.debateId
Updated after the clarification

There are many limitations in MySQL views, but I don't think this is one of them.
I think it is strange that the name of the view is in single quotes. That might be allowed, but the view may not be doing what you want. Try this:
CREATE OR REPLACE VIEW `Stats` AS
select sum(case when `p`.`vote` = 'notcast' then 1 else 0 end) AS `numNotCast`
from `Debate` `d` join
`Participant` `p`
ON `d`.`debateId` = `p`.`debateId`
group by `d`.`debateId`;
By the way, in MySQL, you can simplify the select to:
select sum(p.vote = 'notcast') as numNotCast

Related

How do I calculate the difference of two alias for sorting

Considering the following code:
SELECT SUM(w.valor),
SUM(CASE WHEN w.tipo = '+' THEN w.valor ELSE 0 END) AS total_credit,
SUM(CASE WHEN w.tipo = '-' THEN w.valor ELSE 0 END) AS total_debit,
w.clientUNIQUE,
c.client as cclient
FROM wallet AS w
LEFT JOIN clients AS c ON w.clientUNIQUE = c.clientUNIQUE
WHERE w.status='V'
GROUP BY w.clientUNIQUE
ORDER BY total_credit-total_debit
I'm trying to calculate the difference of two aliased calculated values for sorting purposes, but I'm getting the following error:
Reference 'total_credit' not supported (reference to group function)
What am I doing wrong and how can I order results by using the difference value between the two aliases?
You can't refer to columns by their alias in the same select expression, so there are 2 options...
Repeat the expressions in the order by (yuk):
ORDER BY
SUM(CASE WHEN w.tipo = '+' THEN w.valor ELSE 0 END) AS total_credit -
SUM(CASE WHEN w.tipo = '-' THEN w.valor ELSE 0 END) AS total_debit
Or easier on the brain and easier to maintain (DRY), order via a sub query:
select * from (
<your query without the ORDER BY>
) q
ORDER BY total_credit - total_debit

Difference between these two mysql queries

I want to know the difference between these two queries: The first query is giving me all the records and its just fine.
Select * from table1 where tender_id='$tender_id' group by supplier_name
But in the following query I have added a sum(case), but I am not getting the desired output. The first query is showing all the records, but the second query is not showing all the records. What mistake am I making?
select cs.*, tender_id,
SUM(CASE WHEN ifmain = 'Yes' THEN total_inr ELSE 0 END) AS maintotal,
SUM(CASE WHEN ifmain = 'No' THEN total_inr ELSE 0 END) AS subtotal
from table1 cs
where cs.tender_id='$tender_id'
group by cs.supplier_name
I want to know if the second query can display all the records with conditions (tender_id)? or its iterating more?
In standard SQL, a query that includes a GROUP BY clause cannot refer
to nonaggregated columns in the select list that are not named in the
GROUP BY clause.
see (for example) https://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
MySQL has a an unfortunate (and in my opinion incorrect) default behavior when using a GROUP BY clause. This query would NOT be valid in most SQL databases:
Select * from table1 where tender_id='$tender_id' group by supplier_name
and it would not be valid in MySQL either if the ONLY_FULL_GROUP_BY SQL mode has been enabled.
I strongly recommend you treat all queries using GROUP BY as needing ALL non-aggregating columns in that clause. e.g.
select
cs.supplier_name
, SUM(CASE WHEN ifmain = 'Yes' THEN total_inr ELSE 0 END) AS maintotal
, SUM(CASE WHEN ifmain = 'No' THEN total_inr ELSE 0 END) AS subtotal
from table1 cs
where cs.tender_id='$tender_id'
group by
cs.supplier_name
If you need extra columns then e.g.
select
cs.supplier_name
, tender_id
, SUM(CASE WHEN ifmain = 'Yes' THEN total_inr ELSE 0 END) AS maintotal
, SUM(CASE WHEN ifmain = 'No' THEN total_inr ELSE 0 END) AS subtotal
from table1 cs
where cs.tender_id='$tender_id'
group by
cs.supplier_name
, tender_id
and so on. Of course as you include more columns in the group by clause you may increase the number of rows produced, but that is how GROUP BY should work.

Reduce the number of queries

I have seperate queries but i need to reduce the no so put all in one
select count(applicant_id) as registered from student_application where filter_status=0 AND
select count(applicant_id) as filer_select from student_application where filter_status=1 AND
select count(applicant_id) as filter_reject from student_application where filter_status=2
but this shows some errors
Use CASE expression.
Query
select
count(case when filter_status = 0 then applicant_id else null end) as registered,
count(case when filter_status = 1 then applicant_id else null end) as filer_select,
count(case when filter_status = 2 then applicant_id else null end) as filer_reject
from student_application;
SQL Fiddle
You could also use group_by, with the where clause if you're looking for a subset rather than all possible values of filter_status:
SELECT filter_status, COUNT(*)
FROM student_application
WHERE filter_status in (0, 1, 2)
GROUP BY filter_status;

How to get multiple SUM with one SQL query?

I am wondering how to fix this query.
SELECT
SUM(`month_using`) used,
SUM(`month_cash`) paycash,
SUM(`month_profit` CASE WHEN `id` <> 'system' THEN 1 ELSE 0 END) profit
FROM `_using_month`
WHERE MONTH(`month_date`) = {month} AND YEAR(`month_date`) = {year}
Thanks
Use Group by clause to get multiple SUM:
For Example:
SELECT YEAR(month_date), MONTH(month_date),
SUM(month_using) used, SUM(month_cash) paycash,
SUM(month_profit CASE WHEN id <> 'system' THEN 1 ELSE 0 END) profit
FROM _using_month
GROUP BY YEAR(month_date), MONTH(month_date)

Laravel group by and SUM

Having trouble getting my head around this one, it likely doesn't help I don't have any SQL 'group by' experience.
I have a table that has a transaction_type column and an amount column. Basically I am trying to use SQL/Eloquent to get the following in a single query (if possible, I know this isn't sql):
add = SUM(amount) where transaction_type == 1
delete = SUM(amount) WHERE transaction_type == 2
return from MySQL: add-delete
I'm assuming this would be done using groupby, but despite my best efforts I haven't be able to find a solution by reading the sql documentation.
I think I have got it using plain SQL, but how would I convert this to Eloquent:
SELECT `transaction_type`, SUM(`amount`) FROM `credit_logs` WHERE `to_group_id` = '1'
I can't help you with eloquent, but your query should be something like this:
SELECT
SUM(CASE WHEN transaction_type = 1 THEN amount ELSE NULL END)
-
SUM(CASE WHEN transaction_type = 2 THEN amount ELSE NULL END)
FROM credit_logs
WHERE to_group_id = 1;
or for all id's you wish to group by:
SELECT
SUM(CASE WHEN transaction_type = 1 THEN amount ELSE NULL END)
-
SUM(CASE WHEN transaction_type = 2 THEN amount ELSE NULL END)
FROM credit_logs
GROUP BY to_group_id;
Easiest would be, if you'd find a way to just execute a query with eloquent. Never understood why someone wants to transform a nice readable query into some "improved" syntax of a framework.