mysql query sum values group by month but divided in years - mysql

I have a table with 4 columns: ID, fieldDATE, fieldINT1, fieldINT2.
Table is like this:
ID| fieldDate | FieldINT1 | FiledINT2 |
===================================
1 | 2016-01-01 | 100 | 1 |
2 | 2016-01-08 | 200 | 1 |
3 | 2016-02-01 | 150 | 1 |
4 | 2016-02-05 | 400 | 2 |
5 | 2017-01-01 | 120 | 1 |
6 | 2017-01-21 | 123 | 1 |
7 | 2017-02-03 | 30 | 1 |
8 | 2018-01-01 | 123 | 1 |
9 | 2018-01-03 | 30 | 1 |
I'd like to create a table with 12 rows, with the first column is the month name, and the other columns are sum of fieldINT1 and fieldINT2, group by month in a specific YEAR. So in my example, there will be 4 columns ( MONTH NAME, 2016, 2017, 2018)
How can I do it?

Related

MySQL: How to select (count renewal bookings & count of all bookings) for each room?

+----+---------+---------+
| id | room_id | user_id |
+----+---------+---------+
| 1 | 5 | 10 |
| 2 | 5 | 20 |
| 3 | 5 | 20 |
| 4 | 6 | 30 |
| 5 | 6 | 40 |
| 6 | 6 | 50 |
| 7 | 7 | 10 |
| 8 | 7 | 10 |
| 9 | 8 | 20 |
| 10 | 9 | 10 |
| 11 | 9 | 10 |
| 12 | 9 | 10 |
+----+---------+---------+
I want to select top rooms with count bookings and with count of renewal bookings for each room
To get this result
+---------+----------------+---------------+
| room_id | total_bookings | total_renewal |
+---------+----------------+---------------+
| 5 | 3 | 1 |
| 6 | 3 | 0 |
| 7 | 2 | 1 |
| 8 | 1 | 0 |
| 9 | 3 | 2 |
| 5 | 1 | 0 |
+---------+----------------+---------------+
And this live mysql here
https://paiza.io/projects/Ao2C6d6pgE133QXQAl03Ug?language=mysql
........
Considering your requirement for renewal is not clear, something like this could do the job, using pure sql:
select
s.room_id,
sum(s.total_bookings) as total_bookings,
sum(case when s.total_bookings > 1 then s.total_bookings - 1 else 0 end) as total_renewal
from (
select room_id, user_id, count(*) as total_bookings
from booking
group by room_id, user_id
) s
group by s.room_id;

mysql - return zero if no results for a given period

I have this table:
+----+--------+------------+-----------+------------------+
| id | amount | period | player_id | payment_category |
+----+--------+------------+-----------+------------------+
| 3 | 100 | 2017-06-01 | 1 | 1 |
| 10 | 100 | 2017-06-01 | 2 | 1 |
| 6 | 100 | 2017-08-01 | 2 | 1 |
| 5 | 100 | 2017-10-01 | 3 | 1 |
| 2 | 100 | 2017-11-01 | 2 | 1 |
| 4 | 100 | 2017-11-01 | 1 | 1 |
| 1 | 100 | 2017-12-01 | 1 | 1 |
| 7 | 100 | 2017-12-01 | 1 | 1 |
| 9 | 100 | 2017-12-01 | 2 | 1 |
| 11 | 200 | 2017-12-01 | 1 | 3 |
+----+--------+------------+-----------+------------------+
Current result:
2017-06-01 2
2017-08-01 1
2017-10-01 1
2017-11-01 2
2017-12-01 3
Desired result:
2017-06-01 2
2017-07-01 0
2017-08-01 1
2017-09-01 0
2017-10-01 1
2017-11-01 2
2017-12-01 3
How can I do that? I tried with coalesce and ifnull. No luck.
I started with DQL:
$query = $em->createQuery('
SELECT COUNT(p.id) as total, p.period
FROM AppBundle:Payment p
WHERE p.period >= :fromTime
AND p.period <= :toTime
AND p.paymentCategory = 1
GROUP BY p.period
')
Of course this does not return the results for missing months ;) I'm looking for a method (doctrine or native mysql) which will somehow fill in the missing months. They can't be hardcoded - I need the results from last x months. Is it even possible or I should do it in PHP?

MySQL select rows by max(column) by another column in SQL while using GROUP BY WEEK?

