Jump to content

[SOLVED] Struggling with an average kinda thing...!


Recommended Posts

Hi

 

I have two tables, one called users, one called fans.

 

Users:

username, joindate

 

Fans:

username, fanof

 

 

A user can become a fan of another user, so:

 

Users:

Bob, 2009-01-14

Bill, 2009-01-14

Rita, 2009-01-12

Sue, 2009-01-05

 

Fans:

 

Bob, Sue

Bob, Rita

Bill, Bob

Sue, Rita

 

 

What I'm trying to do is calculate the average number of fans for each user since they joined.

 

i.e. Rita has two fans (Sue and Bob) since she joined 2 days ago, therefore she has an average fan score of 1 (2 fans in 2 days)

 

I have this sql statement which works:

 

SELECT users.username AS NAME FROM users ORDER BY (SELECT ((SELECT count(*) as fans FROM fans WHERE fanof =NAME)/(SELECT DATEDIFF(NOW(), users.joindate) as DiffDat FROM users WHERE username=NAME)) AS average FROM users WHERE username = NAME) DESC

 

and produces a list of names in the right order..Great!

 

But, I want it to produde a list of names AND the average fans they have.

 

I have no idea.

 

Please help.

how about this:

SELECT f.fanof, COUNT(f.username) as numfans, (DATEDIFF(NOW(),u.joindate)) as numdays, COUNT(f.username)/(DATEDIFF(NOW(),u.joindate)) as fansperday FROM fans f LEFT JOIN users u ON f.fanof = u.username GROUP BY f.fanof

you should be able to run that query...if the tables are structured as listed. i used the following table structure and data:

CREATE TABLE `users` (
  `username` varchar(255) NOT NULL,
  `joindate` date NOT NULL,
  PRIMARY KEY  (`username`)
);
INSERT INTO `users` (`username`, `joindate`) VALUES 
('Bill', '2009-01-14'),
('Bob', '2009-01-14'),
('Rita', '2009-01-12'),
('Sue', '2009-01-05');

CREATE TABLE `fans` (
  `username` varchar(255) NOT NULL,
  `fanof` varchar(255) NOT NULL
);
INSERT INTO `fans` (`username`, `fanof`) VALUES 
('Bob', 'Sue'),
('Bob', 'Rita'),
('Bill', 'Bob'),
('Sue', 'Rita');

used this query:

SELECT
    u.username, 
    COUNT(f.username) as numfans,
    DATEDIFF(NOW(),u.joindate) as numdays,
    COUNT(f.username)/(DATEDIFF(NOW(),u.joindate)) as fansperday
  FROM fans f LEFT JOIN 
    users u ON f.fanof = u.username
  GROUP BY u.username

and got the following:

username  numfans  numdays  fansperday

Bob      1        0        NULL

Rita      2        2        1.0000

Sue      1        9        0.1111

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.