Group by a field that exist inside a nested query - mysql

i want to thank you for contributing to the answer here is an SQL fiddle
http://sqlfiddle.com/#!9/610e7/1
-- This query will return all the attributes of an issue
select * from dataissue where issue = '25998' .
What I want to do is :
sum(value) count(value)
where field = 'version(s)_corrigée(s)'
and value = 'Fermée'
and field = 'point_d_effort'
and value = 'récit'
and group it by value where field = 'version(s)_corrigée(s)'
for example this query return the group by but the sum explode because we use the field of the outer query :
sqlfiddle.com/#!9/610e7/4

I suspect that you want something like this:
select sum(case when field = 'point_d_effort' then value + 0 end), as v
sum(field = 'point_d_effort') as cnt
from dataissue
group by issue
having sum(field = 'version(s)_corrigée(s)' and value = 'Fermée') > 0 and
sum(field = 'Type de demande' and value = 'récit') > 0;
You have an entity-attribute-value (EAV) data structure. One method is to first aggregate by key. If you want the total across all issues, then you need another aggregation:
select sum(v), sum(cnt)
from (select sum(case when field = 'point_d_effort' then value + 0 end), as v
sum(field = 'point_d_effort') as cnt
from dataissue
group by issue
having sum(field = 'version(s)_corrigée(s)' and value = 'Fermée') > 0 and
sum(field = 'Type de demande' and value = 'récit') > 0
) t

Related

How can I combine two SQL queries returning multiple columns into separate columns of the same table?

I would like to combine the following SQL queries to return a single table with the column names:
mnth, correct_predictions, incorrect_predictions
ts is a timestamp type.
SELECT monthname(ts) as mnth, count(*) AS correct_predictions
FROM prediction
WHERE actual = 1 AND result = 1 OR actual = 0 AND result = 0
GROUP BY monthname(ts);
SELECT monthname(ts) as mnth, count(*) AS incorrect_predictions
FROM prediction
WHERE actual = 0 AND result = 1 OR actual = 1 AND result = 0
GROUP BY monthname(ts);
Since MySQL treats booleans as 1 or 0 in a numeric context, you can just SUM the result of comparing actual and result to get your required columns:
SELECT MONTHNAME(ts) as mnth,
SUM(actual = result) AS correct_predictions,
SUM(actual != result) AS incorrect_predictions
FROM prediction
GROUP BY mnth;
Note that using MONTHNAME will result in values from every year being grouped together (e.g. May 2019 with May 2020). That may be what you want (or perhaps you are restricting the range of the query with a WHERE clause) but if not, you should use something like
EXTRACT(YEAR_MONTH FROM ts) AS mnth
to include the year in the value you are grouping by.
Try the following with case
SELECT
monthname(ts) as mnth,
sum(case
when (actual = 1 AND result = 1) OR (actual = 0 AND result = 0) then 1 else 0
end)
AS correct_predictions,
sum(case
when (actual = 0 AND result = 1) OR (actual = 1 AND result = 0) then 1 else 0
end)
AS incorrect_predictions
FROM prediction
GROUP BY monthname(ts);

How to order table results by another rows value

I have created a query to select the record column in a table where other rows exist but i want to add an order by which orders by another rows value.
SELECT
record
FROM
record_attributes
GROUP BY
record
HAVING
sum(type = 'lead' AND option_name = 'pupil_cap' AND value > 0) > 0 AND
sum(type = 'lead' AND option_name = 'car_type' AND value = 'Manual') > 0 AND
sum(type = 'lead' AND option_name = 'instructor_areas_covered' AND value = '26') > 0 AND
sum(type = 'lead' AND option_name = 'diary_updates' AND value = '1') > 0
How can I order these results by another query in the same table with the following data:
ORDER BY record_attributes.value WHERE record_attributes.type = 'lead' AND record_attributes.option_name = 'instructor_rank'
And if the row doesn't exist, then use this:
ORDER BY record_attributes.value WHERE record_attributes.type = 'lead' AND record_attributes.option_name = 'instructor_start_date'
Not sure I understood correctly.
But something like this?
...
ORDER BY
MAX(CASE WHEN type = 'lead' AND option_name = 'instructor_rank' THEN value END),
MAX(CASE WHEN type = 'lead' AND option_name = 'instructor_start_date' THEN value END)
You can test it here on rextester.

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