For each week I want to select the rows which have the highest weight for each distinct value of reps
-------------------------------------
| id | reps | weight | date |
| 1 | 1 | 15 | 2015-06-10 |
| 2 | 2 | 29 | 2015-06-12 |
| 3 | 1 | 30 | 2015-06-13 |
| 4 | 4 | 11 | 2015-06-14 |
| 5 | 1 | 15 | 2015-06-29 |
| 6 | 1 | 9 | 2015-06-30 |
and I would like it to return
-------------------------------------
| id | reps | weight | date |
| 2 | 2 | 29 | 2015-06-12 |
| 3 | 1 | 30 | 2015-06-13 |
| 4 | 4 | 11 | 2015-06-14 |
| 5 | 1 | 15 | 2015-06-29 |
I've tried
SELECT MAX(weight) as weight, reps, date, id FROM log_items
GROUP BY reps, WEEK(date)
But it seems to return completely random results, I have also tried using sub queries but they didn't work either (I'm guessing I was doing it wrong)
Try:
SELECT *
FROM log_items
WHERE (reps, WEEK(date), weight ) in (
SELECT reps, WEEK(date) , MAX(weight)
FROM log_items
GROUP BY reps, WEEK(date)
)
demo: http://sqlfiddle.com/#!9/4bf84/9

Group consecutive days in Week in MySql

I want to be able to group a table of sales by week but only by the first x number of days.
I can group by week easily
SELECT SUM( order_total_price ) AS total, WEEK(order_time,1) AS week_number
FROM orders
WHERE YEAR( order_time ) = 2014
GROUP BY WEEK( order_time, 1 )
So I want to get the aggregate sum of orders for each day of the week.
I will need to run this query 7 times for each of the days.
Here is some sample data. I have selected a range of totals from Mon-Sun
+-------------+-------------------+
| order_time | order_total_price |
+-------------+-------------------+
| 2014-03-03 | 20 |
| 2014-03-04 | 25 |
| 2014-03-05 | 30 |
| 2014-03-06 | 15 |
| 2014-03-07 | 20 |
| 2014-03-08 | 15 |
| 2014-03-09 | 30 |
| 2014-03-10 | 20 |
| 2014-03-11 | 15 |
| 2014-03-12 | 10 |
| 2014-03-13 | 25 |
| 2014-03-14 | 30 |
| 2014-03-15 | 25 |
| 2014-03-16 | 10 |
+-------------+-------------------+
Here is the results that I am after
+----------+-------------+-------+
| end_day | week_number | total |
+----------+-------------+-------+
| 1 | 10 | 20 |
| 2 | 10 | 45 |
| 3 | 10 | 75 |
| 4 | 10 | 90 |
| 5 | 10 | 110 |
| 6 | 10 | 125 |
| 7 | 10 | 155 |
| 1 | 11 | 20 |
| 2 | 11 | 35 |
| 3 | 11 | 45 |
| 4 | 11 | 70 |
| 5 | 11 | 100 |
| 6 | 11 | 125 |
| 7 | 11 | 135 |
+----------+-------------+-------+
The end_day(1=Mon - 7=Sun) would be the day which the aggregate of the week total is calculated to. Notice how the totals are the aggregate total to that day of the week.
SELECT
sum(order_total_price) AS total,
WEEK(order_time,1) AS week_number, date_format(order_time,'%w') as day_of_week
FROM orders
WHERE YEAR(order_time) = 2014
AND date_format(order_time,'%w') >= 1
AND date_format(order_time,'%w') <= 2
-- date_format(date,format)
-- %w: Day of the week (0=Sunday, 6=Saturday)
GROUP BY WEEK(order_time,1), date_format(order_time, '%w')

Mysql Show records where timestamp interval is lower then 4 minutes

let's assume i have follow table
| ID | teamid | timestamp |
| 5 | 1 | 2013-07-27 10:19:00 |
| 6 | 2 | 2013-07-27 10:20:00 |
| 7 | 1 | 2013-07-27 10:25:00 |
| 8 | 3 | 2013-07-27 10:26:00 |
| 9 | 1 | 2013-07-27 10:28:00 |
| 10 | 2 | 2013-07-27 10:29:00 |
| 11 | 3 | 2013-07-27 10:30:00 |
| 13 | 3 | 2013-07-27 10:31:00 |
What i need is the records where the interval between the timestamp is lower then 4 minutes and grouped by the team id
so output need looks like
| 7 | 1 | 2013-07-27 10:25:00 |
| 9 | 1 | 2013-07-27 10:28:00 |
| 11 | 3 | 2013-07-27 10:30:00 |
| 13 | 3 | 2013-07-27 10:31:00 |
can someone show me the correct way to solve
tnx
The following sql statement will return your desired list:
SELECT table1.id, table1.teamid, table1.timestamp
FROM exampleTable table1, exampleTable table2
where table1.id != table2.id AND
table1.teamid = table2.teamid AND
ABS(table1.timestamp - table2.timestamp) < 400
ORDER BY teamid, id