How to generate the following statistic from a table? - mysql

I have a table named incident_summary that structure and data as following:
month,system_id,s_count
202104,1,50
202104,2,6
202105,1,14
202105,2,4
202106,1,1
202106,2,1
I would like to generate the following statistic:
s_count_on_202106,s_count_before_202106
2,74
where
s_count_on_202106 is sum of s_count value on 202106
s_count_before_202106 is sum of s_count value before 202106
I have tried the following SQL:
select
sum(case when month<202106 then s_count else 0 end)
sum(case when month=202106 then s_count else 0 end)
from incident_summary
group by month
However, it does not work, would you help to me to solve the problem?

Try the following Query.
May be it helps you.
SELECT
t1.s_count_on_202106,
t2.s_count_before_202106
FROM
(
SELECT
sum(s_count) AS s_count_on_202106
FROM
incident_summary
WHERE
month = 202106
) AS t1,
(
SELECT
sum(s_count) AS s_count_before_202106
FROM
incident_summary
WHERE
month < 202106
) AS t2

Sum again on your result set.
SELECT SUM(s_count_before_202106)s_count_before_202106, SUM(s_count_on_202106)s_count_on_202106
FROM
(
select
sum(case when month<202106 then s_count else 0 end)s_count_before_202106 ,
sum(case when month=202106 then s_count else 0 end)s_count_on_202106
from incident_summary
group by month
)T;

Related

Struggling with Pivot synta

Hi can someone tell me what is the error in this sample pivot command
SELECT *
FROM
(
SELECT *
FROM issued
) Src
PIVOT
( SUM(quantity)
FOR team IN (production,wastage,staff)
) AS Pvt;
I would do conditional aggregation instead :
select col,
sum(case when team = 'Production' then qty else 0 end) as production_qty,
sum(case when team = 'wastage' then qty else 0 end) as wastage_qty,
sum(case when team = 'staff' then qty else 0 end) as staff_qty
from issued
group by col --- use actual column name if any

Count different values in a column, while doing total and group by different column values

We have a table with data from different nodes and one of the column will have status report as "compliant or non-compliant", sample data as below
I want to filter the table in such a way that if any of the checks on a node shows non compliant, it should be flagged as non-compliant and rest as compliant. Using below query i am able to do it
SELECT COUNT(*) AS total_nodes,
SUM(fully_compliant = 0) AS Non_compliant_nodes,
SUM(fully_compliant = 1) AS compliant_nodes
FROM (
SELECT Node, CASE WHEN SUM(Status = 'Compliant') = COUNT(*) THEN 1 ELSE 0 END AS fully_compliant
FROM your_table GROUP BY Node
)
Now, i want to group and split the result by dept as below, how can i achieve this
I think you're looking for this:
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
sum(case when 'Non-Compliant' then 1 else 0 end) as non_compliant_chk
from your_table
group by dept,
node
) v
group by dept;
With few modifications to what Brian suggested, I am able to get the desired result
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
COUNT(CASE WHEN Compliance-Status = 'Non-Compliant' THEN 1 END) 'non_compliant_chk'
from table WHERE DOR >= DATE(NOW()) - INTERVAL 7 DAY
group by Dept,
Node
) v
group by Dept;

How to solve error "Subquery returns more than 1 row" in mysql?

i made a query such as the following in codeigniter
$query = $this->db->query('SELECT tanggal,
( SELECT COUNT(*) from absensi_siswa where kehadiran="alfa" GROUP BY tanggal ) AS alfa,
( SELECT COUNT(*) from absensi_siswa where kehadiran="izin" GROUP BY tanggal ) AS izin,
( SELECT COUNT(*) from absensi_siswa where kehadiran="hadir" GROUP BY tanggal ) AS hadir,
( SELECT COUNT(*) from absensi_siswa where kehadiran="sakit" GROUP BY tanggal ) AS sakit
FROM absensi_siswa GROUP BY tanggal');
return $query->result();
Your each sub clause (select count(*) .. ) returns morethan 1 rows, From your query i assume you need individual counts based on different values of kehadiran, you simplify your query as
SELECT tanggal,
SUM(CASE WHEN kehadiran="alfa" THEN 1 ELSE 0 END) AS alfa,
SUM(CASE WHEN kehadiran="izin" THEN 1 ELSE 0 END) AS izin,
SUM(CASE WHEN kehadiran="hadir" THEN 1 ELSE 0 END) AS hadir,
SUM(CASE WHEN kehadiran="sakit" THEN 1 ELSE 0 END) AS sakit
FROM absensi_siswa
GROUP BY tanggal
Using mysql you could make sum clause even shorter like sum(kehadiran="alfa") as alfa, Putting expression in sum(a=a) will return as a boolean 0/1 or true/false and you will get a count based on your expression

mySQL Select Query Statement

in my Table attendance
Insert Into tblAttendance(cEmpID,cCode,dDate,notMin) Values
('1000','R' , 120,'2016-10-27'),
('1000','R' , 120,'2016-10-28'),
('1000','S' , 120,'2016-10-29'),
('1000','L' , 120,'2016-10-30'),
('1001','R' , 120,'2016-10-27'),
('1001','R' , 120,'2016-10-28'),
('1001','S' , 120,'2016-10-29'),
('1001','L' , 120,'2016-10-30')
i need to sum all OT Min. per Code
the output something like this..
EmpID,R-Total,S-Total,L-Total
here's my Sample Query
Select (Select sum(nOTMin) from tblattenddetail Where cCode='R') 'R-Total',
(Select sum(nOTMin) from tblattenddetail Where cCode='S') 'S-Total',
(Select sum(nOTMin) from tblattenddetail Where cCode='L') 'L-Total'
i need to Include the cEmpID..
Please Help me how will i revise this to Include the cEmpID..
You can use a case expression to conditionally aggregate the correct values. This should do what you want:
SELECT
cEmpID,
SUM(CASE WHEN cCode='R' THEN nOTMin ELSE 0 END) AS 'R-Total',
SUM(CASE WHEN cCode='S' THEN nOTMin ELSE 0 END) AS 'S-Total',
SUM(CASE WHEN cCode='L' THEN nOTMin ELSE 0 END) AS 'L-Total'
FROM tblAttendance
GROUP BY cEmpID
Sample SQL Fiddle

MYSQL sum of a field depending on the value of other field

I am trying to create a field as
1.sum the purchases field where value of purchases type=vat as purchases_vat
2.sum the purchase field where value of purchase type=exempt as purchases_exempt
source table & result table example in attached image
example table
How about this?
SUM(CASE WHEN purchases_type='vat' THEN Purchases ELSE 0 END) AS purchases_vat,
SUM(CASE WHEN purchases_type='exempt' THEN Purchases ELSE 0 END) AS purchases_exempt,
Two row version:
SELECT 'vat', SUM(purchases)
FROM source_table
WHERE purchase_type = 'vat4'
UNION
SELECT 'exempt', SUM(purchases)
FROM source_table
WHERE purchase_type = 'exempt';
One row:
SELECT
(SELECT sum(purchases) from source_table where purchase_type = 'vat4') purchase_vat,
(SELECT sum(purchases) from source_table where purchase_type = 'exempt') purchase_exempt;
This will give you the sum of purchases for 'vat'. You can similarly do this for 'exempt'
SELECT `s` FROM (SELECT SUM(purchases), type FROM my_table GROUP BY type) as T1 WHERE T1.type = 'vat
select
sum(case when purchase_type = 'vat4' then purchases else 0 end) as purchase_vat4,
sum(case when purchase_type = 'exempt' then purchases else 0 end) as purchase_exempt
from source_table