sql Calculate boys and girls present and absent - php

Can't manage to come up with a correct query to compute the total no of students
I have three tables:
student - contains student profile either male or female
2.student_attendance - contains attendance details for whether a student was present a either "0" or "1"
attendance - contains all session details where by a one session can be attended by a number of students.
I need to calculate the number of boys/girls in present or absent for a session.
my major headache is to interpreate these logic to sql
if(in_attendace =1) then
sum the number of boys as boys_present
sum the number of girls as girls_present
else
sum the number of boys as boys_absent
sum the number of girls as girls_absent
# MY closest sql is its not working :(
select
case when a.in_attendance = 1 then
SUM(CASE b.gender when 1 then 1 else 0 end ) as male_present,
SUM(CASE b.gender when 2 then 1 else 0 end ) as female_present,
ELSE
SUM(CASE b.gender when 1 then 1 else 0 end ) as male_absent,
SUM(CASE b.gender when 2 then 1 else 0 end ) as female_absent
END
from attendance_student as a inner join student as b on a.student_id = b.id where a.session_details_id = 38

Well, you are not very far from the solution, you just need to separate them into different columns(I assume that's what you want) :
select COUNT(CASE WHEN a.in_attendance = 1 and b.gender = 1 then 1 END) as male_present,
COUNT(CASE WHEN a.in_attendance = 1 and b.gender = 2 then 1 END) as female_present,
COUNT(CASE WHEN a.in_attendance = 0 and b.gender = 1 then 1 END) as male_absent,
COUNT(CASE WHEN a.in_attendance = 0 and b.gender = 2 then 1 END) as female_absent
FROM attendance_student a
INNER JOIN student b
ON a.student_id = b.id
WHERE a.session_details_id = 38

Related

how to pass IF condition in sum case when

#my code
select('a.client_location_id as location_id','b.name as location_name','c.name as client_name',
DB::raw('SUM(CASE WHEN a.type = 1 employee_qty THEN 1 ELSE 0 END) as e_qty_in'),
DB::raw('SUM(CASE WHEN a.type = 2 employee_qty THEN 1 ELSE 0 END) as e_qty_out')
)
i want to do IF conditioning like , if type = 1 result is SUM employee_qty AS e_qty_in and if type 2 the result is SUM employee_qty AS e_qty_out,
#table

SQL multiple rows in single row

Hi everyone I have this table and I want to show result in single row group by client id and show availability for stock 1 and 2.
Here is my table
id client stock material quantity availability date
62 56 1 0 0 100 2017-12-16 23:55:01
63 56 2 0 0 900 2017-12-16 23:55:01
64 56 1 100 -20 80 2017-12-16 23:55:20
65 56 1 80 100 180 2017-12-16 23:56:06
66 56 1 180 200 380 2017-12-16 23:56:21
67 56 1 380 500 880 2017-12-16 23:58:11
68 56 1 880 -580 300 2017-12-16 23:58:38
69 56 2 900 -90 810 2017-12-17 23:59:18
Outcome I want is get result from last date, group by client id and combine stock 1 and stock 2 to single row
client availability1 availability2
56 300 810
I try this query
SELECT
historys.id
,(CASE WHEN historys.stock = 1 THEN availability END) AS availability1
,(CASE WHEN historys.stock = 2 THEN availability END) AS availability2
FROM historys
GROUP BY historys.client
ORDER by historys.id
The result is
id availability1 availability2
56 NULL 810
I will be grateful if someone help me. Thanks.
You need to filter to the right rows before the aggregation. Here is one method:
SELECT h.client,
MAX(CASE WHEN h.stock = 1 THEN h.availability END) AS availability1
MAX(CASE WHEN h.stock = 2 THEN h.availability END) AS availability2
FROM historys h
WHERE h.date = (SELECT MAX(h2.date) FROM historys h2 WHERE h2.stock = h.stock)
GROUP BY h.client
Use a union
SELECT
client, max(availability1) AS availability1, max(availability2) AS availability2
FROM
(
SELECT
client
,availability AS availability1
,0 AS availability2
FROM historys hist
WHERE id = (select max(id) from historys where client = hist.client and stock = 1)
UNION ALL
SELECT
client
,0 AS availability1
,availability AS availability2
FROM historys hist2
WHERE id = (select max(id) from historys where client = hist2.client and stock = 2)
) a
GROUP by client
ORDER by client
The NULL you have is because your selecting data from lines of historys that cannot contain the availability for stock = 1 and stock = 2 at the same time.
You can bypass that using historys both times like above :
In the subrequest (as d) we get max dates by client, then we join history two times to get both availability in a row.
select d.client,
h1.availability availability1,
h2.availability availability2
from
(
select client,
max(case when stock = 1 then date end) d1,
max(case when stock = 2 then date end) d2
from historys
group by client
) d
join historys h1 on (h1.stock = 1
and h1.date = d.d1
and h1.client = d.client)
join historys h2 on (h2.stock = 2
and h2.date = d.d2
and h2.client = d.client)
You can find an SQLFiddle here : http://sqlfiddle.com/#!9/d1ea4/5
Thanks
This may help you :
SELECT T.client client ,
SUM(CASE WHEN T.Stock = 1 THEN T.availability
END) availability1 ,
SUM(CASE WHEN T.Stock = 2 THEN T.availability
END) availability2
FROM historys T
INNER JOIN ( SELECT MAX(date) Max_Date ,
stock
FROM historys
GROUP BY stock
) T1 ON T1.Max_Date = T.date
AND T1.stock = T.stock
GROUP BY T.client
Depending on your MySQL version, you should be able to do this with a window function & a common table expression:
-- Build derived src table with "latest date" rows
WITH src AS (
SELECT id, client, stock, material, quantity, availability, date
FROM (
SELECT
id, client, stock, material, quantity, availability, date,
ROW_NUMBER() OVER(PARTITION BY client, stock ORDER BY date DESC) ClientStockRank -- For each (client, stock) pair, rank each row by date
FROM historys a
) src
WHERE ClientStockRank = 1 -- Only get rows with "latest date"
)
SELECT
client,
MAX(CASE WHEN stock = 1 THEN availability ELSE NULL) END AS Availability1, -- Get "max" value for "stock 1"
MAX(CASE WHEN stock = 2 THEN availability ELSE NULL) END AS Availability2 -- Get "max" value for "stock 2"
FROM src
GROUP BY client
I think you need MySQL 8+. If not, there should be other ways to do it too. Let me know if that works.
Update
Actually, this should also suffice:
SELECT DISTINCT
client,
MAX(CASE WHEN stock = 1 THEN availability ELSE NULL) OVER(PARTITION BY client ORDER BY date DESC) AS Availability1,
MAX(CASE WHEN stock = 2 THEN availability ELSE NULL) OVER(PARTITION BY client ORDER BY date DESC) AS availability2
FROM historys

select column names whose entries are value 1

I want to retrieve columns name from table whose value is 1
My query is give below...
mysqli_query($conn,"SELECT * FROM `tbl_therapist_schedule` WHERE `schedule_date`='30.11.2017'");
My table structure is give below...
slot1 slot2 slot3
1 1 0
2 1 1
1 1 2
3 1 0
my result...
slot1 slot2 slot3
I have the serious feeling that your data is not normalized. Instead of having separate columns for each slot, I would store all this data in a single column with metadata to relate each slot's state.
That being said, one way to get the output you want would be to aggregate over each slot column and check for the presence/absence of at least one 1 value. I then use GROUP_CONCAT to generate a CSV list of matching slots.
SELECT GROUP_CONCAT(slots)
FROM
(
SELECT
CASE WHEN SUM(CASE WHEN slot1 = 1 THEN 1 ELSE 0 END) > 0
THEN 'slot1' END AS slots
FROM tbl_therapist_schedule
WHERE schedule_date = '30.11.2017'
UNION ALL
SELECT
CASE WHEN SUM(CASE WHEN slot2 = 1 THEN 1 ELSE 0 END) > 0
THEN 'slot2' END
FROM tbl_therapist_schedule
WHERE schedule_date = '30.11.2017'
UNION ALL
SELECT
CASE WHEN SUM(CASE WHEN slot3 = 1 THEN 1 ELSE 0 END) > 0
THEN 'slot3' END
FROM tbl_therapist_schedule
WHERE schedule_date = '30.11.2017'
) t;
Demo

For loop inside SQL query

I have a table named watersourcetype consisting of water types.
link : watersourcetype TABLE
and another table health_and_sanitation consisting of household no. and watersource_id
I have this query :
SELECT h.purok_number,
SUM(CASE WHEN hs.watersystem = 'Community water system-own' THEN 1 ELSE 0 END) AS a,
SUM(CASE WHEN hs.watersystem = 'Community water system-shared' THEN 1 ELSE 0 END) AS b,
SUM(CASE WHEN hs.watersystem = 'Deep well-own' THEN 1 ELSE 0 END) AS c,
SUM(CASE WHEN hs.watersystem = 'Deep well-shared' THEN 1 ELSE 0 END) AS d,
SUM(CASE WHEN hs.watersystem = 'Artesian well-own' THEN 1 ELSE 0 END) AS e,
SUM(CASE WHEN hs.watersystem = 'Artesian well-shared' THEN 1 ELSE 0 END) AS f,
SUM(CASE WHEN hs.watersystem = 'Dug/shallow well-own' THEN 1 ELSE 0 END) AS g,
SUM(CASE WHEN hs.watersystem = 'Dug/shallow well-shared' THEN 1 ELSE 0 END) AS h,
SUM(CASE WHEN hs.watersystem = 'River, stream, lake, spring, bodies of water' THEN 1 ELSE 0 END) AS i,
SUM(CASE WHEN hs.watersystem = 'Bottled water' THEN 1 ELSE 0 END) AS j,
SUM(CASE WHEN hs.watersystem = 'Tanker truck/Peddler' THEN 1 ELSE 0 END) AS k
FROM health_and_sanitation AS hs, house_hold AS h, f_member as f
WHERE
h.brgy_name='$brgy_name' AND
h.hh_number=hs.hh_number AND
h.hh_number=f.hh_number AND
f.is_household='HOUSEHOLD' AND
EXTRACT(YEAR FROM f.reg_date) BETWEEN '$sel_year' AND '$sel_year'
group by h.purok_number
order by h.purok_number
what i want is to put a for loop inside above sql query since table watersourcetype is dynamic another data will be added to watersourcetype soon so i dont have to define in my case statement on above query the watersystem . The query should look like this :
$qry = pg_query("select cwatertype from tbl_watersourcetype");
SQL:
SELECT h.purok_number,
// is this possible ? putting a while loop or forloop inside a query in PHP ?
while($row = pg_fetch_array($qry))
{
SUM(CASE WHEN hs.watersystem = '$row['cwatertype']' THEN 1 ELSE 0 END) AS a
}
FROM health_and_sanitation AS hs, house_hold AS h, f_member as f
WHERE
h.brgy_name='$brgy_name' AND
h.hh_number=hs.hh_number AND
h.hh_number=f.hh_number AND
f.is_household='HOUSEHOLD' AND
EXTRACT(YEAR FROM f.reg_date) BETWEEN '$sel_year' AND '$sel_year'
group by h.purok_number
order by h.purok_number
is that possible ?
If I understand your query correctly, you are trying to retrieve the number of watersourcetypes per household, with a single record returned per household. If that is indeed the case you need to use the crosstab() function from the tablefunc extension.
However, if you can get by with multiple rows giving the number of sources per watersourcetype per household then you can simply and more efficiently use the COUNT() aggregate in the SELECT statement (with some decent formatting for legibility):
SELECT h.purok_number, hs.watersystem, COUNT(hs.watersystem) AS num
FROM health_and_sanitation AS hs,
house_hold AS h,
f_member AS f
WHERE h.brgy_name='$brgy_name'
AND h.hh_number=hs.hh_number
AND h.hh_number=f.hh_number
AND f.is_household='HOUSEHOLD'
AND EXTRACT(YEAR FROM f.reg_date) BETWEEN '$sel_year' AND '$sel_year'
GROUP BY h.purok_number, hs.watersystem
ORDER BY h.purok_number;
Additionally, I am assuming that your health_and_sanitation table is a view on the watersourcetype table; otherwise you are duplicating data in two tables which is (generally) a big boo-boo. Given also your issue with additional water source type, you may want to see if your database design is in 3NF.
Cheers,
Patrick
this is an example on how to put for loop inside SELECT statement :
$qry_6_12 .= " SELECT count(ncropfarmingreasonid) as counted , " ;
for($i=2;$i<=$count_row;$i++) // loop the number of rows and used $i as ncropfarmingreasonid
{
if(($count_row-$i)==0)
{
$qry_6_12 .= "SUM(CASE WHEN ncropfarmingreasonid = ".$i." THEN 1
ELSE 0 END) a".$i."";
}
else
{
$qry_6_12 .= "SUM(CASE WHEN ncropfarmingreasonid = ".$i." THEN 1
ELSE 0 END) a".$i.",";
}
}
$qry_6_12 .= " FROM tbl_climatechange as c, tbl_household as h, tbl_barangay as b where h.chholdnumber=c.chholdnumber and b.cbrgycode=h.cbrgycode and b.cbrgyname = 'AMPAYON' ";
$query_6_12 = pg_query($qry_6_12);

