Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've done a system for a client where they can nominate colleagues for doing a great job. Each person has 12 nominations to give annually.
So I've got a database with a table nominations which stores, id, nominator(id), nominated(id), reason and date.
I've also got a table user which stores user data such as, total nominations given, received, id, email etc.
So I'm creating a page from which you can pull reports.
Here you can choose a start date and end date and amount of records you would like.
How would the SQL query look to determine who made the most nominations between the specified dates?
I'm not a SQL guru at all...so any help would be appreciated very much.
After some research I've managed to find out COUNT(*) is the way to go...but don't want to run a query for every user that nominated between the specified dates...and sorting it this way could be a problem.
Please any help would be great.
select nominator, count(*)
from yourTable
where nominatedDate >= '1 Jan 2013' and nominatedDate <= '31 Jan 2013'
group by nominator
When you do aggregation functions (like COUNT. MIN, MAX, AVG) you either need to apply them to every row selected, which will give just one row in the output, or to GROUP BY items you want to make into sub-totals. In this case, for each value of Nominator in the table we get the Nominator value, and the count of rows containing that value.
The Where clause limits the counted rows to those where nominatedDate is in the given range. You can put AND and OR other tests (its already got one and).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm creating a function that retrieves records based on several different fields and then stores those records so we can query them later.
When querying the records, we're selecting a record to be used elsewhere if it matches the requirements of another set of fields/criteria.
For example:
Returning employee records so that one of them can be selected for a new position (management maybe?) based on fields such as skill set, location, and so on...
The problem
Say I loop through these stored records (in an array or something similar) and I check if the first employee in the array is suitable for the position, then find out they are, so then add them to the position, I'm neglecting the other x amount of employees that have been stored. I feel like this would be an issue because it might turn out that the order in which someone is stored might determine the likelyhood they are chosen for the position.
I thought this might be solved by creating a sorting function to sort the stored records based on the employers preferences (location, salary, availability,...), although I'm not sure how to implement this.
I'm wondering if there is any built in MySQL functions that would help sort the records based on something?
This might be something I might have to figure out on my own, but I thought I'd ask just in case there was anything useful I could use.
I hope the question was clear. If not, please comment below.
Just thinking out loud - won't sorting create the same issue you are trying to avoid - unless you can come up with a weighted score...
create table as employees_to_consider as
select e.*,
availability_score*availablity_factor
+ salary_score*salary_factor
+ location_score*location_factor as weighted_score
from employees e
where -- whatever your criteria is for selection here
order by weighted_score
The real task is deciding how to determine the score for each factor and what the appropriate weight should be.
For example, salary score can be determined by taking the ratio of what the employee is willing to work for by the target. If they match, it can be scored as 50. The number can be increased by the amount the employee is willing to work under the target and decreased by an amount they are over. Salary is pretty important so the factor might be 33%.
Similarly, if the employee lives with 15 minutes, then they can be scored at 75, within 30 at 50, over 30 25. Location is not as important as salary so it is scored at 10%.
Hopefully, you will be able to assign meaningful scores and factors to each measure.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I´m trying to get entries from my mysql database which represent a certain "hotness". Let´s say those entries are music tracks which carry attributes like "played" (how many times this track was played) and the attribute "added" (when the track was added (in timestamp format)). I already have the category "newest", which filters for the added-attribute and the category "top" which filters for the views-attribute. Now I need the category "hot" which should combine both. I came up with this formula: hotness = views / lifetime. So if a track has been played a lot of times but added recently it might be hot. And the other way round. Does that make sense? Anyway: How can I create a sql query in php which gives me the entries which have the highest "hotness"?
My query for the top-category:
$sql = "SELECT * FROM tracks ORDER BY views DESC LIMIT 0,35";
query for new-category:
$sql = "SELECT * FROM tracks ORDER BY added DESC LIMIT 0,35";
Thanks in advance.
To be really able to track the hotness, you have to have a history of when which track was played.
The total number of times a track was played and the date it was added added only allows for querying for all-time popular. The case that a track was added years ago but is getting popular now cannot be distinguished from it was added years ago, was popular back then but is not anymore.
You could add a new table where you store a date, a track ID and the number of times it was played on that date. Then you can query for all those tracks that were played a lot in the last day, 10 days and so on.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
most likely a clueless question but I would like to start off on the good foot:
Despite trying my best, I have actually never really learned to program and I'm kind of "learning as I go" so please excuse me if this seems very obvious to you...
It's more of a suggestion and feedback kind of question rather than pure programming.
My situation is the following:
I'm building a racing game that would receive various inputs from a number of users (through a php website), I am storing that information in a MySQL database, and once a week I would like to process all that information to generate "lap times", which will then create a race (my "output").
Not taking into account the various methods of calculating that output, I need to do two important things which I'm not sure how to begin with at all :
1) Storing the race information for every user (lap time per lap, fastest lap, race position per lap, race position at end of race, award points depending on the position).
Where and how should I optimally store those informations ?
I have created a race DB with a unique identifier that auto increments, I'm thinking I will generate 1 set of data for each race, so should I store all the information pertaining to that race in there ?
Would I then create a data row (with type time?) for the lap time informations (1 row for lap1, 1 row for fastest, etc... ?)? But how would I know which user (I have a unique userID for each) did which lap (how would I assign the userID to the lap time)?
2) At the end of the race I need to award points depending on race position at the end, should I just compare total lap times (additional row?) and sort by lowest first ? The points data would be stored in the user DB ?
I appreciate any input you might have for the modeling of this project !
Drop every lap_round, lap_time and position in the DB and add a user_id and a race_id.
Afterwards query the laps. That way you can tell which is fastest overall, fastest per user, time per lap and much more.
To get the position query the db for the last lap. It holds its position.
Points are user based, so put them in the user table. Just add. But if you want to tell how many points were added per race than make a seperate table points (user_id, race_id, points)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to retrieve content content based on their time stamp. And my query is
SELECT link,title,timestamp,photo,link,author,comments FROM posts
WHERE timestamp='*Todays Date -1*' LIMIT 6
If the query returns zero rows, how can I query for results 2 days back or three days back using a while statement?
Why not just query full table (without timestamp filter) and return limit of 6 rows. in this case there would be no reason to iteratatively query
SELECT link,title,`timestamp`,photo,link,author,comments
FROM posts
ORDER BY `timestamp` DESC
LIMIT 6
This gives you up to 6 records with most recent records returned first. You should have index on timestamp field for this query, which is OK because you should already have one because you were previously trying to filter on this field.
If you have a case where more than 3 rows show up within last day and you don't need to show records from previous days, this is easy enough to achieve by inspecting the values as you loop through the result set.
This also prevents you from getting into an infinite loop if you only have 2 records in the database.
Also note timestamp is a reserved word, you should be careful when using such field names. If you have to use a reserved word as field name, you must enclose it with backticks.
maybe a for could work
$i=1
for ($row='';$count=0;$i++) {
$result = mysql_query("SELECT link,title,timestamp,photo,link,author,comments FROM posts WHERE timestamp='*Todays Date -$i *' LIMIT 6");
$count = mysql_num_rows($result);
}
I think the query that you want both filters the results and orders them.
SELECT link, title, timestamp, photo, link, author, comments
FROM posts
WHERE timestamp <= '*Todays Date -1*'
ORDER BY timestamp desc
LIMIT 6;
If you actually want 3 rows and not 6 (as suggested by the post title), then change the 6 to 3.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't find an answer to my question, it's probably easy but... anyway!
I have a database with every NHL game for one specific team in a table. Every game has been entered in the good order.
On my home page, I would like to display the upcoming game as well as the result of the previous game. How can I create a mySQL query to display the previous game based on today's date?
Thanks!
Do your game records have a timestamp or datetime value?
If so you could write a query ordering your games by the date smaller that now() and limit by one.
The query should look like this:
select * from games where gamedate < now() order by gamedate desc limit 1