Jump to content

Determining an AVERAGE date


NiallFH
Go to solution Solved by Barand,

Recommended Posts

Did some experimenting and, amazingly, this seems to work

SELECT * FROM testtime;
+----+------------+
| id | start      |
+----+------------+
|  1 | 2014-05-22 |
|  2 | 2014-07-24 |
+----+------------+

SELECT CAST(ROUND(AVG(start),0) as DATE) as average FROM testtime;
+------------+
| average    |
+------------+
| 2014-06-23 |
+------------+
Link to comment
Share on other sites

Thanks Barand,

 

Trying this just now but it only seems to return a year? No month or date showing.  

$get_average_dob = mysql_query("
SELECT 
	CAST(ROUND(AVG(P.PlayerDOB),0) as DATE) as dob
FROM 
	tplss_players P, tplss_appearances A, tplss_matches M
WHERE 
	P.PlayerID = A.AppearancePlayerID AND
	A.AppearanceMatchID = M.MatchID AND
	M.MatchDateTime = '$matchdate' AND
	P.PlayerDOB IS NOT NULL
",$connection) or die(mysql_error());
Link to comment
Share on other sites

Here it is:

-- phpMyAdmin SQL Dump
-- version 2.11.11.3
-- http://www.phpmyadmin.net
--
-- Host: ###
-- Generation Time: May 22, 2014 at 03:04 PM
-- Server version: 5.0.96
-- PHP Version: 5.1.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `###`
--

-- --------------------------------------------------------

--
-- Table structure for table `tplss_players`
--

CREATE TABLE `tplss_players` (
  `PlayerID` int(10) unsigned NOT NULL auto_increment,
  `PlayerFirstName` varchar(32) character set utf8 NOT NULL,
  `PlayerLastName` varchar(64) character set utf8 NOT NULL,
  `PlayerFriendlyName` varchar(200) collate latin1_general_ci NOT NULL,
  `PlayerNickName` varchar(128) collate latin1_general_ci NOT NULL default '',
  `PlayerSeasonID` int(11) unsigned NOT NULL default '0',
  `PlayerPositionID` tinyint(1) unsigned NOT NULL default '0',
  `PlayerNumber` tinyint(3) unsigned NOT NULL default '0',
  `PlayerDescription` text collate latin1_general_ci NOT NULL,
  `PlayerProfile` text collate latin1_general_ci NOT NULL,
  `PlayerPublish` tinyint(1) unsigned NOT NULL default '1',
  `PlayerShowStats` tinyint(1) NOT NULL default '1',
  `PlayerAllData` tinyint(1) NOT NULL default '1',
  `PlayerTask` varchar(128) collate latin1_general_ci NOT NULL default '',
  `PlayerFullName` varchar(128) collate latin1_general_ci NOT NULL,
  `PlayerHeight` varchar(128) collate latin1_general_ci NOT NULL default '',
  `PlayerDead` tinyint(1) NOT NULL default '0',
  `PlayerDOB` date default NULL,
  `PlayerDied` date default NULL,
  `PlayerBirthplace` varchar(220) collate latin1_general_ci NOT NULL,
  `PlayerDeathplace` varchar(220) collate latin1_general_ci NOT NULL,
  `PlayerPos` varchar(128) collate latin1_general_ci NOT NULL default '',
  `PlayerYouthID` tinyint(3) NOT NULL default '9',
  `PlayerSigned` date default NULL,
  `PlayerFee` int(13) NOT NULL default '0',
  `PlayerNationID` tinyint(3) NOT NULL default '0',
  `PlayerCapped` tinyint(1) default NULL,
  `PlayerCaps` tinyint(3) default NULL,
  `PlayerCapsGoals` tinyint(11) default NULL,
  `PlayerCapsNotes` text collate latin1_general_ci,
  `PlayerIntDebut` date default NULL,
  `PlayerIntOpponent` tinyint(11) default NULL,
  `PlayerHomegrown` tinyint(1) NOT NULL default '0',
  `PlayerExpiry` date default NULL,
  PRIMARY KEY  (`PlayerID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1493 ;

Edited by NiallFH
Link to comment
Share on other sites

I had tried this:

$get_average_dob = mysql_query("
SELECT 
	FROM_UNIXTIME(avg(unix_timestamp(P.PlayerDOB))) as dob
FROM 
	tplss_players P, tplss_appearances A, tplss_matches M
WHERE 
	P.PlayerID = A.AppearancePlayerID AND
	A.AppearanceMatchID = M.MatchID AND
	M.MatchDateTime = '$matchdate'
",$connection) or die(mysql_error());

However, the result it presents seems wildly inaccurate.  To the point where a bunch of players who were born between 1958 and 1963 returns an average DOB of some time in 1969??

Link to comment
Share on other sites

Ha! Okay, it seems to work fine when the players involved are born AFTER 1969?

 

But any time there are players involved who were born before that date, the result seems to default to 1969-12-31.

Edited by NiallFH
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.