I am getting this error but I don't understand why my query doesn't work. Can someone please give me a hand?
The question is this:
SELECT activity
FROM Friends
GROUP BY activity
HAVING COUNT(activity) > MIN(COUNT(activity))
AND COUNT(activity) < MAX(COUNT(activity))
My idea is that as long as the count of the activity is larger than the activity that has the minimum count and less than the activity that has the maximum count, it should be returned. But I am having "Invalid use of group function" error which I don't understand. One possible thing that I could think of is that the parts that I am comparing with the COUNT(activity) have to be a number that is selected from the table instead of a part that has "MIN" or "MAX". But I don't understand why as they both look like the same number to me.
If your version of MySql is less than 8.0, the following will work:
select activity, count(*) as cnt from Friends group by activity
having cnt not in (
select max(cnt) as cnt from (
select activity, count(*) as cnt from Friends group by activity
) sq1
union
select min(cnt) as cnt from (
select activity, count(*) as cnt from Friends group by activity
) sq2
);
activity
cnt
Singing
2
View on DB Fiddle
Since the above SQL references the same select 3 times, namely select activity, count(*) as cnt from Friends group by activity, you might consider creating a quasi-temporary table (but there is the overhead in creating such a table to consider -- for the number of rows you actually presented, this would run more slowly):
create table t as select activity, count(*) as cnt from Friends group by activity;
select activity from t where cnt not in (
select max(cnt) as cnt from t
union
select min(cnt) as cnt from t
);
drop table t;
activity
Singing
View on DB Fiddle
If you just want data from friends table then you can use the analytical function (My sql 8.0 or higher) as follows:
select activity from
(SELECT activity, dense_rank() over (order by count(*)) as rn_asc,
dense_rank() over (order by count(*) desc) as rn_desc
FROM Friends
GROUP BY activity)
where rn_asc <> 1 and rn_dsc <> 1
Related
I'm trying to generate a query that will do the following:
I have an offer catalog table that has [Id, term, loanamount, interestrate], for every id there are multiple offers, i.e. every customer gets multiple offers. I'm trying to retrieve a dataset with the offer for each id that has [Id, max(term), max(loanamount)] and then the interest rate for the offer with the max(term) and max(loanamount) the way the I view it is the following:
SELECT a.Id as oppId,
a.MAX(Term) as maxTerm,
a.MAX(LoanAmount) as maxAmount,
b.InterestRate
FROM table a
JOIN ( SELECT InterestRate
FROM table
WHERE Id = oppId
AND Term = maxTerm
AND LoanAmount = maxAmount) b
GROUP BY Id
This doesn't seem to work so if anyone has a way to acomplish this it would be fantastic!
You can use row_number()
select * from
(
select *,row_number() over(partition by id order by Term desc, LoanAmount desc) as rn
from tablename
)A where rn=1
If I understand correctly, you can use row_number():
select t.*
from (select t.*,
row_number() over (partition by id order by term desc, loan_amount desc) as seqnum
from t
) t
where seqnum = 1
Which team has the most number of members on their roster?
Okay so below is the code that I have input currently. It returns all the teams as well as how many people are on each team. I am not sure how to code it to only display the team with the most members as when I try to use a max function and a count function I get an error.
SELECT Team_Name, COUNT(Member.Student_ID_Num)
FROM Teams
JOIN Member ON Teams.Team_Number = Member.Team_Number
GROUP BY Team_Name
you can try below - using limit and order by desc
Select Team_Name, count(Member.Student_ID_Num) as cnt
from Teams join Member on Teams.Team_Number = Member.Team_Number
group by Team_Name
order by cnt desc
limit 1
If you are using MySQL 8+, then the ROW_NUMBER function comes in handy here:
SELECT Team_Name, cnt
FROM
(
SELECT t.Team_Name, COUNT(*) AS cnt,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) rn
FROM Teams t
INNER JOIN Member m
ON t.Team_Number = m.Team_Number
GROUP BY t.Team_Name
) t
WHERE rn = 1;
If you instead want all ties for the highest count, should two or more teams be tied, then replace ROW_NUMBER with RANK.
If you have to do this the old fashioned way, without LIMIT or ROW_NUMBER, then get ready for a really ugly query:
SELECT
t.Team_Name,
COUNT(*) AS cnt
FROM Teams t
INNER JOIN Member m
ON t.Team_Number = m.Team_Number
GROUP BY t.Team_Name
HAVING COUNT(*) = (SELECT MAX(cnt) FROM (SELECT COUNT(*) AS cnt
FROM Teams t
INNER JOIN Member m
ON t.Team_Number = m.Team_Number
GROUP BY t.Team_Name) t );
In order to get the team with the highest member count, we need no join (unless we also want to show the member count). One thing to keep in mind that there may be multiple teams sharing the same maximum member count.
The old fashioned way in standard SQL, before there was any limit clause (FETCH FIRST ROWS in standard SQL) was this:
Count members per team number.
Get the maximum count.
Get the team number(s) with this maximum count.
Get the team(s) for these team numbers.
The query:
select *
from teams
where team_number in
(
select team_number
from member
group by team_number
having count(*) =
(
select max(cnt) as max_cnt
from
(
select count(*) as cnt
from member
group by team_number
) counted
)
);
As of MySQL 8 we would rather use a wnindow function, however:
select *
from teams
where (team_number, 1) in
(
select team_number, rank() over (order by count(*) desc)
from member
group by team_number
);
**Above is a picture of this particular table*
I need to write a query for a database that lists the name of a department for the department that controls the most projects.
In my database, departments are identified by dnums.
So my question is, how can I write something that checks for the greatest occurrence of a Dnum in SQL? Because that's how I will identify the department that controls the most projects.
I've tried several different queries, but none of them work properly.
Could anyone explain a method that could compare occurrences?
You know already how to count per department:
select dnum, count(*) from project group by dnum;
In SQL Server it is easy to select the dnum(s) with the maximum occurrences; you order by count descending and take the top row(s) using TOP() WITH TIES.
select top(1) with ties dnum from project group by dnum order by count(*) desc;
(In standard SQL that would be order by count(*) desc fetch 1 row with ties).
In standard SQL (and SQL Server) you also have the option of ranking your records per count:
select dnum
from (select dnum, rank() over (order by count(*) desc) as rnk from project) ranked
where rnk = 1;
MySQL doesn't give you any of these options, lacking both a WITH TIES clause and analytic functions such as RANK.
So in MySQL you would not find the departments with the maximum count in one step, but only the maximum count alone first. You would get the according department(s) only in a second step.
Two approaches here:
select count(*) from project group by dnum order by count(*) desc limit 1;
or
select max(cnt) from (select count(*) as cnt from project group by dnum) counted;
Then join the counted departments again:
select p.dnum
from
(
select count(*) as cnt
from project
group by dnum
order by count(*) desc limit 1
) m
(
select dnum, count(*) as cnt
from project
group by dnum
) p on p.cnt = m.cnt;
The last step is the same in both DBMS:
select dname
from departments
where dnumber in (select dnum ...);
(Or join the departments table instead, so you can show both name and count.)
You can use the COUNT function:
SELECT dnum, COUNT(*)
FROM project
GROUP BY dnum
ORDER BY COUNT(*)
If you need the department_name, you'll have to join to your department table (assuming you have one). It could look something like this:
SELECT d.dnum, d.name, COUNT(p.pnumber)
FROM department d
INNER JOIN projects p ON d.dnum = p.dnum
GROUP BY d.dnum, d.name
ORDER BY COUNT(p.pnumber)
I would like to select multiply conditions using below query:
SELECT (SELECT count(*)
FROM users
)
as totalusers,
(SELECT sum(cashedout)
FROM users
) AS cashedout,
(SELECT COUNT(*)
FROM xeon_users_rented
) AS totalbots,
(SELECT sum(value)
FROM xeon_stats_clicks
WHERE typ='3' OR typ='1'
) AS totalclicks
The above query takes just under a second (0.912 to be exact) to execute. This slows things down a lot with thousands of requests.
What seems logical for me is this approach:
SELECT (SELECT count(*), sum(cashedout)
FROM users
)
as totalusers, cashedout,
(SELECT COUNT(*)
FROM xeon_users_rented
) AS totalbots,
(SELECT sum(value)
FROM xeon_stats_clicks
WHERE typ='3' OR typ='1'
) AS totalclicks
However that doesn't work, as I get the following error:
#1241 - Operand should contain 1 column(s)
Furthermore, how can I join the two other tables "xeon_users_rented" and "xeon_stats_clicks" in my first query?
It's slow because you have multiple subqueries. Try using joins instead.
Also, a list of your tables, columns would help us better assist you.
Your 2nd query is using wrong syntax, it should be
SELECT
count(*) as totalusers,
sum(cashedout) cashedout,
(SELECT COUNT(*) FROM xeon_users_rented) AS totalbots,
(SELECT sum(value) FROM xeon_stats_clicks
WHERE typ='3' OR typ='1') AS totalclicks
FROM users
That is a picture of my table.
I must select "Fastanumer" of all cars where "Tegund" is the most common value (which is Toyota in this example)
This is the code i tried
SELECT Fastanumer FROM `Bill`
WHERE Tegund =
(SELECT MAX(y.cnt) FROM (SELECT COUNT(Tegund) AS cnt FROM Bill ) AS y)
Which i had to work pretty hard to figure out only to end up beating myself in the head over the fact that MAX will only turn into a number. (And since Tegund isn't a list of numbers...)
Is this even possible? How can i do this?
I guess it should work this way:
SELECT Fastanumer
FROM `Bill`
WHERE Tegund = (
SELECT Tegund
FROM (
SELECT Tegund,COUNT(*) FROM Bill GROUP BY Tegund ORDER BY COUNT(*) DESC LIMIT 1
) t1
)
Or even like this:
SELECT Fastanumer
FROM `Bill`
WHERE Tegund = (
SELECT Tegund FROM Bill GROUP BY Tegund ORDER BY COUNT(*) DESC LIMIT 1
)
Here's my solution:
SELECT Bill.*
FROM Bill
WHERE Tegund IN (
SELECT Tegund
FROM Bill
GROUP BY Tegund
HAVING COUNT(*) = (
SELECT MAX(cnt) FROM (
SELECT COUNT(*) cnt
FROM Bill
GROUP BY Tegund
) s
)
)
A little more complicated than others, but if more than one Tegund shares the same number of rows, this query will show all Tegunds which are the most common.
Please see fiddle here or here.
What you want to do first is figure out which Tegund appears the most in your table. That is what the subquery is doing. Then you will select the Fastanumer which matches that Tegund.
SELECT DISTINCT Fastanumer
FROM 'BILL'
WHERE Tegund = (
SELECT TOPT 1 Tegund,
COUNT(*) as Count
FROM `BILL`
GROUP BY Tegund
ORDER BY Count DESC)
Depends on the DB (and associated SQL). Try
select fastanumber from bill
inner join
(select count(*) as cnt, tegund from Bill group by tegund) grpby
on bill.tegund = grpby.tegund
and grpby.cnt = (select max(cnt) from (select count(*) as cnt, tegund from Bill group by tegund))