can't calculate proper total hours? - mysql

I have following fields
Date InTime OutTime Status
2016-01-04 10:19:00 05:10:00 Out
I tried below query
SELECT sum(time_to_sec(timediff(CONCAT(`TodayDate`,' ',`OutTime`),CONCAT(`TodayDate`,' ',`InTime`)))/ 3600) as total FROM `wp_ag_punch` where UserId=$userid and TodayDate='$today'
but it's give -5.15 hr.
please help me to solve it

You are using wrong system architecture rather then using two fileds for date and time use only one which will work for all your cases
Please use following structure
DatetimeIn (timestamp) DatetimeOut (timestamp) Status
1451912118 1451912140 Out
now directly do
Select DatetimeIn - DatetimeOut as output
that will give you output in seconds
output
------
22

You should be storing the datetime fields as DateTime datatype, instead of trying to keep the two fields as separate. So for you initial DB setup, you would instead want:
| DateTimeIn | DateTimeOut | Status |
|---------------------|---------------------|--------|
| 2016-01-04 10:19:00 | 2016-01-04 05:10:00 | Out |
Which, in turn, can be queried for the difference between the two timestamps using something like:
SELECT TIMESTAMPDIFF(MINUTE,DateTimeIn,DateTimeOut) AS total_minutes
which would return:
| total_minutes |
|---------------|
| -309 |
Assuming that the later time is actually at 5 PM, the data could be expressed as:
| DateTimeIn | DateTimeOut | Status |
|---------------------|---------------------|--------|
| 2016-01-04 10:19:00 | 2016-01-04 17:10:00 | Out |
and the query:
SELECT TIMESTAMPDIFF(MINUTE,DateTimeIn,DateTimeOut) AS total_minutes
would return :
| total_minutes |
|---------------|
| 411 |
Or, if you wanted the time-span in hours, you only need to change that part of query like:
SELECT TIMESTAMPDIFF(HOUR,DateTimeIn,DateTimeOut) AS total_hours
to get the result :
| total_hours |
|---------------|
| 6 |

Actually either your out time should be '17:10:00' or date should be different as per mysql time format.
Further after seeing your record format, you don't need to concat date with time, instead you just do your calculation on time as per below-
SELECT SUM(TIME_TO_SEC(TIMEDIFF(`OutTime`,`InTime`)))/ 3600 AS total FROM `wp_ag_punch` WHERE UserId=$userid AND TodayDate='$today'

Try below query:
SELECT (time_to_sec(timediff(OutTime,Intime)) / 3600) as total FROM `wp_ag_punch` where UserId=$userid and TodayDate='$today'

I solved it.just change the 12 hour format to 24 hour format and use below query.
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(OutTime,InTime)))) AS totals FROM wp_ag_punch where UserId=$userid and TodayDate='$today'

Related

Need to fetch date, and get for how many hours date is booked from mysql

I am working on hotel booking, In which i am storing pickup_date and return_date data, table name is booked, I need to get date from booked table and need to check for how many hours that date is booked, can anyone please help me for this query, i don't have idea how to get that, Here i have added my table data, and also added my required output
Booked table
I need its output as below :
|----Date---|---Duration---|
|2019-06-06 | 18.15 |
|2019-06-07 | 19.45 |
|2019-06-08 | 21.15 |
|2019-06-09 | 24.00 |
|2019-06-10 | 24.00 |
|2019-06-11 | 24.00 |
|2019-06-12 | 09.00 |
You can use function TIMEDIFF()
SELECT pickup_date, TIMEDIFF(return_date, pickup_date) duration FROM booked
Duration will be returned in format HH:MM:SS. Not exactly what you want but you can play with that result

how to select two different date in mysql?

I'm new to database and MySQL. I'm developing a stock tracking software backend with MySQL database. I have a problem in the MYSQL query.
I need to track price change for a certain period. how can I select two different column and row based on date. for example I need MYSQL to return 'open' value from date '2019-02-27' and 'close' value from date '2019-03-01';
and calculate the % differences in between two value(which is Decimal).
Is it possible to do this kind of query in MYSQL or I should write program which send two query. one to get 'open' from '2019-02-27' and other to get 'close' from '2019-03-01'.
here is the SQL fiddle for my problem http://sqlfiddle.com/#!9/eb23e3/6
here is any example table
symbol | date | open | close | low | high
----------------------------------------------------
HCL | 2019-02-27 | 36.00 | 38.00 | 34.00 | 40.00
HCL | 2019-02-28 | 37.00 | 39.00 | 36.00 | 41.00
HCL | 2019-03-01 | 38.00 | 42.00 | 37.00 | 46.00
how can I get 'open' from date '2019-02-27' AND 'close' from date '2019-03-01'
and then calculated the % difference like (2019-02-27) 'open' value is 36.00 and (2019-03-01) 'close' value is 42.00 so the % percentage difference is +16.6%.
With this you get the 3 needed columns:
select t.*,
concat(
case when t.close > t.open then '+' else '' end,
truncate(100.0 * (t.close - t.open) / t.open, 1)
) percentdif
from (
select
(select open from dailyprice where date = '2019-02-27') open,
(select close from dailyprice where date = '2019-03-01') close
) t
See the demo
I suggest to run two different queries and then calculate the difference of the returned values

