Negative result in MySQL query with BIGINT values - mysql

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.

Related

Select IDs (same table) that don't have any records/rows after a specific date

I have a table, let's say, like this one:
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| user_id | mortgage_id | value | classification | created_at |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 6c1e1f12-2e5d-488d-b02d-29fcffe783f2 | 1e76bcbb-70ee-4966-87fd-1d6024a04513 | 0 | initial | 2014-08-23 14:25:42 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 49dc3dab-d2d0-400b-b964-71e03339d475 | 59366911-f1a8-4a8c-b7ea-c3257d04478e | 1 | created | 2015-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 76ce889b-2f2c-435f-8754-7c5ec15cbfcb | b962e26b-1ba6-4547-8eb8-167989a0705e | 5 | created | 2016-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 5d9f1892-05c0-4b0a-b5d9-a501595fa351 | fb4be36e-e156-4c1b-bd40-422d30646f8e | 8 | created | 2016-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 49dc3dab-d2d0-400b-b964-71e03339d475 | 2cee0bc7-744f-4f51-a094-f5eb66ac482e | 2 | created | 2017-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 76ce889b-2f2c-435f-8754-7c5ec15cbfcb | b0d27c9e-907c-43df-abd2-5772785cb91c | 0 | created | 2017-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
I'm trying to fetch/get all the distinct/unique user_ids that don't have any records from a given moment in time and onwards.
For instance, if I choose that "time frame" to be: After 2017-01-01 00:00:00, the return would be:
|--------------------------------------+
| user_id |
|--------------------------------------+
| 6c1e1f12-2e5d-488d-b02d-29fcffe783f2 |
|--------------------------------------+
| 5d9f1892-05c0-4b0a-b5d9-a501595fa351 |
|--------------------------------------+
I have this query, but I think there should be a better way to do this:
SET #timestamp = '2017-01-01 00:00:00';
SELECT DISTINCT user_id
FROM mortgages
WHERE user_id NOT IN (SELECT DISTINCT user_id FROM mortgages WHERE created_at > #timestamp);
I would use group by:
select user_id
from mortgages
group by user_id
having max(created_at) <= #timestamp;

Divide timestamp in MySQL

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.

How to get recent 5 fields based on date and time which are passed as varchar

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.

MySQL - Join tables and convert rows to columns

I have two tables similar to these (t_stamp would normally be a DATETIME, abbreviated here for clarity):
datapoints
+------+---------+----+---------+
| ndx | value | ID | t_stamp |
+------+---------+----+---------+
| 1 | 503.42 | 1 | 3/1/15 |
| 2 | 17.81 | 2 | 3/1/15 |
| 4 | 498.21 | 1 | 3/2/15 |
| 4 | 19.51 | 2 | 3/2/15 |
+------+---------+----+---------+
parameters
+------+----+---------------+-------+
| ndx | ID | description | unit |
+------+----+---------------+-------+
| 1 | 1 | wetwell level | ft |
| 2 | 2 | effluent flow | MGD |
+------+----+---------------+-------+
I'm looking to combine them so that the descriptions become column headers and list the values in order of time stamp, end result looking something like this:
new table
+---------+---------------+---------------+
| t_stamp | wetwell level | effluent flow |
+---------+---------------+---------------+
| 3/1/15 | 503.42 | 17.81 |
| 3/2/15 | 498.21 | 19.51 |
+---------+---------------+---------------+
Bearing in mind, I have considerably more rows in each table so I'm looking for something dynamic. It could be query or stored procedure based. Thank you for any help!

mysql query - find columns that match for given date range

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(*)