Mapping a column value to which dates it has rows of - mysql

I have a table of sign-ins for people who are logging time on different projects and I need to compile a report of which days each project was worked on.
My table looks something like this:
id | project | time_in
----------------------------------
1 | 1 | 2014-12-07 05:00:00
2 | 2 | 2014-12-08 06:00:00
3 | 1 | 2014-12-05 14:00:00
4 | 3 | 2014-12-07 08:30:00
5 | 2 | 2014-12-07 12:00:00
6 | 1 | 2014-12-08 05:00:00
7 | 2 | 2014-12-05 06:00:00
8 | 1 | 2014-12-06 14:00:00
9 | 3 | 2014-12-08 08:30:00
10 | 2 | 2014-12-06 12:00:00
time_in is of type TIMESTAMP.
What I need to figure out is, given a date range (e.g. December 5 - 8), which days of the week each project was worked on. I'm totally flexible on the query, i.e. I can generate the query using a loop, and I'm also flexible on how the result set looks, so long as I can parse it to get the information I need. For example, maybe:
project | days
-----------------------------------------------------
1 | 2014-12-07,2014-12-08,2014-12-05,2014-12-06
2 | 2014-12-08,2014-12-07,2014-12-05,2014-12-06
3 | 2014-12-07,2014-12-08
Or better yet:
project | d0 | d1 | d2 | d3
---------------------------
1 | 1 | 1 | 1 | 1
2 | 1 | 1 | 1 | 1
3 | 0 | 0 | 1 | 1
I honestly have no idea where to even begin on a query like this, if it's even possible.

You could use group_concat to achieve your initial results:
select project, group_concat(date(time_in) order by time_in)
from yourtable
group by project
SQL Fiddle Demo
If you want additional columns, you could use max with case:
select project,
max(case when date(time_in) = '2014-12-05' then 1 else 0 end) d0,
max(case when date(time_in) = '2014-12-06' then 1 else 0 end) d1,
max(case when date(time_in) = '2014-12-07' then 1 else 0 end) d2,
max(case when date(time_in) = '2014-12-08' then 1 else 0 end) d3
from yourtable
group by project
More Fiddle

Related

subtract 2 DB rows from each other for each unique partner_ID? MYSQL

I have a MYSQL table where I need to get to subtract values from 2 different rows.
This is my DB table:
Tablename: ext_partnertotals
| Partner_ID | Partnername | Month | Year | Total |
|------------|-------------|-------|------|-------|
| 1 | Partner 1 | 1 | 2018 | 10 |
| 1 | Partner 1 | 2 | 2018 | 12 |
| 2 | Partner 2 | 1 | 2018 | 18 |
| 2 | Partner 2 | 2 | 2018 | 12 |
It should get this with a query:
| Partner_ID | Partnername | up/down |
|------------|-------------|---------|
| 1 | Partner 1 | +2 |
| 2 | Partner 2 | -6 |
I need to get the Subtract value of 2 different months for each Partner.
Every Partner has a tablerow for each month and a value for that month.
Now I need to get If they went up or went down in value since the month before.
Can someone write me a query?
Since you're unable to improve your terrible schema, I recommend you use a (very ugly/hard to maintain and very slow) correlated subquery:
SELECT Partner_ID, Partnername, Year, Month, Total - (
SELECT Total
FROM ext_partnertotals AS prev
WHERE prev.Partner_ID = cur.Partner_ID AND CASE cur.Month
WHEN 1 THEN prev.Year = cur.Year - 1 AND prev.Month = 12
ELSE prev.Year = cur.Year AND prev.Month = cur.Month - 1
END
) AS `up/down` FROM ext_partnertotals AS cur
See it on sqlfiddle.

Creating a crosstab query in MySQL

