MySQL AVG TIME Duration Calculation and Related Functions? - mysql

table with login data of the website users with two datetime columns. One is logged in time and other one is logged out time.
table has following columns
user_id (user id of the
datetime (start login time DATETIME )
datetime_end (end logout time DATETIME)
From this data I need to generate a report or calculate the following
Daily AVG Login Duration for Users (i.e 01 March 2015 its 21 mins , 03 November 2016 its 25 mins ..etc )
second report is
Current Day (Last 24 hours) based on Hour AVG Login Duration (i.e same as above but only per hour AVG)
is there a way i can achieve this via MySQL query ? (or SQL query)
table is create is
CREATE TABLE IF NOT EXISTS `users_login` (
`login_id` int(11) unsigned NOT NULL,
`user_id` int(11) unsigned NOT NULL DEFAULT '0',
`last_ip` varchar(45) NOT NULL DEFAULT '0.0.0.0',
`server_ip` varchar(225) DEFAULT '0.0.0.0',
`country` varchar(2) NOT NULL DEFAULT '00',
`continent` tinytext,
`datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`datetime_end` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Something like this for the first one
SELECT DATE(datetime),AVG(TIMESTAMPDIFF(MINUTE,datetime,datetime_end)) as DailyAvg
FROM users_login
GROUP BY DATE(datetime)
And for hourly :
SELECT DATE(datetime),extract(hour from datetime) as HourCol,
AVG(TIMESTAMPDIFF(MINUTE,datetime,datetime_end)) as HourlyAvg
FROM users_login
WHERE DATE(datetime) = date(now())
GROUP BY DATE(datetime),extract(hour from datetime)
Of course this queries have a few exceptions.. You didn't explain you entire logic so I assumed you want to group by the hour of the login and the date of the login..

Related

When i Create table with Month he only given 0000-00-00

I create table for hour
CREATE TABLE hour (
Name1 varchar(25) not null,
Datee Datetime not null DEFAULT (CURRENT_TIMESTAMP()),
Monthh date not null DEFAULT (MONTH(CURRENT_DATE()))
);
Mysql only give me 0000-00-00 not Name where i use button in php. What is wrong with this and how correct? In my opinion my phpadmin dont have a MONTH function
CREATE TABLE hour ( Name1 varchar(25) not null,
Datee DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Monthh TINYINT NOT NULL DEFAULT (MONTH(CURRENT_TIMESTAMP))
);
fiddle
PS. HOUR is Reserved word - I'd recommend to rename a table.
Store full timestamp and it's month part in separate columns in same table is absolutely redundant. You can story only datetime and get it's month in select query:
CREATE TABLE Godzina (
Nazwa varchar(25) not null,
Randka Datetime not null DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO Godzina (Nazwa) VALUES ('TestName');
SELECT
Nazwa,
Randka,
MONTH(Randka) AS Miesiac
FROM Godzina;
Test it on SQLize.online

MySQL Interval 1 Second Not Working

I'm sending syslog-ng to Percona. I have different logging sources filtered into different MySQL tables. I'm trying to determine the number of logs per second, minute and hour.
This is how the table was created:
CREATE TABLE syslog.switchlogs (
host varchar(40) DEFAULT NULL,
facility varchar(10) DEFAULT NULL,
level varchar(10) DEFAULT NULL,
tag varchar(10) DEFAULT NULL,
program varchar(50) DEFAULT NULL,
msg text,
seq bigint(20) unsigned NOT NULL AUTO_INCREMENT,
timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (seq),
KEY host (host),
KEY timestamp (timestamp),
KEY host_timestamp (host,timestamp)
) ENGINE=InnoDB;
Most of the tables get the results I'm expecting:
Last hour:
SELECT count(seq) as thecount FROM syslog.switchlogs WHERE timestamp>=DATE_SUB(NOW(),INTERVAL 1 HOUR);
Last minute:
SELECT count(seq) as thecount FROM syslog.switchlogs WHERE timestamp>=DATE_SUB(NOW(),INTERVAL 1 MINUTE);
Last second:
SELECT count(seq) as thecount FROM syslog.switchlogs WHERE timestamp>=DATE_SUB(NOW(),INTERVAL 1 SECOND);
I get results like this:
Hour: 804
Minute: 16
Second: 1
One of my tables has VMware logs and I get odd results from counts...
Hour: 30,180
Minute: 24,278
Second: 24,160
That's obviously wrong. If I look at the last 50 logs in the table there's only 10 of them in the last second and 39 in the last minute. Why is the SQL above not working as expected?

optimize mysql query with huge data using indexing

every minute the table size is increasing with 3 columns TimeInsert(timestamp), errorMsg (varchar 50) as 'Success' , 'Failure' and amount (int) . When i run the below query to generate reports of last 3 days , the below query takes a lot of time , how can i make it run fast , using multi column indexes , shall i use indexing for the 2 columns , timeinsert and errorMsg ? the table is myisam
select sum(amount) from sdpcallbackairtel where TimeInsert >= NOW() - INTERVAL 3 DAY and errorMsg ='Success' and hour(timeinsert)=16 ;
CREATE TABLE tablename (
`Id` int(10) NOT NULL DEFAULT '0',
`errorMsg` varchar(10) DEFAULT NULL,
`amount` int(5) DEFAULT NULL,
`TimeInsert` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Mysql Subquery between specific dates

I'm a newbie to MySQL querying and need some assistance with the subqueries.
I am using ASP .NET charting control that retrieves data from MySQL.I want to display a drill down chart and need some help on MySQL subquery.
Below is my table:
CREATE TABLE IF NOT EXISTS `data` (
`runtime` smallint(6) NOT NULL,
`app` varchar(60) NOT NULL,
`process` varchar(40) NOT NULL,
`username` varchar(51) NOT NULL,
`time` time NOT NULL,
`date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Step 1 :
Showing a pie chart of Top 10 users with highest time between 2 dates.
I get the top 10 users used between 2 dates using the below query:
SELECT username ,SUM(runtime) as Runtime,
process,ROUND(SUM(runtime/201600),2) as 'Total Time',
role ,
date
FROM data
WHERE `date` BETWEEN 'date1' AND 'date2'
Group BY process LIMIT 10.
Step 2:
When user clicks on the individual user in chartArea, I wan to display the top 10 apps/process between specific dates.

Don't understand the date format in mysql like 1286066935 - UserCake

I'am using UserCake for UserManagement - in the table userCake_Users there is a column LastSignIn but the value is in this format: 1286066935
with this function I get the right Date
public function updateLastSignIn()
{
global $db,$db_table_prefix;
$sql = "UPDATE ".$db_table_prefix."Users
SET
LastSignIn = '".time()."'
WHERE
User_ID = '".$db->sql_escape($this->user_id)."'";
return ($db->sql_query($sql));
}
but which format is 1286066935?
this is the sql file
--
-- Table structure for table `Users`
--
CREATE TABLE IF NOT EXISTS `Users` (
`User_ID` int(11) NOT NULL auto_increment,
`Username` varchar(150) NOT NULL,
`Username_Clean` varchar(150) NOT NULL,
`Password` varchar(225) NOT NULL,
`Email` varchar(150) NOT NULL,
`ActivationToken` varchar(225) NOT NULL,
`LastActivationRequest` int(11) NOT NULL,
`LostPasswordRequest` int(1) NOT NULL default '0',
`Active` int(1) NOT NULL,
`Group_ID` int(11) NOT NULL,
`SignUpDate` int(11) NOT NULL,
`LastSignIn` int(11) NOT NULL,
PRIMARY KEY (`User_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
My guess is it's a UNIX Timestamp.
but which format is 1286066935?
It's Unix time. It's the number of seconds since midnight GMT on 1st January 1970.
1286066935 represents 00:48:55 GMT today, 3rd October 2010.
You can convert a Unix timestamp to an 'ordinary' date/time using an online converter like this one. Alternatively you can use the date command, on Linux:
$ date -d #1286066935
Sun Oct 3 01:48:55 BST 2010
It's called a Unix Time Stamp
Basically, in Unix time stamps are represented as the number of seconds from the Unix Epoch or Jan 1, 1970
It's UNIX time: http://en.wikipedia.org/wiki/Unix_time
The time()-function in php (assuming that's what you're using) returns the time in the very same format, for example.
Looks like a UNIX timestamp to me (number of seconds since 1st January 1970.)
Assuming you're using PHP based on that code snippet, you can use the time() function to return the current UNIX timestamp for insertion into your database.
If you wanted to do the processing with your MySQL query, take a look at the UNIX_TIMESTAMP() function.
Probably UNIX time. Number of seconds since 01/01/1970.