I've been reading plenty about crosstab reports (Pivot Tables) in PHP and I've been trying to complete a report but I'm stuck.
I have a DB Table with report_date, employee_name, employee_id, leader_name, leader_id, employee_dept, stat1, stat2, stat3, stat4, stat5, stat6.
what I'm trying to do is to be able to query and return a sum and division of values based on the report_date range selected. So if I choose to query data between 10/01/2014 & 10/25/2014, I need it to sum and divide all the values found within that range.
This is my current SQL query.
Select
report_date,
employee_name,
employee_id,
leader_name,
leader_id,
employee_dept,
stat1,
stat2,
stat3,
stat4,
stat5,
stat6,
SUM(stat2)/SUM(stat1) AS `Result Name2`,
SUM(stat3)/SUM(stat1) AS `Result Name3`,
SUM(stat4)/SUM(stat1) AS `Result Name4`,
SUM(stat5)/SUM(stat1) AS `Result Name5`,
SUM(stat6)/SUM(stat1) AS `Result Name6`
FROM daily_records
GROUP BY report_date, employee_id
Might be too much to ask but how do I use this in PHP to query the totals from a selected report_date range?
Edited to add Sample data:
DB table data
report_date employee_id employee_name employee_dept STAT1 STAT2 STAT3 STAT4 STAT5 STAT6 leader_name leader_id
9/11/2014 1983122 emp_name1 ARK 17 7941 191 5 8137 2 Name 1001
9/11/2014 1983130 emp_name2 ARK 11 5067 516 3 5586 1 Name 1001
9/11/2014 1983138 emp_name3 ARK 3 184 16 4 204 1 Name 1001
9/11/2014 1983138 emp_name4 ARK 12 2576 7 6 2589 2 Name 1002
9/11/2014 1983138 emp_name5 ARK 21 9069 400 139 9608 1 Name 1002
9/11/2014 1983328 emp_name6 ARK 69 17929 1893 1096 20918 1 Name 1002
9/11/2014 1983349 emp_name7 ARK 12 2259 17 112 2388 2 Name 1002
9/11/2014 1983349 emp_name8 ARK 23 8194 880 211 9285 2 Name 1003
9/11/2014 1983829 emp_name9 ARK 81 16175 1431 311 17917 2 Name 1003
9/11/2014 1983888 emp_name10 ARK 7 1442 22 9 1473 1 Name 1003
9/12/2014 1983122 emp_name1 ARK 35 6823 774 22 7619 1 Name 1001
9/12/2014 1983642 emp_name2 ARK 80 18268 1439 135 19842 2 Name 1001
9/12/2014 1983643 emp_name3 ARK 55 20321 962 466 21749 1 Name 1001
9/12/2014 1983677 emp_name4 ARK 72 16379 1157 418 17954 2 Name 1002
9/12/2014 1983682 emp_name5 ARK 17 5017 419 425 5861 1 Name 1002
9/12/2014 1983978 emp_name6 ARK 48 9898 228 94 10220 1 Name 1002
(I tried to paste the table with a proper format but I couldn't)
Stats 2 to 6 should be divided by stat 1 to get a result for each field.
for example: stat2 / stat1 AS Result Name
Let me know if you need anything else.
Thanks in advance
First you should use DATE type for date. After append your query date condition like that;
Select
report_date,
employee_name,
employee_id,
leader_name,
leader_id,
employee_dept,
stat1,
stat2,
stat3,
stat4,
stat5,
stat6,
SUM(stat2)/SUM(stat1) AS `Result Name2`,
SUM(stat3)/SUM(stat1) AS `Result Name3`,
SUM(stat4)/SUM(stat1) AS `Result Name4`,
SUM(stat5)/SUM(stat1) AS `Result Name5`,
SUM(stat6)/SUM(stat1) AS `Result Name6`
FROM daily_records
WHERE report_date >= "2014-10-01"
OR report_date <= "2014-10-25"
GROUP BY employee_id`
Related
This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.
I have following table
slno date productid companyid price
88 2017-05-17 1 1 65.27
87 2017-05-17 1 2 72.94
86 2017-05-17 1 3 73.13
85 2017-05-17 2 1 73.73
84 2017-05-17 2 2 67.71
83 2017-05-16 1 1 65.40
82 2017-05-16 1 2 72.49
81 2017-05-16 2 1 73.31
80 2017-05-16 2 2 67.17
Now I want price of product 1 for 2017-05-17 and difference of price from yesterday for same company id.
e.g:
getPrice( productid = 1, date='2017-05-17')
and this should return :
companyid , productid , date, price, difference from yesterday:
1, 1,'2017-05-17', 65.27, -0.13
2, 1,'2017-05-17', 72.94, 0.45
...
or it should return:
companyid , productid , date, price, yesterday price:
1, 1,'2017-05-17', 65.27, 65.40
2, 1,'2017-05-17', 72.94, 72.49
...
How to get this in PHP SQL?
You should consider doing this with php, because php can tell you yesterdays and today date. So get yesterdays and today date with php and use it to manipulate the sql query
so the query should look something like this
yesterdays Price:
select companyid, productid , date, price from table where productid='1' and date='$yesterday';
Today price
select companyid, productid , date, price from table where productid='1' and date='$today';
now you can get both values and subtract them with php to get the difference
I want to get the yearly max values and the record id information for further joins with other tables.
Consider the following table:
tur_id Datum SZ Art VW StV TV NSP
189 23.06.2010 09:40:00 S 1 -37 -35 46
7 11.05.2012 08:40:00 S 1 -19,9 -21 45
140 02.07.2011 10:30:00 S 1 -25 -26 45
62 31.07.2013 31.07.2013 S 1 -16 -16 42
136 12.07.2011 11:20:00 S 1 -21,4 -23 41
181 04.08.2010 10:00:00 S 1 -30,1 -28 41
195 24.10.2009 09:40:00 S 1 -45 -47 41
90 22.10.2013 22.10.2013 S 1 -14,2 -16 40
11 16.06.2012 10:50:00 S 1 -17 -18 40
153 13.05.2011 09:25:00 S 1 -27,4 -29 40
1 23.07.2014 23.07.2014 S 1 -13,6 -14 39
56 15.06.2013 15.06.2013 S 1 -17,3 -18 39
45 26.10.2012 26.10.2012 S 1 -17,4 -18 39
.....
The following query returns the yearly max values without record id (in my case turid).
SELECT year(datum) rok, max(nsp) FROM turniere GROUP BY year(datum)
Result:
rok max(nsp)
2009 41
2010 46
2011 45
2012 45
2013 42
2014 39
How can I get the info of the turid or the datum value?
You are half-way there. Join the original data back in:
SELECT t.*
FROM turniere t JOIN
(SELECT year(datum) as rok, max(nsp) as maxnsp
FROM turniere
GROUP BY year(datum)
) tt
ON year(t.datum) = tt.rok and t.nsp = tt.maxnsp;
Afternoon,
I have a couple of tables in mysql
The first holds the ticket information and status of ticket
The second holds the items and costs for each item.
Each ticket can have multiple items which are stored into the items table.
example table 1
Ticket id Manufacturer status
102 man-A 10
103 man-A 20
104 man-A 10
105 man-C 10
106 man-B 20
example table 2
Ticket id Item Price
102 item_a 100.00
103 item_a 100.00
103 item_b 110.00
103 item_c 115.00
104 item_c 115.00
105 item_b 110.00
106 item_a 100.00
106 item_c 115.00
now on a quick stats page i need to show.
Manufacturer Qty(status 10) Qty(status 20) Value
man-A 2 1 530.00
man-B 0 1 225.00
man-C 1 0 110.00
If there are no tickets with status 10 or status 20 i do not need to show that manufacturer.
I would like to do 1 mysql request to get the stats i need to show.
Many Thanks
Try this with join and using SUM()
SELECT
t1.Manufacturer,
SUM(t1.status =10) `Qty(status 10)`,
SUM(t1.status =20) `Qty(status 20)`,
SUM(t2.price) `Value`
FROM table1 t1
JOIN table2 t2 ON (t1.`Ticket id` =t1.`Ticket id`)
GROUP BY t1.Manufacturer
Fiddle Demo
I'm losing some hair over this. I found several online threads with a similar issue, but not exactly what I am running into, so I am still a bit jammed.
I am writing a very complex query (at least its complex for me :))
In my ages table, I have an age_start and age_end field.
For example:
age_start | age_end
------------------------
0 - 64
19 - 23
0 - 19
25 - 29
65 - 100
...
And what I am trying to achieve is a query which reads in between these ranges.
The best I have come up with so far is having these two fields set as array values, and in my MySQL query do something like:
Enter Age: [ 33 ]
Get ages data into an array. And then query like:
...WHERE $age >= $age_start AND age <= $age_end...
Would return:
age_start | age_end
------------------------
0 - 64
25 - 29
Does anyone have any ideas or suggestion or a better approach to this?
SELECT *
FROM ages
WHERE 32 BETWEEN age_start AND age_end
SELECT
CASE
WHEN age < 13 THEN 'Under 13'
WHEN age BETWEEN 13 and 15 THEN '13 - 15'
WHEN age BETWEEN 16 and 20 THEN '16 - 20'
WHEN age BETWEEN 21 and 25 THEN '21 - 25'
WHEN age BETWEEN 26 and 30 THEN '26 - 30'
WHEN age BETWEEN 31 and 35 THEN '31 - 35'
WHEN age BETWEEN 36 and 40 THEN '36 - 40'
WHEN age BETWEEN 41 and 45 THEN '41 - 45'
WHEN age BETWEEN 46 and 50 THEN '46 - 50'
WHEN age BETWEEN 51 and 55 THEN '51 - 55'
WHEN age BETWEEN 51 and 55 THEN '56 - 60'
WHEN age BETWEEN 51 and 55 THEN '61 - 65'
WHEN age BETWEEN 51 and 55 THEN '66 - 70'
WHEN age BETWEEN 51 and 55 THEN '71 - 75'
WHEN age BETWEEN 51 and 55 THEN '76 - 80'
WHEN age BETWEEN 51 and 55 THEN '81 - 85'
WHEN age BETWEEN 51 and 55 THEN '86 - 90'
WHEN age BETWEEN 51 and 55 THEN '91 - 95'
WHEN age BETWEEN 51 and 55 THEN '96 - 100'
WHEN age >= 100 THEN 'Over 100'
WHEN age IS NULL THEN 'Not Filled In (NULL)'
END as age_range,
COUNT(ids) AS age_count,group_concat(ids) as chart_ids
FROM (SELECT
TIMESTAMPDIFF(YEAR, `TABLE_FIELD_OF_DATE_OF_BIRTH`, CURDATE()) AS age, user_id as ids
FROM
jw_user
WHERE
is_active=1
AND
is_deleted = 0
) as derived
GROUP BY age_range
ORDER BY age_range
and the would be amazing :)