How to rearrange the following data using php? - php

i retrieved the following data using sql query from mysql
TOTAL COMPUTER DATE GROUP
-----------------------------------------------
48 LAPTOP2 2009-08-19 1
77 LAPTOP2 2009-08-20 1
0 LAPTOP2 2009-08-21 1
15 LAPTOP1 2009-08-19 1
25 MAIN 2009-08-23 1
25 LAP3 2009-08-18 2
3 LAP3 2009-08-19 2
55 LAP3 2009-08-20 2
i would like to rearrange the data like using php
group computer 2009-08-18 2009-08-19 2009-08-20 2009-08-21 2009-08-22 2009-08-23
------------------------------------------------------------------------------------------------
1 LAPTOP2 0 48 77 0 0 0
1 LAPTOP1 0 15 0 0 0 0
1 MAIN 25
2 LAP3 25 3 55 0 0 0

Use the following query to pivot the data:
SELECT t.group,
t.computer,
MAX(CASE WHEN t.date = '2009-08-18' THEN t.total ELSE 0 END) AS '2009-08-18',
MAX(CASE WHEN t.date = '2009-08-19' THEN t.total ELSE 0 END) AS '2009-08-19',
MAX(CASE WHEN t.date = '2009-08-20' THEN t.total ELSE 0 END) AS '2009-08-20'
--, etc...
FROM TABLE t
GROUP BY t.group, t.computer
Your options are either to define each column for the data you are pivoting, or you can use MySQL's Prepared Statement syntax to dynamically create those columns.
I feel the need to point out that your example is inconsistent - for LAPTOP2, you have zero as the value for 2009-08-18, but the main value for that is blank. Neither have a record for that date. If you want these to show as blank/etc, change ELSE 0 END to ELSE NULL END in the CASE statements.

Related

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

Merging rows in SQL

