Alright, I've searched through posts but can't find an answer to the specific query I'm trying to do. I have a very simple table:
mysql> DESCRIBE offpeak_mccavg_raw;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| date | date | YES | | NULL | |
| node | varchar(30) | YES | | NULL | |
| price | decimal(6,4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
Each day, there are about 1700 entries for a given node and a given price (note, in the example below the dates are not consecutive. I've been testing some things so this is why the dates are way off. Normally, there would be entries each day.)
mysql> select * from offpeak_mccavg_raw order by node limit 10;
+------------+---------------+---------+
| date | node | price |
+------------+---------------+---------+
| 2012-01-08 | AEC | -0.4083 |
| 2013-01-18 | AEC | 1.2125 |
| 2013-01-18 | AECI | -1.7575 |
| 2012-01-08 | AECI | -1.2488 |
| 2013-01-18 | AECI.ALTW | -5.0950 |
| 2012-01-08 | AECI.ALTW | -6.3258 |
| 2013-01-18 | AECI.AMMO | -0.4725 |
| 2012-01-08 | AECI.AMMO | -0.8117 |
| 2013-01-18 | AECI.APM_1.AZ | -0.9088 |
| 2012-01-08 | AECI.APM_1.AZ | -0.7729 |
+------------+---------------+---------+
I'm trying to find pairs of nodes that have the same price each day over a given date range. If I only have 1 day's worth of data, I've been able to write a query that finds pairs that match for that given day - but not over a range of days. Ideally, I'd like to run a query that shows all pairs of nodes that match price for the past 2 weeks, 2 months, etc.
Here is the query that works for a given day:
SELECT b1.node, b2.node FROM onpeak_mccavg_raw AS b1 LEFT JOIN onpeak_mccavg_raw AS b2 ON b1.price = b2.price WHERE b1.node < b2.node;
I've tried to extend the WHERE clause and add a date range but it doesn't work.
Any ideas?
SELECT b1.node, b2.node
FROM onpeak_mccavg_raw b1
JOIN onpeak_mccavg_raw b2
ON b1.date = b2.date
AND b1.node < b2.node
GROUP BY b1.node , b2.node
HAVING SUM(b1.price = b2.price) = COUNT(*)
Related
I have a table that looks like the below and would like to a count of all bookings with a a count of all guests per week. There are 30 guest fields:
locations
| Field | Type |
| ------------ | ---------- |
| location | text |
| day_number | int(11) |
| month_number | int(11) |
| year | text |
| week_number | tinyint(4) |
| start | datetime |
| end | datetime |
| guest_1 | text |
| guest_2 | text |
| … | |
| guest_30 | text |
I am using:
select locaton, count(*) AS `Number of bookings per week`, week_number,
month_number, `month`
FROM locations
GROUP BY location, week_number;
That gets me:
| location | ...Bookings | week_number | month_number | January |
| ------------ | ----------- | ----------- | ------------ | -------- |
| Location One | 3 | 2 | 1 | January |
| Location One | 5 | 3 | 1 | January |
| Location One | 2 | 4 | 1 | January |
| Location One | 2 | 5 | 2 | February |
| Location One | 5 | 6 | 2 | February |
| Location One | 1 | 7 | 2 | February |
| Location One | 3 | 8 | 2 | February |
There are many locations.
How can I combine a count of guests over the 30 guest fields for each week and location in my query?
Thanks for any help in advance.
Simply count the guests for each combination and then sum up those counts
SELECT location,week_number,month_number,SUM(
(guest_1 <> "") +
(guest_2 <> "") +
(guest_3 <> "") +
....
(guest_30 <> "")
) AS num_guests
FROM locations
GROUP BY location,week_number,month_number
It's difficult because this table is not normalized.
In my opinion is this possible please re-create this solution and create new table GUEST with foreign key from LOCATION.
Next step is create subquery for example:
(you query) t1
having 30 = (select count(*) from GUEST where idlocation=t1.idlocation and year=t1.year, week_number=t1.week_number)
It will be more better resolve because in this situation you can add more guest without change logical table.
In your situation you always check every column separately.
I have a question regarding time in MySQL.
How do i get a time stamp like this:
2014-12-07 12:54:42.000000
To correctly insert itself in this table:
The timestamp has to divide to,Hour,Dayname,Weeknumber,Daynumber
I need a fixed value of 1 in every layar of the table
+-------------+----------+--------+-----+-----+-----------x
| fixedval | Hour | Dayname| Weeknumber| Daynumber |
+-------------+----------+--------+-----------+-----------+
| 1 | | | | |
| 1 | | | | |
| 1 | | | | |
| 1 | | | | |
| 1 | | | | |
+-------------+----------+--------+-----------+-----------+
There are functions for that
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
Insert into table
(fixedval,hour,dayname,weeknumber,daynumber)
(Select 1, hour(inputdate), dayname(inputdate), weekofyear(inputdate), dayofyear(inputdate))
SELECT HOUR(your_date) as Hour,DAYNAME(your_date) as Dayname,WEEK(your_date) as Weeknumber, DAYOFMONTH(your_date) as datenumber from table.
NOTE: You can use either DAYOFMONTH/DAYOFWEEK for day number.
i am trying to get the recent 5 orders based on date_of_order and time_of_order fields in the below table and the data type of both these fields is varchar and here order_child_id is some random number which is not stored in order.
How can I write the query for this?
order_details_child table:
+------------------+------------+---------------+---------------+
| order_child_id | vendor_id | date_of_order | time_of_order |
+------------------+------------+---------------+---------------+
| 2000010313044428 | 5060091713 | 2015-03-11 | 16:44:28 |
| 2000010313044920 | 5060091713 | 2015-03-13 | 16:49:20 |
| 2000010313044951 | 5060091713 | 2015-03-11 | 16:49:51 |
| 2002880313043941 | 5000818755 | 2015-03-13 | 16:39:41 |
| 2002880313044029 | 5000818755 | 2015-03-12 | 16:40:29 |
| 3000010313044555 | 5060091713 | 2015-03-12 | 16:45:55 |
| 4000010313044555 | 5000818755 | 2015-03-13 | 16:45:55 |
+------------------+------------+---------------+---------------+
Is this what you are looking for?
select od.*
from order_details od
order by date_of_order desc, time_of_order desc
limit 5;
You should store dates and times in the native format. But if you can't for some reason, your formats are the right way to store them as strings.
I'm making the following query with MySQL:
SELECT id, from_unixtime(`timestamp`/1000, '%Y-%m-%d') as day_time, timestamp, end_timestamp, min((`end_timestamp`-`timestamp`)/60000) as min_diff_minutes from table1 WHERE imp<=130;
The table is the following (this table is only a small portion of the original one):
+--------------+------------+---------------+---------------+
| id | day | timestamp | end_timestamp |
+--------------+------------+---------------+---------------+
| 71fce85098b9 | 2014-03-09 | 1394367994962 | 1394368030106 |
| 81fgr85093y6 | 2014-03-09 | 1394367950493 | 1394367993432 |
| 12rtw63423d1 | 2014-02-15 | 1394367302837 | 1394367783463 |
| 77grs82544e7 | 2014-01-21 | 1394367003472 | 1394367903843 |
+--------------+------------+---------------+---------------+
The result of the query is the following:
+--------------+------------+---------------+---------------+------------------+
| id | day | timestamp | end_timestamp | min_diff_minutes |
+--------------+------------+---------------+---------------+------------------+
| 71fce85098b9 | 2014-03-09 | 1394367994962 | 1394368030106 | -23359195.5028 |
+--------------+------------+---------------+---------------+------------------+
How is possible to have
-23359195.5028
as result of the MIN() function, since the value "min_diff_minutes " is just the difference of "end_timestamp" with "timestamp" divided for 60000 ?
PS Anyway, the values "timestamp" and "end_timestamp" are of type BIGINT.
I have two tables containing datetime and double.
Which looks like this:
mysql> select * from ONE; | mysql> select * from TWO;
+---------------------+----------+ | +---------------------+----------+
| date | value | | | date | value |
+---------------------+----------+ | +---------------------+----------+
| 2002-03-18 10:30:02 | -181.241 | | | 2002-03-18 10:30:00 | -188.192 |
| 2002-03-18 10:30:06 | -180.673 | | | 2002-03-18 10:30:04 | -187.619 |
| 2002-03-18 10:30:10 | -180.055 | | | 2002-03-18 10:30:08 | -187.032 |
| 2002-03-18 10:30:14 | -179.459 | | | 2002-03-18 10:30:12 | -186.418 |
| 2002-03-18 10:30:18 | -178.801 | | | 2002-03-18 10:30:16 | -185.807 |
I'm trying to perform a query on the values from each of the tables with a common date column (using ONE.date as reference). However, in some cases the measurements does not have symmetric date. Above is an example of such a period (this is worst case scenario), here the values from table TWO corresponds to the values in table ONE two seconds later.
In order to sort the values of the two tables according to the time, I want to allow a "sorting accuracy", set to be a maximum time difference between the measurements of one "event". This accuracy is in this case 2 seconds.
How I wan't it to look: If the time difference between ONE.data and TWO.date is less than +- 2 seconds; join the value from TWO in ONE:
mysql> select * from ONE;
+---------------------+----------+-----------+
| date | valueONE | valueTWO |
+---------------------+----------+ ----------+
| 2002-03-18 10:30:02 | -181.241 | -188.192 |
| 2002-03-18 10:30:06 | -180.673 | -187.619 |
| 2002-03-18 10:30:10 | -180.055 | -187.032 |
| 2002-03-18 10:30:14 | -179.459 | -186.418 |
| 2002-03-18 10:30:18 | -178.801 | -185.807 |
If the difference is more than +- 2 seconds, discard the measurement.
This is what I have tried, however the query below just times out.
SELECT ONE.date, ONE.value, TWO.date, TWO.value
FROM ONE
join TWO on ONE.date = TWO.date+INTERVAL 1 SECOND or ONE.date = TWO.date
Is it possible to have a IF sentence inside the join statement such that if
if SELECT TIMESTAMPDIFF(SECOND,'2002-03-18 10:30:02','2002-03-18 10:30:00') <= 2, then join
You could use BETWEEN:
SELECT ONE.date, ONE.value value_ONE, TWO.value value_TWO
FROM
ONE join TWO
on ONE.date BETWEEN TWO.date AND TWO.date + INTERVAL 2 SECOND
ORDER BY ONE.date
Please see fiddle here.