How to group rows into output columns, by date - mysql

I have data in a MariaDB table similar to the following. It is basically weather data at two locations, and I want to feed the data to a stats program. I want to output the data in a way that groups the rows by datetime, but puts the grouped row values into columns.
obsv_location obsv_value obsv_datetime
------------- ---------- -------------
airport1 35.0 2020-01-01 12:00
airport2 35.2 2020-01-01 12:00
airport1 36.5 2020-01-01 13:00
airport2 36.4 2020-01-01 13:00
Is it possible to create a query that outputs something like the following?
obsv_datetime airport1 airport2
------------- -------- -------------
2020-01-01 12:00 35.0 35.2
2020-01-01 13:00 36.5 36.4

One method uses join; another conditional aggregation. The second:
select obsv_datetime,
max(case when obsv_location = 'airport1' then obsv_value end) as airport1,
max(case when obsv_location = 'airport2' then obsv_value end) as airport2
from t
group by obsv_datetime;

Related

Calculate Employee Experience from Multiple Jobs

I have a table named employee experience with id, userId, startDate, endDate columns.
I want to calculate employee experience. Can someone please help with mysql query or JPA specification code?
For example in case of following data:
id
userID
startDate
endDate
1
1
2021-01-01
2022-01-01
2
2
2019-01-01
2020-01-01
3
2
2020-01-02
2021-01-01
4
3
2021-01-01
2022-01-01
the output should be:
userID
experience
1
1
2
2
3
1
Successfully did this with the following:
SELECT SUM(TIMESTAMPDIFF(YEAR, START_DATE, END_DATE)) AS experience,
SOCIAL_PROFILE_ID
FROM tableName
GROUP BY SOCIAL_PROFILE_ID

how can i summarize some data in a MySQL table based on subsets of data determined by a datetime field?

Lets say i have this table:
id name date qty
1 bananas 2020-04-01 00:00:00 20
2 apples 2020-04-02 00:00:00 15
3 apples 2020-04-05 00:00:00 15
4 bananas 2020-04-06 00:00:00 10
5 bananas 2020-04-15 00:00:00 40
6 bananas 2020-04-16 00:00:00 20
7 apples 2020-04-17 00:00:00 15
8 apples 2020-04-17 00:00:00 5
What i want to achieve is a result set with total quantity of each fruit before 2020-04-06 in a InitialStock field and total quantity of each fruit after 2020-04-17 as FinalStock
It looks like you want conditional aggregation:
select
name,
sum(case when date <= '2020-04-06' then qty end) initial_stock,
sum(case when date >= '2020-04-17' then qty end) final_stock
from mytable
group by name
For each name, this gives you the sum of qty before April 4th and after Apri 17th, in two separated columns.

MYSQL return zero in date not present, and COUNT how many rows are present with a specific date

I have a table named calendar with a single column (mydate DATE).
MYDATE
2020-01-01
2020-01-02
2020-01-03
...
I have also a table named delivery with three columns (id PK, giorno DATE, totale FLOAT)
ID GIORNO TOTALE
1 2020-01-01 0.10
2 2020-01-01 5
3 2020-01-02 12
4 2020-01-12 5
5 2020-02-02 13.50
This is what I'm trying to obtain:
Day Numbers of orders
2020-01-01 2
2020-01-02 1
2020-01-03 0
2020-01-04 0
2020-01-05 0
2020-01-06 0
2020-01-07 0
2020-01-08 0
2020-01-09 0
2020-01-10 0
2020-01-11 0
2020-01-12 1
...
I was trying this query:
SELECT c.mydate, IFNULL(d.totale, 0) value
FROM delivery d
RIGHT JOIN calendar c
ON ( c.mydate = d.giorno )
GROUP BY c.mydate
ORDER BY c.mydate
Consider:
select c.mydate, count(d.id) number_of_orders
from calendar c
left join delivery d on d.giorno = c.mydate
group by c.mydate
This works by left-joining the calendar table with the orders table, then aggregating by date, and finally counting the number of matching rows in the order table.
This is quite close to your original query (although this uses left join instead of right join), however this uses an aggregate function to count the orders.

Hive query to get min and max of date column group by year and id

I have table with two columns ,id and date.I want to get the max and min dates for that particular id as per that year. Below is the sample data and the result I wanted
Sample Data
id date_col
123 2015-05-01 04:00:00
123 2017-04-01 04:00:00
123 2017-09-01 04:00:00
123 2014-09-01 04:00:00
123 2012-12-01 05:00:00
123 2016-08-01 04:00:00
123 2014-05-01 04:00:00
123 2016-10-01 04:00:00
Results I am expecting
123 2014-05-01 2014-09-01
123 2015-05-01
123 2016-08-01 2016-10-01
123 2017-04-01 2017-09-01
I tried by using below multiple query but i won't be sorting according to the year
SELECT id,MAX(date_col) AS maxdate_col,MIN(date_col) AS mindate_col FROM table GROUP BY id
SELECT id,MAX(date_col) AS max_votes,MIN(date_col) AS mindate_col,YEAR(date_col) FROM test GROUP BY id,YEAR(date_col)
Try this
SELECT id
,MIN(date_col) AS mindate_col
,(CASE WHEN MAX(date_col) <> MIN(date_col) THEN MAX(date_col) END) AS maxdate_col
,YEAR(date_col) AS year_col
FROM test
GROUP BY id,YEAR(date_col)
ORDER BY id,year_col
If you want only the date part, you can cast it CAST(date_col AS DATE)

Show Null Value from 2 comparable records in MySQL

I have example data table from attendance machine system like this:
ID Name In
1 John 2015-04-17 08:00:00
1 John 2015-04-17 16:30:00
1 John 2015-04-20 10:01:00
1 John 2015-04-21 10:00:00
1 John 2015-04-21 19:00:00
Here my query:
SELECT a.id AS ID, a.nama AS `Name`, DATE_FORMAT(a.att, '%d-%b-%y') AS `Date`,
DATE_FORMAT(a.att, '%T') as `IN`, DATE_FORMAT(b.att, '%T') AS `OUT`
FROM attendance a
INNER JOIN attendance b
on date(a.att) = date(b.att) AND a.id = b.id
WHERE b.att > a.att
With above query I can split the In (column) time into 2 column become In and out like this:
Id Name Date In Out
1 John 17-Apr-15 08:00:00 16:30:00
1 John 21-Apr-15 10:00:00 19:00:00
The problem is if user John forget to use finger print where he out, for example on date 20 April, the data is not display. So, I want to my output become like this:
Id Name Date In Out
1 John 17-Apr-15 08:00:00 16:30:00
1 John 20-Apr-15 10:01:00 NULL
1 John 21-Apr-15 10:00:00 19:00:00
You should use a LEFT JOIN instead of an INNER JOIN. Or you can use this query instead:
SELECT
Id,
Name,
DATE(att) AS `Date`,
DATE_FORMAT(MIN(att), '%T') AS `In`,
CASE WHEN MAX(att)<>MIN(att) THEN DATE_FORMAT(MAX(att), '%T') END AS `Out`
FROM
attendance
GROUP BY
Id, Name, DATE(att)
Please see a fiddle here.