My table looks like this:
Dept Wk1 Wk2
100 25 50
200 25 50
300 25 50
400 25 50
500 25 50
My results need to look like this:
Dept Wk1 Wk2
100 25 50
200 25 50
300 25 50
400 50 100
Things worth noting; the database is Pervasive PSQL10 and I'm using PHP to handle the results. I thought about doing this merge in PHP, but ideally if it could be done in the query that would save me the need for the extra code.
Reason for combining rows is that 2 depts will be treated as one when the data is pulled.
I think I'm overthinking it, but I've explored union, group by, and stuff (which I'm probably using incorrectly) and nothing is giving me what I need.
Any help or direction would be greatly appreciated, because what I've read so far addresses rows that have the same values, but not different.
Here is what my query actually looks like:
SELECT
dept_workcenter,
SUM(case when right(date_sequence,2) between 0 and 7 then hours_worked else 0 end) as wk1,
SUM(case when right(date_sequence,2) between 8 and 14 then hours_worked else 0 end) as wk2,
SUM(case when right(date_sequence,2) between 15 and 21 then hours_worked else 0 end) as wk3,
SUM(case when right(date_sequence,2) between 22 and 28 then hours_worked else 0 end) as wk4,
SUM(case when right(date_sequence,2) between 29 and 31 then hours_worked else 0 end) as wk5
FROM job_detail
WHERE job between '017079' AND '017207'
AND date_sequence BETWEEN '141200' and '141231'
AND dept_workcenter != ''
GROUP BY dept_workcenter
HAVING SUM(hours_worked) <> '0'
AND SUM(hours_worked) > '0.05';
You can use a case statement in the group by:
select (case when dept in (400, 500) then 400 else dept end) as dept,
sum(wk1) as wk1, sum(wk2) as wk2
from table t
group by (case when dept in (400, 500) then 400 else dept end)
order by dept;
So basically take the 500 and merge it with the 400?
SELECT Dept, SUM(Wk1) AS Wk1, SUM(Wk2) AS Wk2
FROM yourtable
GROUP BY Dept IN (400,500), Dept
This is not an uncommon problem in reporting. The typical method to handle this is to generate a temporary table in which you can modify the 500 to be 400, and then do a GROUP BY and SUM. This method will become more useful the more complicated your reporting becomes over time.
create table #DeptReptTable from select * from department;
update #DeptReptTable set Dept = 400 where Dept = 500;
select Dept, SUM(Wk1) Wk1, SUM(Wk2) Wk2 from #DeptReptTable group by Dept
drop table #DeptReptTable
Note: The # in front of the temp table is a Sybase-ism, which will create a table tied to the current SQL connection / session. I don't know if that magic works for Pervasive.

Retrieve sum of two different table column field in mysql using php

Hey guys I m stuck over this query
I have two different table with same column name but different values with primary key id
Table 1 : q1
id ability_to_delegate communication confidence commitment
1 0 0 1 0
2 0 0 0 0
3 0 0 0 0
4 1 0 1 0
Table 2 : q2
id ability_to_delegate communication confidence commitment
1 0 0 2 1
2 0 0 1 1
3 0 0 0 0
4 0 0 1 1
Now what I want is to sum the values of two different tables with same field name but different IDs.
For example I want values of confidence field from table q1 with id = 4 i.e 1 and values of confidence field from table q2 with id = 1 i.e 2 to be added i.e 3.
I tried using union but not getting the rseult
$mresult=mysqli_query($con,"select sum(sm) from
(select confidence sm from q1 where id='$id'
union
select confidence sm from q2 where id='$id') ss");
while ($row1 = mysqli_fetch_assoc($mresult)){
echo "Sum ". $row1['ss'];
}
I m getting warning
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in .... on line 89
Please help me out
The query to accomplish what you're looking for is
SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence`
FROM `q1`, `q2`
WHERE `q1`.`id` = 4
AND `q2`.`id` = 1
You can plug this into your PHP and substitute the variables where appropriate.
$mresult=mysqli_query($con,"SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence` FROM `q1`, `q2`WHERE `q1`.`id` = '{$q1id}' AND `q2`.`id` = '{$q2id}'");
while ($row1 = mysqli_fetch_assoc($mresult)){
echo "Sum ". $row1['TotalConfidence'];
}

Select max win series: sql query

MySQL
I have table, where i store user_matches and it result:
n_match id_user id_score
1 55 1
1 66 0
This mean, 'user with id=55 win match with id=1 to user with id=66'.
So, we have 10, 100, 1000 matches, where user win or lose to opponents:
n_match id_user id_score
1 55 1 (win)
1 66 0
2 55 0 (lose)
2 77 1
3 55 1 (win)
3 77 0
4 55 1 (win)
4 77 0
5 55 1 (win)
5 77 0
Ok. As u can see, user win 3 matches without losing (win series)- and that's what i need from my query.
Question: How could i get from this table the longest series of won matches? Is it possible without looping on sql side or server side- just from query?
Thx.
Edit: One of solution i just now understand,- to get all matches as string like 001010101111010101011, then split it into array of strings with separator '0' -> [1, 1, 1, 1111, ...] and just take the longest string length.
But in this case i have to write server side code =\ That's not good, but mb the fastest.
The best way to do this is to calculate the cumulative number of losses for any match. For a sequence of wins, this value is constant. You can then use group by to get the length of the longest such sequence.
This version of the query is database-neutral. It uses subqueries to get the counts:
select user_id, max(NumWinsInRow)
from (select user_id, cumlosses, count(*)-1 as NumWinsInRow
from (select m.*,
(select sum(case when id_score = 0 then 1 else 0 end) from user_matches m2 where m2.id_user = m.id_user and m2.n_match <= m.n_match
) as CumLosses
from user_matches m
) t
group by cumlosses, user_id
) t
group by user_id
This query should run faster if you have an index on user_matches(id_user, n_math, id_score).

Finding the min in a column where two other columns are zero

I am trying to write a MySQL search that will find the lowest imageID where the other two columns = 0. In this case the imageID returned would be 8.
ImageId Processing Finished
5 0 1
6 1 0
7 0 1
8 0 0
9 1 0
10 0 1
11 1 0
12 0 0
13 0 0
14 0 0
15 0 0
find the lowest imageID where the other two columns = 0
Just convert your sentence in the query:
(find) (the lowest imageID) (where the other two columns = 0)
[SELECT] [MIN(imageID)] [WHERE Processing = 0 AND Finished = 0]
So your full query should be (using MIN() aggregate function):
SELECT MIN(ImageId) as LowestImageId
FROM Mytable
WHERE Processing = 0
AND Finished = 0
See this SQLFiddle demo
select min(ImageId) from tablename where processing=0 and finished=0;
This is fairly basic SQL, and can easily be found if you do some research of your own.
SELECT MIN(ImageId) FROM your_table WHERE Processing = 0 AND Finished = 0

Categories