finding most 'popular' items in multiple tables in mySQL [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have multiple tables of items which are 'ordered' by their ratings/popularity.
I need to combine all the tables into one table with a top 10.
The top 10 will combine the number of times an entry (with wildcard as the names may be slightly different) appears in all the lists and it's position in the tables.
is this possible?
I've researched Joins but it seems quite a complicated procedure given there are two factors (nubmer of entries and position in the tables).
Apologies for being vague, I didn't think I was doing so. This is my first question on stackoverflow
table 1 table 2 table 3
--------------------
bob | bob | Ian
fred | james |john
kate | fred | bob
mary | brian | brian
the 'rankings' results of the three tables need to appear in a final table (called 'final' for example)
As you can see Bob would rank highly on 'final'.
But Ian appears only once, even though he is top of the list in table 3.
Fred appears in position 2 and position 3 so should he be higher or lower than Ian.
would I need an algorithm for the sorting or is there some trick in mySQl that will examine the rankings?

You can return them with a ranking, but you need to define how that rank applies.
For example if you just return the ranking from each table then Bob appears twice in the first position. If you add those 2 ranks together it gives 2. How do you compare that to Ian who is only ranked 1 once.
For this you are probably best building a ranking from the last row (or calculating it as total number of rows - ranking).
You can get a basic ranking from each table with the following:-
SELECT some_name, #rank_1:=#rank_1 + 1 AS ranking
FROM table_1
CROSS JOIN (SELECT #rank_1:=0) sub_1
UNION ALL
SELECT some_name, #rank_2:=#rank_2 + 1 AS ranking
FROM table_2
CROSS JOIN (SELECT #rank_2:=0) sub_2
UNION ALL
SELECT some_name, #rank_2:=#rank_2 + 1 AS ranking
FROM table_2
CROSS JOIN (SELECT #rank_2:=0) sub_3
but this will give you each record from each table and their ranking.
As it is though, you have nothing to say that Bob is the first record on table_1. While it may be the first record, as far as the order returned this is not a certainty.

Related

How to find average pay rate by each company in SQL database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have this table called job opening and it has attributes ( company name, opening_available, opening_description, Hourly_Pay) whereby i have 4 rows of data, each with unique companies.
i have been given a query to calculate average pay of each companies
i am confused how to do it..here is my table
company name|opening_available|opening_description|Hourly_Pay
1. Facebook| 4 | Senior programmer | 4.50
2. Google | 5 | Web developer | 5.00
3. Metropol| 4 | Web Designer | 6.00
4. dailybug| 3 | Junior programmer | 7.50
You want an average, so you should use a so-called aggregation function named AVG. However, to use an aggregation function, you must separate the table data into parts, and the average shall be calculated upon each part. You can do this with a GROUP BY expression (the parts are groups). Now you want to split the data among the companies, and the splitting criterion will be the company name.
Also, in MySQL if you have column names with spaces, you should wrap them in backticks.
SELECT `company name`, AVG(Hourly_Pay) as average_pay
FROM `job opening`
GROUP BY `company name`

How to retrieve rows with particular number of different column values in SQL?

I am implementing an exam portal. I have teacher and student as users.
Teachers generate question set for particular subject for taking exam. He has 4 options based on exam full marks ( 20 marks, 50 marks, 80 marks and 100 marks ), duration is also fixed during marks selection as 30 min, 60 min, 90 min, and 120 mins.
I have a question table. That has field for question, answer and level(Easy, Medium, Hard). Easy question 1 marks, Medium level question 2 marks and Hard level question 3 marks.
Any number of questions could be added to the set by teacher. And questions will be fetched randomly from database. Also each student should get 6 easy, 4 medium and 2 hard level question in a set of 20 marks, i.e. total 12 questions to be fetched randomly following criteria of levels. Similarly, for 50 marks set, 15 EASY level, 10 MEDIUM level and 3 HARD level questions have to be fetched and so on.
Please avoid all other conditions you think must be present and help me with forming an mysql query or just help me with some clues of what sql clause should I use.
I'd use a union select two times additionally to first query so each select gets a set by question level.
From just the title, I would guess you are looking for
HAVING COUNT(col) = 7

Using two columns to obtain count on another in SQL

I have a simple question that I wasn't really sure how to search for (or title!). I apologize if this has been asked a million times. For the following table, how do I generate a report that will detail the number of companies that a person has worked for and how many people have also worked for that same number? So, for example, this table should return:
people, companiesperperson
1, 1
2, 2
1, 3
for the following table called personalinfo:
id_number first last company
1 John Doe Intel
2 John Doe Microsoft
3 Phil Jenkins Amgen
4 Phil Jenkins Bayer
5 Phil Jenkins Sanofi
6 Josh Edwards Walgreens
7 Amy Dill URS
8 Amy Dill ARCADIS
Let me know if this is still confusing and if I can further clarify what I am looking to do.
Thanks!
This is a rough estimate of the query but
SELECT count as companiesperperson, COUNT(first, last) as people FROM
(SELECT COUNT(company) as count, first, last FROM personalinfo GROUP BY (first, last)) as a
GROUP BY count
To explain the query first in the subquery we are asking for the names and count of companies after splitting up all the rows by names
Then in the outer query we split up all the rows by their count and ask how many unique names can be found in each group.
There may be a few syntax errors I've left straggling but the group by feature is really what's essential to understanding how to solve this question.

How to normalize category database [closed]

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 8 years ago.
Improve this question
I've got a MySQL DB with ~5000 rows. It occurred to me that before adding anymore records or columns I need to get the database normalized. But even with all the research I've done I'm struggling to understand several concepts. Here is an example of my existing database schema.
PK Category1 Category2 Category3 Produce_String Keywords Zip State City Country
1 Vegetable Potato f5kkfid34fbn organic ... ..... ... .....
2 Vegetable Potato plf85jfuvj organic,fresh ... ..... ... .....
3 Vegetable Cherry Tomato jf9vmu37jg9 fresh
4 Fruit Lemon kfkt8hkf0e fresh,yellow
5 Fruit Lemon fkg8rr03gnf
6 Fruit Red Apple fkf9gkty367r6 crispy
My main misunderstanding, is how to relate the data to one another once the columns are separated into individual tables? For example, in a DB client I can see the rows and how they relate to one another, but if I separate them this will no longer be the case. I am also concerned with having to update multiple tables for the same record but I suppose this is unavoidable.
Also, I'm not clear on the proper way to normalize this. My mind tells me to only separate the Keywords column since it's the only column that has comma separated entries. But by normalization standards I believe I need to separate the categories, keywords, and location.
EDIT
Another concern I have, is that if I put the categories in a separate table, each with their own row, I lose the structure. So I lose the ability to sort by the specific categories. For example, the vegetable category would not be related to a fruit. Since the Produce_String is unique, could I use it as the foreign key in the other tables?
You can have separate Category and Keywords tables
category keyword
------------------------- -------------------------
id | name id | name
1 | Vegetable 1 | organic
2 | Potato 2 | fresh
Than make two additional tables for MANY TO MANY relations:
category_to_product keyword_to_product
------------------------- -------------------------
category_id | product_id keyword_id | product_id
1 | 1 1 | 1
1 | 2 1 | 2
2 | 1 2 | 2
And to update categories for product #1:
DELETE * FROM `category_to_product` WHERE `product_id` = :product_id;
INSERT INTO `category_to_product` (`category_id`, `product_id`) VALUES (1, 1), (2, 1), (8, 1);
This is a question with a potentially long answer, and you might be better off starting with somewhere like wikipedia or similar. But in a nutshell:
Normalisation usually solves more problems than it causes. Consider someone who spelt "Vegetable" as "Vegteable" for one of the rows in your example, or consider if you wanted to add a fourth category. Or what about if you wanted to change all instances of the category "Baby Marrow" to "Zuccini". You are correct that both of these could be implemented as separate tables.
One of the criteria that you can use for deciding whether to normalise or not is to think about where you want to control the integrity of the data. You may be in control of application code now, that ensures that the category names are always used consistently, but it's hard to foresee what applications will come in the future. Keeping the list of categories in it's own table makes sure that when you link two products to the same category, they are indeed linked to the same category (i.e. the single row "Vegetable" in the category table). When you change a category, you can change it in one place. You can easily find all products linked to a single category before you delete a category, and so on.
Yes, once the data is in separate tables, you don't see it all in one row any longer, but joining the data is what relational data is all about, and you can probably use database views to recreate the layout that you've shown us from the underlying normalised data. It is very normal to join several tables in an SQL select statement.

MySQL select distinct dateTime. Cant figure this query out

My users answer two different questions, they are added to my answers_table as one row each. I'm trying to count how many users have answered my questions which seems simple at first. Count(*) and divide by 2. YES! But: the users can add new questions as they go, so suddenly there might be three questions, all answers/3 becomes an incorrect number of users if there was two questions for a while and then three, for example!
Can anyone figure out a query for doing this? So far I've got
SELECT COUNT(DISTINCT date) as totNum FROM login_answers
But it doesn't take the time in consideration. Meaning i just get the amount of answers in total for that day, not knowing how many questions was available that day.
Here's my table:
qid is the QuestionID so i guess thats somewhere to start?
id qid answer date deviceID
1502 2 2 2012-10-19 08:42:41 7
1503 1 3 2012-10-19 08:51:53 7
1504 2 2 2012-10-19 08:51:53 7
1505 1 4 2012-10-19 09:05:23 7
1506 2 2 2012-10-19 09:05:23 7
1507 1 4 2012-10-19 09:40:59 7
My proposed solution would be to store a session Id or some auto-generated key against the answers to uniquely identify a "session" of 2, 3 or more answers from a single person.
The query would simply be
SELECT COUNT(DISTINCT session_id) totNum
FROM login_answers
With your current schema with no identifiable key, it's quite hard to answer your question correctly if at all possible.