I have a MySQL table that tracks certain totals by both hour of the day and various locations. I am trying to create a query that will total not only each column, but also each row. The query I have so far totals each column, but I can't figure out how to get a total for each row as well.
This is my query:
SELECT * FROM
(SELECT IFNULL(hour,"Total") as hour, SUM(location1), SUM(location2), SUM(location3), SUM(location4), SUM(location), FROM counts WHERE ay = 'AY1617' GROUP BY hour WITH ROLLUP) as crossdata
ORDER BY FIELD (hour,'8:00am','9:00am','10:00am','11:00am','12:00pm','1:00pm','2:00pm','3:00pm','4:00pm','5:00pm','6:00pm','7:00pm','8:00pm','9:00pm','10:00pm','11:00pm')
This is ultimately what I want the output to look like:
hour location1 location2 location3 location4 totals
8am 4 3 2 1 10
9am 1 2 2 1 6
10am 2 3 2 3 10
totals 7 8 6 5 26
How can I achieve this?
For what it's worth, this is not a crosstab query. You aren't pivoting rows to columns.
I tried this query and got the result you want:
SELECT IFNULL(hour, 'Total') AS hour,
SUM(location1) AS location1,
SUM(location2) AS location2,
SUM(location3) AS location3,
SUM(location4) AS location4,
SUM(location1)+SUM(location2)+SUM(location3)+SUM(location4) AS totals
FROM counts
WHERE ay = 'AY1617'
GROUP BY hour WITH ROLLUP;
You should really use the TIME data type instead of strings for the hour. Then it just sorts correctly.
+----------+-----------+-----------+-----------+-----------+--------+
| hourt | location1 | location2 | location3 | location4 | totals |
+----------+-----------+-----------+-----------+-----------+--------+
| 08:00:00 | 4 | 3 | 2 | 1 | 10 |
| 09:00:00 | 1 | 2 | 2 | 1 | 6 |
| 10:00:00 | 2 | 3 | 2 | 3 | 10 |
| Total | 7 | 8 | 6 | 5 | 26 |
+----------+-----------+-----------+-----------+-----------+--------+

count and sum different values per each day

how count different values per each day ? and the total value for each day? in one request.
key1 | key2 | tdate | tview
1 | 20161123454647 | 2016-11-23 11:11:11 | view1
2 | 20161123454648 | 2016-11-23 11:11:11 | view2
3 | 20161122454649 |2016-11-22 11:11:11 | view2
4 | 20161122454650 |2016-11-22 11:11:11 | view1
5 | 20161122454653 |2016-11-22 11:11:11 | view2
6 | 20161122454661 |2016-11-22 11:11:11 | view2
7 | 20161121454622 |2016-11-21 11:11:11 | view3
8 | 20161121454679 |2016-11-21 11:11:11 | view1
9 | 20161121454684 |2016-11-21 11:11:11 | view3
I found to count the total of all values of tview per day :
SELECT DATE(tdate) Date, COUNT(DISTINCT tview) totalOfViews FROM mytable GROUP BY DATE(tdate)
I have a key (key2) which the concatenation of date and the number of a render because I don't want to hive two same render in the same day.
It's most easy for me when I insert a new render with 'INSERT ON DUPLICATE key2 UPDATE'. I update just the number of view with one request. I don't know 'INSET ON DUPLICATE' <2 differents keys> UPDATE :newview. Interesting question too ;-)
The date is a timestamp in my table.
I use php 7, MySQL and PDO to do statement.
One of interesting output:
day | totaView1 | totalView2 | totalView3 | totalView1+view2 |totalOfViews
2016-11-23 | 1 | 1 | 0 | 2 | 2
2016-11-22 | 1 | 3 | 0 | 4 | 4
2016-11-21 | 1 | 0 | 2 | 1 | 3
After i found to range date of request and compare évolution of number view per day. Example:
Day (currentmonth) | totaView1 | totalView1 (lastmonth) |totalOfViews
Is the "alter table" can do this result?
One possibility is to use conditional aggregation:
SELECT DATE(tdate) AS day,
SUM(CASE WHEN tview = 'view1' THEN 1 ELSE 0 END) AS totaView1,
SUM(CASE WHEN tview = 'view2' THEN 1 ELSE 0 END) AS totaView2,
SUM(CASE WHEN tview = 'view3' THEN 1 ELSE 0 END) AS totaView3,
SUM(CASE WHEN tview = 'view1' OR tview = 'view2'
THEN 1 ELSE 0 END) AS totaView1Or2,
COUNT(*) AS totalOfViews
FROM mytable
GROUP BY DATE(tdate)

MySql query group by day and by time