How can I select the last timestamp reading per day for multiple user id's and multiple days?

I have a database that contains user id, calories burned (value), and the timestamp at which those calories burned were recorded(reading_date). An individual could have multiple calorie readings for the same day, but I'm only interested in the last reading since it's a total of all the previous readings for that day.
IN:
SELECT
DISTINCT ON (date, user_contents.content_id)
date_trunc('day',reading_date + time '05:00') date,
user_id,
created_at,
value
FROM data
OUT:
date | user_id | created_at | value
2019-01-13 00:00:00 | 138 | 2019-01-18 06:07:52 | 81.0
2019-01-15 00:00:00 | 137 | 2019-01-15 15:43:25 | 87.0
2019-01-16T00:00:00 | 137 | 2019-01-18 04:22:11 | 143.0
2019-01-16T00:00:00 | 137 | 2019-01-18 06:12:11 | 230.0
additional values omitted
I want to be able to select the maximum reading value for each day per person. I've tried using DISTINCT statements such as:
SELECT
DISTINCT ON (date, user_contents.content_id)
date_trunc('day',reading_date + time '05:00') date,
Sometimes that results in an error message:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
Sometimes it filters out some results, but isn't always giving me the last reading of the day or only one result per person per day.
My optimal end result would look like this (the third record having been removed):
date | user_id | created_at | value
2019-01-13 00:00:00 | 138 | 2019-01-18 06:07:52 | 81.0
2019-01-15 00:00:00 | 137 | 2019-01-15 15:43:25 | 87.0
2019-01-16T00:00:00 | 137 | 2019-01-18 06:12:11 | 230.0
additional values omitted
Ultimately, I'm going to use this data to sum up the value column and determine the total number of calories burned by everyone in the dataset over a time period.
You appear to be using Postgres.
Follow the instructions in the error message. You want something like this:
SELECT DISTINCT ON (user_id, reading_date::date)
date_trunc('day',reading_date + time '05:00') date,
user_id, created_at,value
FROM data
ORDER BY user_id, reading_date::date DESC, reading_date DESC

MySQL select Current_timestamp between

I have a list of users in MySQL and on subscription the timestamp is set in the data base using the CURRENT_TIMESTAMP.
Now I want to do a select from this table where the subscribe date is between day X and day Y
I tried several queries but somehow they all turn up empty.
Here is my last version
SELECT *
FROM users
WHERE subscribe_date BETWEEN '2013-10-07'AND '2013-13-10'
As I know for sure this date: 2013-10-08 14:38:49
is in the subscribe_data field It should turn up somehow
What is the best way to do this?
Maybe good to know my 'subscribe_date' column has type 'timestamp' and is auto filled with 'CURRENT_TIMESTAMP'
Here is the data in this table:
+----+-----------+---------------------+
| id | firstname | subscribe_date |
+----+-----------+---------------------+
| 20 | Peter | 2013-10-01 14:37:17 |
| 21 | Jack | 2013-10-08 14:38:49 |
| 22 | Andrew | 2013-10-10 14:41:03 |
| 23 | Margret | 2013-10-14 14:42:46 |
+----+-----------+---------------------+
Since TIMESTAMP is up to seconds precision usually, you have to add time part:
SELECT *
FROM users
WHERE (subscribe_date BETWEEN '2013-10-07 00:00:00' AND '2013-12-10 23:59:59')
I've fixed your '2013-13-10' to '2013-12-10 23:59:59' since there's no 13-th month (and in DATETIME format it's YYYY-MM-DD, so month goes second)

Counting Occurrences of Day Names By Distinct Weeks

Apologies in advance if this question is badly worded!
I have a MySQL table which has a datetime field. How would one count the number of occurrences of each day name, for distinct weeks?
Here is the query I'm trying, and the result it gives from just over 2 weeks of data. It is (obviously) counting every occurrence for each record, whereas I want the output to be either 2 or 3.
SELECT dayname(datetime), COUNT(dayofweek(datetime))
FROM mytable GROUP BY dayofweek(datetime);
+-------------------+----------------------------+
| dayname(datetime) | count(dayofweek(datetime)) |
+-------------------+----------------------------+
| Monday | 404 |
| Tuesday | 275 |
| Wednesday | 251 |
| Thursday | 196 |
| Friday | 201 |
| Saturday | 128 |
+-------------------+----------------------------+
Grouping by week did not solve the problem. I feel as though I need to "count where week is distinct" but I'm not sure if this is possible.
Any guidance is much appreciated, thank you!
Try to count distinct values (date part only) of your datetime field. More about COUNT(DISTINCT expr,[expr...]) and DATE(expr) that returns the date part only from datetime field.
SELECT dayname(datetime), COUNT(distinct date(datetime))
FROM mytable GROUP BY dayname(datetime);
You normally group on what you are not counting.