count number of rows of a specific value IReport

I have a column Status and I'd like to count how many lines are "Pendings" and how many are "Finalized"
I tried this:
Variable class = java.lang.Integer
Calculation = Sum
ResetType = Report
Increment type = None
Variable expression = $F{tbl_suspensao_status}.equals("Finalized") ? 0 : 1
But it did not work.
Query:
SELECT
tbl_suspensao.`codigoBeneficiario` AS tbl_suspensao_codigoBeneficiario,
tbl_suspensao.`contrato` AS tbl_suspensao_contrato,
tbl_suspensao.`data_fim` AS tbl_suspensao_data_fim,
tbl_suspensao.`data_inicio` AS tbl_suspensao_data_inicio,
tbl_suspensao.`status` AS tbl_suspensao_status,
tbl_usuario.`nome` AS tbl_usuario_nome,
tbl_suspensao.`nomeBeneficiario` AS tbl_suspensao_nomeBeneficiario
FROM
`tbl_usuario` tbl_usuario INNER JOIN `tbl_suspensao` tbl_suspensao ON tbl_usuario.`codigo` = tbl_suspensao.`usuario_id`
WHERE
AND tbl_suspensao.`data_inicio` BETWEEN $P{Data_Inicio} AND $P{Data_Fim}
select
sum(if(Status = "Pendings", 1, 0)) Pendings,
sum(if(Status = "Finalized", 1, 0)) Finalized
from table
Or so
select
sum(Status = "Pendings") Pendings,
sum(Status = "Finalized") Finalized
from t1
Demo on sqlfiddle
SELECT status, count(*) from table where status='pending' or status='finalized'
group by status
Wrong order 1 and 0. Change expression to
Variable expression = $F{tbl_suspensao_status}.equals("Finalized") ? 1 : 0
P.S. Calculation sum in SQL is SQL-solution but not iReport-solution

how to get SUM() of COUNT() column in MySQL

i have the following query, tried using case statement
SELECT t.inst_id, t.inst_username, tcm.city_name,
SUM(CASE WHEN psb.pms_student_bucket_id IS NULL THEN 1 ELSE 0 END) AS not_assiged,
SUM(CASE WHEN COUNT(psb.pms_student_bucket_id) BETWEEN 1 AND 50 THEN 1 ELSE 0 END) AS '1-50',
SUM(CASE WHEN COUNT(psb.pms_student_bucket_id) > 50 THEN 1 ELSE 0 END) AS ' > 50'
FROM tbl_si_di t
JOIN tbl_city_master tcm ON tcm.city_id = t.city_ref_id
JOIN tbl_si_students tss ON tss.inst_ref_id = t.inst_id
LEFT JOIN pms_student_bucket psb ON psb.user_ref_id = tss.user_ref_id
GROUP BY t.inst_id;
I need SUM of pms_student_bucket_id column when their COUNT is '1-50' and '>50'. Right now this query is saying Invalid use of group function.
How would I SUM on COUNT of "pms_student_bucket_id" equals/between some value in mysql?
you could put it in a subquery
SELECT inst_id, inst_username, city_name,
SUM(pms_student_bucket_id IS NULL ) AS not_assiged,
SUM(num_bucket >=10 AND num_bucket <= 20) AS '10 - 20'
SUM(num_bucket <= 50) AS '1-50',
SUM(num_bucket > 50) AS ' > 50'
FROM
( SELECT t.inst_id, t.inst_username, tcm.city_name,psb.pms_student_bucket_id,
COUNT(psb.pms_student_bucket_id) as num_bucket
FROM tbl_si_di t
JOIN tbl_city_master tcm ON tcm.city_id = t.city_ref_id
JOIN tbl_si_students tss ON tss.inst_ref_id = t.inst_id
LEFT JOIN pms_student_bucket psb ON psb.user_ref_id = tss.user_ref_id
GROUP BY t.inst_id
)t1
GROUP BY inst_id;
note you can use the boolean values returned to do what you want.. aka you don't need a case statement, the boolean value returns a 1 or 0 which can then be summed..