count number of rows of a specific value IReport - mysql

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

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);

SQL: Creating new columns using Case When, Grouping_ID

How do I create an entirely new column in SQL while using CASE WHEN, GROUPING_ID(), and ROLLUP() syntax?
So far I have tried:
SELECT Country, ContactTitle, COUNT(ContactTitle) AS Count
,CASE(
WHEN
GROUPING_ID(Legend) = 0 THEN ' '
WHEN
GROUPING_ID(Legend) = 1 THEN 'SUBTOTAL(Country)')
GROUP BY ROLLUP(Country, ContactTitle)
FROM dbo.Customers
SELECT Country, ContactTitle, COUNT(ContactTitle) AS Count,
CASE
WHEN GROUPING_ID(Country,ContactTitle) = 0 THEN ''
WHEN GROUPING_ID(Country,ContactTitle) = 1 THEN CONCAT('Subtotal for ',Country)
END AS Legend
FROM dbo.Customers
GROUP BY ROLLUP(Country, ContactTitle);
A CASE needs an END.
And while testing, you could output the GROUPING_ID or GROUPING to understand what they return.
SELECT Country, ContactTitle, COUNT(*) AS Count,
(CASE
WHEN GROUPING_ID(Country, ContactTitle) = 1
THEN CONCAT('SUBTOTAL(',Country,')')
WHEN GROUPING(Country) = 1
THEN 'TOTAL'
ELSE ' '
END) AS Legend
FROM Customers
GROUP BY Country, ContactTitle WITH ROLLUP;

Adding two columns in mysql distinguish the count of male and female

I am trying to add two new columns to my query to be able to get the count of males and females
SELECT adggeth.reg01_maininfo.techname AS TechName
,adggeth.reg01_maininfo.techmobile AS Mobile
,monthname(adggeth.lng02_rpt_b_calvedets.calvdatealv) AS "Calving Month"
,count(adggeth.lng02_rpt_b_calvedets.sex) AS "No of Calves"
FROM adggeth.reg01_maininfo
INNER JOIN adggeth.lng02_maininfo ON adggeth.reg01_maininfo.techmobile = adggeth.lng02_maininfo.aitechid
INNER JOIN adggeth.lng02_rpt_b_calvedets ON adggeth.lng02_maininfo.hh_id = adggeth.lng02_rpt_b_calvedets.hh_id
AND adggeth.lng02_maininfo.visitdate = adggeth.lng02_rpt_b_calvedets.visitdate
GROUP BY adggeth.reg01_maininfo.techname
,adggeth.reg01_maininfo.techmobile
,monthname(adggeth.lng02_rpt_b_calvedets.calvdatealv);
I need to count the number of my female and male calves where
lng02_rpt_b_calvedets.sex = 1 refers to male calves
lng02_rpt_b_calvedets.sex = 2 refers to female calves
Just found my answer
SELECT
adggeth.reg01_maininfo.techname AS TechName,
adggeth.reg01_maininfo.techmobile AS Mobile,
MONTHNAME(
adggeth.`serv00_rpt_calvdtls2`.`calvdatealv2`
) AS "Calving Month",
COUNT(
adggeth.`serv00_rpt_calvdtls2`.`sex2`
) AS "No of Calves",
COUNT(
IF (
adggeth.serv00_rpt_calvdtls2.sex2 = 2,
adggeth.serv00_rpt_calvdtls2.sex2,
NULL
)
) AS 'Femal Calf',
COUNT(
IF (
adggeth.serv00_rpt_calvdtls2.sex2 = 1,
adggeth.serv00_rpt_calvdtls2.sex2,
NULL
)
) AS 'Male Calf'
FROM
adggeth.reg01_maininfo
INNER JOIN adggeth.`serv00_maininfo`
ON adggeth.reg01_maininfo.techmobile = adggeth.`serv00_maininfo`.`aitechid`
INNER JOIN adggeth.`serv00_rpt_calvdtls2`
ON adggeth.`serv00_maininfo`.`fid` = adggeth.`serv00_rpt_calvdtls2`.`fid`
AND adggeth.`serv00_maininfo`.`regdate` = adggeth.`serv00_rpt_calvdtls2`.`calvdatealv2`
GROUP BY adggeth.reg01_maininfo.techname,
adggeth.reg01_maininfo.techmobile,
MONTHNAME(
adggeth.`serv00_rpt_calvdtls2`.`calvdatealv2`
)
You can use a count call on a case expression that filters the calves according to their sex:
COUNT (CASE adggeth.lng02_rpt_b_calvedets.sex WHEN 1 END) as "No of Male Calves",
COUNT (CASE adggeth.lng02_rpt_b_calvedets.sex WHEN 2 END) as "No of Female Calves"
You can also use COUNT aggregation with IF function:
COUNT (IF(adggeth.lng02_rpt_b_calvedets.sex = 1, 1, NULL) ) AS Male_Calves_Count,
COUNT (IF(adggeth.lng02_rpt_b_calvedets.sex = 2, 1, NULL) ) AS Female_Calves_Count,
While COUNT will not count NULLs, and therefore it is possible to use some condition which returns NULL if it's not the value you're looking for, it's much simpler to use SUM instead. Boolean expressions in MySQL return 1 or 0, so you can add up their values directly.
SELECT
SUM(adggeth.serv00_rpt_calvdtls2.sex2 = 2) AS 'Female Calf',
SUM(adggeth.serv00_rpt_calvdtls2.sex2 = 1) AS 'Male Calf'
...

