This code works great for getting both a summary of individual Group counts and a Grand Total combined, but I only want the Grand Total of individual countries with no ROLLUP:
SELECT country, count( * )
FROM mytable
GROUP BY country
WITH ROLLUP
LIMIT 0 , 300
Researching a lot of examples, I was hoping this one would finally work but no joy (can't figure out what the MySQL error is in the code):
SELECT country,COUNT(*)
FROM mytable
GROUP BY country
COMPUTE Sum(Count(*))
Thanks for any assistance!
-stucko
If you want to get the number of rows returned by a GROUP BY you can do something like this
SELECT COUNT(*) FROM (
SELECT country, count( * )
FROM mytable
GROUP BY country
) a /*derived tables need an alias*/
Related
I'm getting an error when trying to group by a sum value.
select sum(price + adjustments) as adjusted_price from items group by adjusted_price
Is this not possible?
Example: I want to grab items that have the same adjusted price. Example ( $1.00 - .50 = .50) --- ( $2.00 - 1.50 = .50), I want to grab items that have .50 grouped together.
Thanks
Maybe you are looking for the grand total adjusted_price? If so you don't need the groupby
select
sum(adjusted_price)
from
(select price+adjustments as adjusted_price from items) tt
Edit:
Sounds like you want to group by the adjusted_price. Try the following which will return the frequency count as well:
select
adjusted_price, count(1)
from
(select price+adjustments as adjusted_price from items) tt
group by adjusted_price
Or the following should get you only the unique adjusted_price values:
select distinct
adjusted_price
from
(select price+adjustments as adjusted_price from items) tt
Is this not possible?
Forget about possibility, moreover that looks wrong and incorrect. Why you are trying to group by the SUM() result BTW? You should rather group by any other column in your table. something like below but can't say for sure unless see your table schema and sample data.
select sum(price) + sum(adjustments) as adjusted_price
from items
group by some_column_name;
I have come across a task, I managed to complete the objective but the solution I got is not optimum, I need more optimum solution. I have used normal Sub Queries May be Correlated Sub Query can solve this better.
This is the table i made
SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid;
The output of this is like:-
What I want is the custid having maximum Total.
One way to do it is using Order by Total DESC LIMIT 1 but this will give only 1 result.
What I did is
SELECT custid
FROM (SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid) c1
WHERE total = (SELECT max(Total)
FROM (SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid) c2)
This gives me correct result that is
What I want to do is reduce the code, because here I am writing the same thing again. I know there must be a simpler way to do it. Maybe a correlated query.
Looking for some good answers. This is basically to clear my concepts only
Sorry, if it is noob question. I am a noob to SQL.
After understand what OP want with #Ravinder 's tip,
I guess build in mysql function GROUP_CONCAT is what you need, sql is:
select custid_count.Total, GROUP_CONCAT(custid_count.custid order by custid_count.custid asc SEPARATOR ',') as custids from
(select custid, count(distinct bid) as Total from loan group by custid order by Total desc) as custid_count
group by custid_count.Total
order by custid_count.Total desc
limit 1;
the result column custids is the max ids concated by ',' ,after the query, you need to split custids by ',' and convert each substring to number type you need,
Here is another way:
select * from loan
where custid =
(
select custid_count.custid from
(select custid, count(distinct bid) as Total from loan group by custid order by Total desc) as custid_count
order by custid_count.Total desc
limit 1
);
First find the custid with max count, then query all rows which match the custid,
I haven't tried this in mysql, but in the sql language I'm using it is fine to use a aggregation function without a group by so something like this
select custid, total, max(total) as maxtotal
from (select custid, count(distinct bid) as total
from loan
group by custid) c1;
would tag on every line both the individual customer total and the table wide max total, and you'd just have to filter on the ones that where the total was equal to the max total. That would give you a final answer of something like this:
select custid
from (select custid, count(distinct bid) as total
from loan
group by custid) c1
where total = max(total);
I have two statements within my table which work fine individually like this:
SELECT fee_earner, (SUM(fe_fees)) AS Total
FROM fees
GROUP BY fee_earner
order by Total desc;
SELECT supervisor, (SUM(sv_fees)) AS Total
FROM fees
GROUP BY supervisor
order by Total desc;
But there are some cases where the fee_earner and supervisor fields have the same person as the data, is there a way to combine these two statements into one to get the overall totals?
You can use union all for this:
SELECT person, sum(fe_fees) as fe_fees, sum(sv_fees) as sv_fees,
(sum(fe_fees) + sum(sv_fees)) as total
FROM ((select fee_earner as person, fe_fees as fe_fees, 0 as sv_fees, 'earner' as which
from fees
) union all
(select supervisor as person, 0 as fe_fees, sv_fees as sv_fees, 'supervisor' as which
from fees
)
) t
GROUP BY person
order by Total desc;
select
fee_earner, SUM(fe_fees) as total, SUM(sv_fees) as total2,
SUM(fe_fees) + SUM(sv_fees) as wholeTotal
from
fees
group by
fee_earner, supervisor
order by
Total desc;
I'm working on simple queries to learn MySQL, in my example database, I keep track of Stores which sells electronic devices, I have a table Sells(Store, Item, Price).
And example data is,
'Best Buy', 'Galaxy S', 1000
'Buy More', 'Macbook Air', 2000
'Best Buy', 'Microsoft Mouse', 20
'Best Buy', 'Macbook Pro Cover', 40
'Buy More', 'Asus Zenbook', 2000
And so on..
I tried the following sql statement, but it says:
Error Code: 1111. Invalid use of group function 0.000 sec
SELECT store
FROM sells
WHERE SUM(price) <
(SELECT SUM(price) AS total
FROM sells
GROUP BY store
ORDER BY total DESC
LIMIT 1)
GROUP BY store
ORDER BY SUM(price) DESC
I would be appreciate if you can help me
Thanks
This will just plain show the second most expensive store;
SELECT STORE
FROM TABLE_A
GROUP BY STORE
ORDER BY SUM(PRICE) DESC
LIMIT 1,1
Demo here.
If you want the price displayed too, you can just select that too;
SELECT STORE, SUM(PRICE) TOTAL_PRICE
FROM TABLE_A
GROUP BY STORE
ORDER BY TOTAL_PRICE DESC
LIMIT 1,1
Demo here.
Edit: If you have several most expensive stores and several second most expensive stores, the query to get the all the second most expensive ones becomes quite a bit more convoluted; I'm sure someone can beat the efficiency of this one;
SELECT STORE, SUM(PRICE) TOTAL_PRICE
FROM TABLE_A
GROUP BY STORE
HAVING TOTAL_PRICE =
(SELECT SUM(PRICE) TMP
FROM TABLE_A
GROUP BY STORE
HAVING TMP <
(SELECT SUM(PRICE) TMP2
FROM TABLE_A
GROUP BY STORE
ORDER BY TMP2 DESC
LIMIT 1)
ORDER BY TMP DESC LIMIT 1)
Demo here.
You can do like this;
SELECT *,
SUM(price) AS totalprice
FROM sells
GROUP BY store
ORDER BY totalprice DESC
LIMIT 2
You first select the sum of the prices and store it temporarily in for ex. totalprice then as you already did group by store. To get the most expensive stores order the sum backwards and then limit to just two results.
You will be able to get the totalprice just as an ordinary column when you loop out the results
almost correct,
SELECT SUM(price) as price_total FROM sells GROUP BY store
if you want to order by you can do subquery, like:
SELECT price_total FROM (SELECT SUM(price) as price_total FROM sells GROUP BY store) as res ORDER BY price LIMIT 2
if you want to take 2nd you might make another query but i think it is better to use your back-end language
SELECT distinct price from sells ORDER BY price DESC, and in your code, just take the second one.
If you need the rest of the info, do this:
SELECT * from sells
WHERE price = (SELECT distinct price from sells ORDER BY price DESC LIMIT 1,1)
didn'T test it but should work
SELECT S.store
FROM (
SELECT SUM(T.price) AS sum_price
FROM formList_Total AS T
GROUP BY T.store
) AS S
ORDER BY sum_price DESC
LIMIT 1 , 1
Sorry, went to testing, here what i ended up with.
I need some help with one of the questions from my homework, Ive been trying for about an hour now and cant get it to run.
List the customers that bought more items than the average number of items per customer
The tables are as follows:
Customer(Cnum, CustomerName, Address)
Item(Inum, ItemName, Manufacturer, Year)
Bought(Cnum, Inum, Date, Quantity)
Prefer(Inum, Cnum)
The best i could figure out was that it needs to be the total Quantity per Customer compared to the overall average of the Quantity. I've tried various forms of this query:
SELECT Cnum
FROM Bought
WHERE
(
SELECT Cnum, SUM(Quantity)
FROM Bought
GROUP BY Cnum;
) >
(
SELECT AVG(Quantity)
FROM Bought
);
But it returns an error -- (phpMyAdmin isnt telling me what the problem is, just failing to execute and going to no connection page, which means error in my query)
I have also tried to return the higher SUM with:
SELECT SUM(Quantity)
FROM Bought
WHERE SUM(Quantity) > AVG(Quantity);
And same issue.
Any help would be appreciated, even an explanation as to why the second one fails.
You might want to take a look at the HAVING clause of SQL.
Note: I'm intentionally not giving you the full answer since this is homework.
why Don't you try this.
SELECT `Cnum` , Sum( Quantity )
FROM `bought`
GROUP BY `Cnum`
HAVING Sum( Quantity ) > ( SELECT AVG( Quantity ) FROM Bought )
Try below:
SELECT Cnum
FROM Bought
having SUM(Quantity) > (SELECT avg(Quantity) FROM Bought)
order by SUM(Quantity) desc
Try this maybe it helps
SELECT Cnum, SUM(Quantity)
FROM Bought
GROUP BY Cnum
HAVING SUM(OrderQuantity) > avg(Quantity)
Try this
SELECT Cnum, Inum, SUM(Quantity) sum, AVG(QUANTITY) average
from bought group by cnum,inum having sum > average