I'm trying create an SQL query to resolve my problem.
My Table:
+----+---------------------+-------+
| id | date | value |
+----+---------------------+-------+
| 1 | 2014-10-10 05:10:10 | 10 |
+----+---------------------+-------+
| 2 | 2014-10-10 09:10:10 | 20 |
+----+---------------------+-------+
| 3 | 2014-10-10 15:10:10 | 30 |
+----+---------------------+-------+
| 4 | 2014-10-10 23:10:10 | 40 |
+----+---------------------+-------+
| 5 | 2014-10-11 08:10:10 | 15 |
+----+---------------------+-------+
| 6 | 2014-10-11 09:10:10 | 25 |
+----+---------------------+-------+
| 7 | 2014-10-11 10:10:10 | 30 |
+----+---------------------+-------+
| 8 | 2014-10-11 23:10:10 | 40 |
+----+---------------------+-------+
I want to sum value in groups by days and this days in three sub groups like a 'morning'(06:00 - 12:00), 'afternoon'(12:00 - 18:00) and 'night'(00:00 - 06:00 and 18:00 - 24:00).
something like this:
+------------+-------+---------+-----------+-------+
| date | value | morning | afternoon | night |
+------------+-------+---------+-----------+-------+
| 2014-10-10 | 100 | 20 | 30 | 50 |
+------------+-------+---------+-----------+-------+
| 2014-10-11 | 110 | 70 | 0 | 40 |
+------------+-------+---------+-----------+-------+
You could use a couple of sums over case expressions:
SELECT DAY(`date`) AS `date`
SUM(CASE WHEN HOUR(`date`) BETWEEN 6 AND 12 THEN value ELSE 0 END) AS `morning`,
SUM(CASE WHEN HOUR(`date`) BETWEEN 12 AND 18 THEN value ELSE 0 END) AS `afternoon`,
SUM(CASE WHEN HOUR(`date`) < 6 OR HOUR(`date`) > 18 THEN value ELSE 0 END) AS `evening`
FROM my_table
GROUP BY DAY(`date`)
There are multiple ways to go about this, but for myself I'd do it by first extracting the pseudo information in a CROSS APPLY, and then grouping on this information.
I believe this offers significant readibility benefits, and allows you to re-use any calculations in other clauses. For example, you have centralised the grouping mechanism, meaning that you only need to change it in the one place rather than in the select and the group by. Similarly, you could add "extraData.Morning = 1" to a WHERE clause rather than re-writing the calculation for mornings.
For example:
CREATE TABLE #TestData (ID INT, Data DATETIME, Value INT)
INSERT INTO #TestData (ID, Data, Value) VALUES
(1 ,'2014-10-10 05:10:10' ,10)
,(2 ,'2014-10-10 09:10:10' ,20)
,(3 ,'2014-10-10 15:10:10' ,30)
,(4 ,'2014-10-10 23:10:10' ,40)
,(5 ,'2014-10-11 08:10:10' ,15)
,(6 ,'2014-10-11 09:10:10' ,25)
,(7 ,'2014-10-11 10:10:10' ,30)
,(8 ,'2014-10-11 23:10:10' ,40)
SELECT
extraData.DayComponent
,SUM(td.Value)
,SUM(CASE WHEN extraData.Morning = 1 THEN td.Value ELSE 0 END) AS Morning
,SUM(CASE WHEN extraData.Afternoon = 1 THEN td.Value ELSE 0 END) AS Afternoon
,SUM(CASE WHEN extraData.Night = 1 THEN td.Value ELSE 0 END) AS Night
FROM #TestData td
CROSS APPLY (
SELECT
DATEADD(dd, 0, DATEDIFF(dd, 0, td.Data)) AS DayComponent
,CASE WHEN DATEPART(HOUR, td.Data) BETWEEN 6 AND 12 THEN 1 ELSE 0 END AS Morning
,CASE WHEN DATEPART(HOUR, td.Data) BETWEEN 12 AND 18 THEN 1 ELSE 0 END AS Afternoon
,CASE WHEN DATEPART(HOUR, td.Data) BETWEEN 0 AND 6
OR DATEPART(HOUR, td.Data) BETWEEN 18 AND 24 THEN 1 ELSE 0 END AS Night
) extraData
GROUP BY
extraData.DayComponent
DROP TABLE #TestData

MySQL Dynamic inner join for combining multiple rows into one?