COUNT multiple types of same column

In my current query:
SELECT COUNT(WC.ID) AS "Regions"
FROM WHOLE_FEATURES_PDB_CHAINS AS WC
;
I COUNT(WC.ID) AS "Regions" .
However, we have multiple regions with WC.Type can be 1,2,3,4. I need to count each type occurrence into COUNT(WC.ID) AS "Region_1", COUNT(WC.ID) AS "Region_2" ... depending on WC.Type.
Is there any way to solve this in one query? I am looking at MySQL IF, yet do not know how to integrate it into the count function.
I need it to be in one row (the shown query here is reduced, it's a larger query)
SELECT COUNT(WC.ID) AS "Region_1" , COUNT(WC.ID) AS "Region_2" ...
Here is the complete query if anyone is interested:
SELECT PCS.PDB_id, PCS.Chain, PPA.ENSEMBL_start, PPA.ENSEMBL_end, PPA.eValue, PIN.TITLE AS "pdbTitle", COUNT(WC.ID) AS "Regions"
FROM PDB_Chains AS PCS
LEFT JOIN WHOLE_FEATURES_PDB_CHAINS AS WC ON WC.PDB_CHAIN_ID = PCS.idPDB_chains, PDB_protein_alignment PPA, PDB_INFOS PIN
WHERE PCS.idPDB_chains = PPA.idPDB_Chains
AND PCS.PDB_id = PIN.PDB_ID
AND PPA.idProteins = (SELECT idProteins from Proteins WHERE ENSEMBL_protein_id = "'+submittedID+'")
GROUP BY PCS.PDB_id, PCS.Chain ORDER BY PCS.PDB_id;
Here's the working solutin based on your kind answers
SELECT PIN.TITLE AS "pdbTitle", COUNT(CASE WHEN WC.STRUCTURAL_FEATURES_ID = 1 then 1 end) AS "PPInterface" , COUNT(CASE WHEN WC.STRUCTURAL_FEATURES_ID = 4 then 1 end) AS "flexibleRegions"
FROM PDB_Chains AS PCS LEFT JOIN WHOLE_FEATURES_PDB_CHAINS AS WC ON WC.PDB_CHAIN_ID = PCS.idPDB_chains, PDB_protein_alignment PPA, PDB_INFOS PIN
WHERE PCS.idPDB_chains = PPA.idPDB_Chains
AND PCS.PDB_id = PIN.PDB_ID
AND PPA.idProteins = (SELECT idProteins from Proteins WHERE ENSEMBL_protein_id = "ENSP00000256078.4")
GROUP BY PCS.PDB_id, PCS.Chain ORDER BY PCS.PDB_id;
You can use case when statement inside your aggregate function.
Try this .
count(case when WC.type = 1 then 1 end) as region_1, similarly repeat for another column.
Select
...
...
sum(if WC.ID = 1 then 1 else 0) as Region1,
sum(if WC.ID = 2 then 1 else 0) as Region2,
sum(if WC.ID = 3 then 1 else 0) as Region3,
sum(if WC.ID = 4 then 1 else 0) as Region4
Might do what you want.
You can use GROUP BY with COUNT to get the required result, e.g.:
SELECT WC.Type, COUNT(WC.ID) AS "Regions"
FROM WHOLE_FEATURES_PDB_CHAINS AS WC
GROUP BY WC.Type;
Update
If you want the counts as pivoted column for each region then you can write inner SELECT queries, e.g.:
SELECT
(SELECT COUNT(ID) FROM WHOLE_FEATURES_PDB_CHAINS WHERE type = 1) AS "Region_1",
(SELECT COUNT(ID) FROM WHOLE_FEATURES_PDB_CHAINS WHERE type = 2) AS "Region_2",
other_column
FROM WHOLE_FEATURES_PDB_CHAINS AS WC
WHERE <some condition>;

Group by a field that exist inside a nested query

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