SELECT count(*) with ORDER BY - mysql

I want to ask you, if its correct if i use this ORDER BY in a SELECT COUNT(*)
Orginal: SELECT count(*) AS cnt FROM players WHERE totalpoints>?
Modified: SELECT count(*) AS cnt FROM players WHERE totalpoints>? ORDER BY timeontheserver DESC
In the Orginal query it outputs the players given "rank" in the database.
But i just noticed that some "players" are having the same amount of points and they get kinda reversed. So it should kinda give the player with an higher amount of "timeontheserver" the better "rank".
I hope you could understand this, thank you.

COUNT(*) will give you a number, you'll want to do a group by on the field you're ordering by to get what you want.
SELECT timeontheserver ,count(*) AS cnt FROM players
WHERE totalpoints>? GROUP BY timeontheserver ORDER BY timeontheserver DESC

Related

Alternative to max query?

Here is the database I'm using: https://drive.google.com/file/d/1ArJekOQpal0JFIr1h3NXYcFVngnCNUxg/view?usp=sharing
I'm trying to find out how I could return the research interest(descrip) with the largest number of interested academics(acnum) without the use of MAX query or ouputting descending order.
Each academic has a unique acnum, so I'm trying to link it with research interest(descrip) without having duplicate acnums.
I tried this:
SELECT x.descrip,
x.name_count
FROM (select u.descrip,
count(*) as name_count,
rank() over (order by count(*) desc) as rank
FROM interest u
WHERE u.descrip IS NOT NULL
GROUP BY u.descrip) x
WHERE x.rank = 1;
It partly works, but the acnums are duplicates, I want it to count distinct acnums.
Thank you

How to Insert Data into Tempdb Until Amount of Distinct Values Met