I am creating a database with the tables below, where shop_id in hours refers to a an id in shop.
Preferably I would have a query to return all data in one row, in stead of needing to post-process a lot of rows to "merge" the result from hours so the end result looks like this.
+-------------------+---------+------+---------+----------------+----------+---------------+---------------+-----+-----+-----+-----+-----+-----+-----+
| name | address | zip | city | municipal | phone | lat | lng | day | day | day | day | day | day | day |
+-------------------+---------+------+---------+----------------+----------+---------------+---------------+-----+-----+-----+-----+-----+-----+-----+
| Coop Marked Budal | false | 7298 | Budalen | Midtre gauldal | 72436410 | 62.8837013245 | 10.4836997986 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+-------------------+---------+------+---------+----------------+----------+---------------+---------------+-----+-----+-----+-----+-----+-----+-----+
Right now I have come to the query below, wich I feel there must be a better alternative to. So my question is: Is there really another solution to this?
Also, I've seen concat, but I want to avoid having to split strings later on when processing the data.
select shop.name, shop.address, shop.zip, shop.city, shop.municipal, shop.phone, shop.lat, shop.lng,
h.day, hh.day, hhh.day, hhhh.day, hhhhh.day, hhhhhh.day, hhhhhhh.day
from shop
/**
As it requires a unique table name, this was the solution I found.
Could this be shortened?
**/
inner join hours h on shop.id = h.shop_id and h.day = 1
inner join hours hh on shop.id = hh.shop_id and hh.day = 2
inner join hours hhh on shop.id = hhh.shop_id and hhh.day = 3
inner join hours hhhh on shop.id = hhhh.shop_id and hhhh.day = 4
inner join hours hhhhh on shop.id = hhhhh.shop_id and hhhhh.day = 5
inner join hours hhhhhh on shop.id = hhhhhh.shop_id and hhhhhh.day = 6
inner join hours hhhhhhh on shop.id = hhhhhhh.shop_id and hhhhhhh.day = 7;
Tables
shop
+----+-------------------+---------+------+---------+----------------+----------+---------------+---------------+----------+-----------+
| id | name | address | zip | city | municipal | phone | lat | lng | chain_id | county_id |
+----+-------------------+---------+------+---------+----------------+----------+---------------+---------------+----------+-----------+
| 1 | Test | false | 1234 | Test | Test | 12341234| 0.0000 | 0.0000 | 3 | 16 |
+----+-------------------+---------+------+---------+----------------+----------+---------------+---------------+----------+-----------+
hours
+-----+----------+----------+---------+
| day | open | close | shop_id |
+-----+----------+----------+---------+
| 1 | 09:00:00 | 18:00:00 | 1 |
| 2 | 09:00:00 | 18:00:00 | 1 |
| 3 | 09:00:00 | 18:00:00 | 1 |
| 4 | 09:00:00 | 18:00:00 | 1 |
| 5 | 09:00:00 | 18:00:00 | 1 |
| 6 | 09:00:00 | 20:00:00 | 1 |
| 7 | 14:00:00 | 20:00:00 | 1 |
+-----+----------+----------+---------+
You can also use a case .. when to do the pivot, and then group by the shop fields and use an aggregate function to process the day.
select
shop.NAME, shop.address, shop.zip, shop.city, shop.municipal, shop.phone, shop.lat, shop.lng,
MAX(CASE WHEN h.DAY = 1 THEN h.DAY ELSE 0 END) AS Day1,
MAX(CASE WHEN h.DAY = 2 THEN h.DAY ELSE 0 END) AS Day2,
MAX(CASE WHEN h.DAY = 3 THEN h.DAY ELSE 0 END) AS Day3,
MAX(CASE WHEN h.DAY = 4 THEN h.DAY ELSE 0 END) AS Day4,
MAX(CASE WHEN h.DAY = 5 THEN h.DAY ELSE 0 END) AS Day5,
MAX(CASE WHEN h.DAY = 6 THEN h.DAY ELSE 0 END) AS Day6,
MAX(CASE WHEN h.DAY = 7 THEN h.DAY ELSE 0 END) AS Day7
from shop
INNER JOIN HOURS h ON shop.id = h.shop_id
group by
shop.NAME, shop.address, shop.zip, shop.city, shop.municipal, shop.phone, shop.lat, shop.lng;
Just a note about what you want displayed in the day columns:
AFAIK if any of the hours rows for a shop : day is missing, your current query will drop the whole row? If you want this behaviour repeated, you will need to also add in a where clause.