MySQL finding what rank a number has in multiple columns - mysql

I've looked a lot on here to see if this has been asked but I couldn't find it. So I'm sorry if it has been answered before.
My question is very basic but for some reason I cannot seem to get it.
I have the following table:
ID Mike Carl Steve Josh
1 2$ 3$ 1$ 5$
2 4$ 5$ 1$ 2$
So what I need is to know what position out of all ID's does Mike rank from lowest to highest? This would mean for ID 1 it would yield position 2 because he is the second lowest. For ID 2 it would yield position 3 because he is the third lowest. This will go on for about 200000 positions for about 20 people but I just lowered it to simplify it.
Please let me know if you have any ideas and thank you so much in advance for all the help!

Just exported to excel and used rank on each column.

Related

Write a query to display the highest average obtained from the students

enter image description here
Data in Mark table:
Value Subject_ID Student_ID
------ ----------- ------------
91 1 1
90 2 1
90 3 1
100 4 9
67 5 9
80 5 4
90 4 4
I have tried the query:
select round(avg(value),2) as avg_mark from Mark;
Expected Output:
enter image description here
I have tried the following code, it executes successfully and gives the desired result, however it fails to clear test case and IDK why?
and gives the desired result
Well.. it might give your desired result but it doesn't give their desired result, 90.33
The question asks for the highest average, which means you need to perform an average per student, then max to find out what the highest average was (Student 1 has the highest average, with 90.33). Perform the AVG in a subquery grouped by student id, then MAX it in an outer query. Perform the rounding in the outer, not the inner (it doesn't matter in this case, but in terms of habit you should strive to perform roundings last - work in as many dp as possible, then round just before output to prevent rounding errors compounding)
I haven't done it for you because this strongly seems like homework. Make an attempt at it using the description i gave above and I'll be happy to help out with anything that's gone wrong

SSRS How to replicate Column Total in each row

Team Sales TotalSales
A 10 70
B 25 70
C 30 70
D 5 70
I have a dataset for an example please refer the above. The total sales for all 4 teams are 70. I wanted to replicate Grandtotal e.g.70 for each Row on a Matrix. Is it possible? Thank you in advance.
Just add a column with the expression
=SUM(Fields.Sales.Value, "mydataSetname")
Remember to click the check mark if you think an answer has solved you question instead of just voting up. Remember and answer is good if if answers you actual question, if you have left information out of your question and you still have an issue, start a new question.

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.

Determining the best result given two variables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm looking for a way to weight my results to get the "best" highest rated result.
I have a table consisting of rating (0-5), mentions and name.
I.E.
RATING MENTIONS NAME
2.5 15 Bob
4.4 14 Susan
1 60 John
5 2 Steve
Both mentions and rating are important so sorting by just rating won't get the desired results.
For this example; while Steve has the highest rating he has very little mentions so i'm not very confident that he is the "best" highest rated person. Susan has several mentions and a high rating so she should surpass Steve. John has a very low rating but lots of mentions, he should only surpass any of the other people if he has a ridiculous amount of mentions.
The ideal result would be something similar to
RATING MENTIONS NAME
4.4 14 Susan
5 2 Steve
2.5 15 Bob
1 60 John
Appreciate the help!
Simplest way to do this is
RATING * RATING * Mentions
Which would provide the following:
RATING MENTIONS NAME SCORE
2.5 15 Bob 93.75
4.4 14 Susan 271.04
1 60 John 60
5 2 Steve 50
It is a pretty simple way to 'weight' the value of the rating.
Obviously you can go more complex but I would think the above is sufficient and the Query is easy so I will let you try and work that out yourself if you like the method!
Obviously you can just add another RATING if you want a LOT of weight on the rating OR multiply it by a a fixed amount - but the squaring / POWER is key (you could try RATING ^ 2.5) (^ is POWER)
When I encounter this problem, I often take the approach of reducing the rating by one standard error. The formula for the standard error is:
standard deviation for the group / sqrt(group size)
If you had the standard deviation for each group, I would order them using:
order by (case when mentions > 1 then stdev / sqrt(mentions) end)
This is not as "punishing" as Evan Miller's suggestion (pointed to by Juergen). This is essentially taking a confidence interval more like 60% than 95%. Admittedly, my preference is a bit empirical (based on experience). However, there is an issue with multiple comparisons and you don't need to estimate the exact confidence interval -- you just need to know the relative ordering of them.
You can calculate the standard deviation using the function stdev().
Well, I'm not very good in statistic, but from your expected result, I believe you need to find the importance of each property.. Which one is more important than the other one, I think you can use equation below:
values = weight * RATING + (1-weight) * MENTIONS
You can play around with the weight value, till you got what you want.. For me 0.8 kind a make sense..
RATING MENTIONS NAME SCORE
4.4 14 Susan 6.32
2.5 15 Bob 5
5 2 Steve 4.4
1 60 John 2

MySQL beginner query ignorance. Changing the table id value so it places everything once again in numerical order

id name age
2 Sandy Smith 21
4 Alie Smith 19
5 James Smith 11
Hello, I am new to programming and am watching tutorials online this summer in my free time. I know this is a simple question but I've searched online and I can't find exactly what I need.
I deleted a row so now i no longer have "id 2 3 4 5". I'm left with 2, 4 ,5". I don't know the MySQL query to place alie smith as #3 and james smith as #4. Please help me out with this query. Thanks
If you still have the original insert statements or can re-create them, you can do a TRUNCATE TABLE [tablenamehere] then reinsert the data. Could be handy if you're trying to get the table back to a known state.
you don't need to. The point of a primary key is that is UNIQUE. ID field will auto increment on its own. The only way to do this would be to take the primary key attribute off of the ID column so that you can control it manually, insert the correct id's, and then put the key on. Read up on normalizing databases and it will make more sense.