I have a main table named tblorder.
It contains CUID(Customer ID), CuName(Customer Name) and OrDate(Order Date) that I care about.
It is currently ordered by date in ascending order(ex. 2001 before 2002).
Objective:
Trying to retrieve most recent 1 Million DISTINCT Customer's CUID and CuNameS, and Insert them Into a Tempdb(#Recent1M) for Later Joining Uses.
So I:
Would Need Order By desc to flip the date to retrieve most recent 1 Million Customers
Only want first 1 Million DISTINCT Customer Information(CUID, CuName)
I know following code is not correct, but it is the main idea. I just can't figure out the correct syntax. So far I have the While Loop with Select Into as the most plausible solution.
SQL Platform: SSMS
Declare #DC integer
Set #DC = Count(distinct(CUID)) from #Recent1M))
While (#DC <1000000)
Begin
Select CuID,CuName into #Recent1MCus from tblorder
End
Thank you very much, I appreciate any help!
TOP 1000000 is the way to go, but you're going to need an ORDER BY clause or you will get arbitrary results. In your case, you mentioned that you wanted the most recent ones, so:
ORDER BY OrderDate DESC
Also, you might consider using GROUP BY rather than DISTINCT. I think it looks cleaner and keeps the select list a select list so you have the option to include whatever else you might want (as I took the liberty of doing). Notice that, because of the grouping, the ORDER BY now uses MAX(ordate) since customers can presumably have multiple ordate's and we are interested in the most recent. So:
select top 1000000 cuid, cuname, sum(order_value) as ca_ching, count(distinct(order_id)) as order_count
into #Recent1MCus
from tblorder
group by cuid, cuname
order by max(ordate) desc
I hope this helps.
Wouldn't you just do this?
select distinct top 1000000 cuid, cuname
into #Recent1MCus
from tblorder;
If the names might not be distinct, you can do:
select top 1000000 cuid, cuname
into #Recent1MCus
from (select o.*, row_number() over (partition by cuid order by ordate desc) as seqnum
from tblorder o
) o
where seqnum = 1;
Use DISTINCT and ORDER BY <colname> DESC to get latest unique records.
Try this SQL query:
SELECT DISTINCT top 1000000
cuid,
cuname
INTO #Recent1MCus
FROM tblorder
ORDER BY OrDate DESC;

MySQL Count the rows of which there are most

I have a businesses table. I want to get the owners name who has the most businesses. So far all I only know that I need to use GROUP BY and HAVING.
The problem is I only know the most basic queries...
Maybe something like this can help:
select owner, count(*) cntx
from businesses
group by owner
order by cntx desc
limit 1
Or executing the query without limit 1 clause, and then iterate the result till your needs are satisfied.
Use GROUP BY and order descending and then take the top one record which is the one that has most businesses:
select OwnerId, count(*) from businesses
group by OwnerId order by count(*) desc
limit 1

MySQL - Get row and average of rows

First of all I'll just warn everyone that I'm something of a rookie with MySQL. Additionally I haven't tested the example queries below so they might not be perfect.
Anyway, I have a table of items, each one with a name, a category and a score. Every 12 hours the top item is taken, used and then removed.
So far I've simply been grabbing the top item with
SELECT * FROM items_table ORDER BY score DESC LIMIT 1
The only issue with this is that some categories are biased and have generally higher scores. I'd like to solve this by sorting by the score divided by the average score instead of simply sorting by the score. Something like
ORDER BY score/(GREATEST(5,averageScore))
I'm now trying to work out the best way to find averageScore. I have another table for categories so obviously I could add an averageScore column to that and run a cronjob to keep them updated and retrieve them with something like
SELECT * FROM items_table, categories_table WHERE items_table.category = categories_table.category ORDER BY items_table.score/(GREATEST(5,categories_table.averageScore)) DESC LIMIT 1
but this feels messy. I know I can find all the averages using something like
SELECT AVG(score) FROM items_table GROUP BY category
What I'm wondering is if there's some way to retrieve the averages right in the one query.
Thanks,
YM
You can join the query that calculates the averages:
SELECT i.*
FROM items_table i JOIN (
SELECT category, AVG(score) AS averageScore
FROM items_table
GROUP BY category
) t USING (category)
ORDER BY i.score/GREATEST(5, t.averageScore) DESC
LIMIT 1

select ONLY repeated rows in a table

I need to find users that received bonus at my DB. The only users that interest to me, are those who got bonus more than one time.
How should I work this query to get ONLY users who got bonus more than once?
select Bonus, BonusUser, BonusType, Amount
from Bonus
where BonusType="1"
order by BonusUser asc;
I need a query that prompts all "duplicated" rows, so I can remove bonus from them.
I haven't explained before, but some users exploited a bug and could get free bonus, so I must select those duplicated rows, analyze and remove if it's abuse case.
you can do like below
select BonusUser,
count(*)
from Bonus
where BonusType="1"
group by BonusUser
having count(*)>1
order by BonusUser asc
you must provide some dummy data with expected result ..
Add a GROUP BY and a HAVING clause
SELECT Bonus
, BonusUser
, BonusType
, Ammount
FROM Bonus
WHERE BonusType="1"
GROUP BY BonusUser
HAVING Count(*) > 1
ORDER BY BonusUser asc;
Based on your comment, I think this is what you want, this will give you the list of all users with a bonus but it will give you the count of those who had more than one bonus:
SELECT Bonus
, t.BonusUser
, BonusType
, amount
, t2.cntbonus
FROM Bonus t
inner join
(
select count(*) as CntBonus, bonususer
from Bonus
where BonusType='1'
group by bonususer
) t2
on t.bonususer = t2.bonususer
WHERE BonusType='1'
ORDER BY BonusUser asc
As an alternative to the other answers, you can also do the following:
SELECT DISTINCT b1.*
FROM Bonus b1
JOIN Bonus b2
ON b1.BonusUser = b2.BonusUser
AND b1.Id > b2.Id
WHERE b1.BonusType = "1"
AND b2.BonusType = "1"
ORDER BY b1.BonusUser ASC;