MySQL Count Records Based On Criteria Within A Group

I have a table containing the following data columns:
ord_number check
OR123 0
OR125 1
OR123 2
OR123 0
OR124 0
OR124 0
OR125 0
OR123 0
The check column indicates if we have fully supplied the order line with a value of zero or below meaning 'yes we have supplied complete' and a value of 1 or above meaning 'no we have not supplied complete' .
To calculate an order fill rate (how many orders have been filled complete) I need to know how many of the orders have only zero check results for each entry and how many have at least one positive check result.
My logic is that I need to GROUP the items and then carry out a COUNTIF within the group (this may be the wrong SQL methodology but logically it makes sense to me in terms of the outcome I'm looking for). From this query I need to return those two values into my PHP script.
The results from this data set would be:
Complete = 1 (orders with only zero checks)
Incomplete = 2 (orders where one or more lines have positive checks)
I've spent the morning trying to find a solution but can't find anything that I either understand enough or is similar enough that I can break it down and understand it.
If someone can help me by pointing me in the right direction or can provide a sample with an explanation I would be very grateful.
For 1 row with 2 counter fields use:
SELECT
SUM(CASE WHEN `check` = 1 THEN 1 ELSE 0 END) AS incomplete,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) AS complete
FROM orders
For 2 rows with 1 name use:
SELECT COUNT(`ord_number`) AS fulfillment
FROM orders WHERE (`check`=0)
UNION SELECT COUNT(ord_number)
FROM orders WHERE (`check`=1)
in which case you need to put row 1 into $complete (check=0)
and row 2 into $incomplete (check=1)
you may add:
UNION SELECT COUNT(ord_number) FROM orders
which will give you the total records on row 3.
output will be:
SELECT * FROM orders
ord_number check
---------------------
OR123 0
OR124 1
OR125 0
OR126 0
OR127 0
OR123 0
OR125 1
SELECT COUNT(`ord_number`) AS counts FROM orders or1 WHERE (`check`=0)
UNION SELECT COUNT(ord_number) FROM orders or2 WHERE (`check`=1)
UNION SELECT COUNT(ord_number) FROM orders
counts
-------
5
2
7
SELECT
SUM(CASE WHEN `check` = 1 THEN 1 ELSE 0 END) AS incomplete,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) AS complete FROM orders
incomplete complete
-------------------------
2 5
As for PHP:
for 1 row:
mysql_select_db($database_test, $test);
$query_countIn1Row = " SELECT SUM(CASE WHEN `check` = 1 THEN 1 ELSE 0 END) AS
incomplete,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) AS complete
FROM orders";
$countIn1Row = mysql_query($query_countIn1Row, $test) or die(mysql_error());
do {
$varIncomplete = $row_countIn1Row['incomplete'];
$varComplete = $row_countIn1Row['complete'];
} while ($row_countIn1Row = mysql_fetch_assoc($countIn1Row));
and for 1 column with counts:
mysql_select_db($database_test, $test);
$query_countOrders = "SELECT COUNT(`ord_number`) AS counts
FROM orders or1 WHERE (`check`=0)
UNION SELECT COUNT(ord_number) FROM orders or2 WHERE (`check`=1)
UNION SELECT COUNT(ord_number) FROM orders";
$countOrders = mysql_query($query_countOrders, $test) or die(mysql_error());
$myCounts = array();
do {
$myCounts[]= $row_countOrders['counts'];
} while ($row_countOrders = mysql_fetch_assoc($countOrders));
$complete = $myCounts[0];
$incomplete = $myCounts[1];
$total = $myCounts[2];
SELECT ord_number,IF(MAX( check) <=0 ,'COMPLETED','PENDING')
from TABLENAME
group by (ord_number )
Through this oneline query you will easily get which order is in pending status and which one is complete status
AS per your given details
The check column indicates if we have fully supplied the order line
with a value of zero or below meaning 'yes we have supplied complete'
and a value of 1 or above meaning 'no we have not supplied complete' .
it means check column can contain negative value as well as 0 for indicating the complete status and 1 or any postive integer number to indicate the pending status.
so the logic should be find the max(check) so if the max for a order is found 0 or any negative number then we will sure that order is complete and if it found a positive number it means there was a pending entry.
You can do this in two steps:
SELECT ord_number,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) as Comp,
COUNT(`check`) as total
FROM tab1
GROUP BY ord_number
This would give you the number of rows with 0 (complete) and the total number of rows for each ord_number.
Then you do a COUNT to validate when those two are equal:
SELECT CASE WHEN comp = total THEN 'COMPLETE' ELSE 'INCOMPLETE' END AS status,
COUNT(*) AS statusCount
FROM(
SELECT ord_number,
SUM(CASE WHEN `check` = 0 THEN 1 ELSE 0 END) as Comp,
COUNT(`check`) as total
FROM tab1
GROUP BY ord_number) a
GROUP BY 1;
This should get you what you want.
sqlfiddle demo
Note that i used ` around check since this is a mysql reserved word

Categories