I have this query that works fine but I need just the top 10 vendors.
Then I need all the remaining totaled in a "All Others" row.
How can I do this wihtout a seperate query with LIMIT 10, 18446744073709551615
SELECT VENDOR_fullname,SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
GROUP BY VENDOR_fullname
ORDER BY SUM(POTENTIAL_RECOVERY) DESC;
For the record, I agree with #DamienBlack's comment, however if it had to be done in one query a UNION ALL could do the trick:
(
SELECT VENDOR_fullname as name, SUM(POTENTIAL_RECOVERY) as recovery
FROM COMPLETE
GROUP BY VENDOR_fullname
ORDER BY SUM(POTENTIAL_RECOVERY) DESC
LIMIT 10
)
UNION ALL
(
SELECT 'All others' as name, SUM(subtotal) as recovery
FROM
(
SELECT SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
GROUP BY VENDOR_fullname
ORDER BY SUM(POTENTIAL_RECOVERY) DESC
LIMIT 10, 18446744073709551615
) as subtotal;
)
You can use union :
SELECT VENDOR_fullname as anme,SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
GROUP BY VENDOR_fullname
ORDER BY SUM(POTENTIAL_RECOVERY) DESC
LIMIT 10
UNION ALL
SELECT 'All Others' as name,SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
ORDER BY SUM(POTENTIAL_RECOVERY) DESC
LIMIT 10, 18446744073709551615
Related
I have the following query.
SELECT COUNT(id) AS NumResults, race_date AS RaceDate, race_time AS RaceTime
FROM results
WHere jockeys_claim = 10
GROUP BY race_date DESC, race_time ASC
I would then like to filter out the NumResults , so the table only shows results where the NumResults = 1. Thanks for looking.
Use the HAVING clause to filter the results of an aggregate function (the COUNT in your case)
SELECT COUNT(id) AS NumResults, race_date AS RaceDate, race_time AS RaceTime
FROM results
WHere jockeys_claim = 10
GROUP BY race_date, race_time
HAVING COUNT(id) = 1
ORDER BY race_date DESC, race_time ASC
SELECT * FROM (
SELECT * FROM cars WHERE site = '5'
ORDER BY cost DESC LIMIT 0 , 10
)
ORDER BY time
How would I execute a sql query like this? So first it selects the 10 cars with the highest cost, THEN it reorders those 10 cars by what time they were added to the DB.
I tried to figure it out but I just cannot get a grip on the syntax :P
Just give an alias to the sub-query.
SELECT * FROM (
SELECT * FROM `cars` WHERE `site` = '5'
ORDER BY `cost` DESC LIMIT 0 , 10
)t
ORDER BY `time`;
This query will give you the desired results
SELECT * FROM ( SELECT * FROM cars WHERE site = 5
ORDER BY cost DESC LIMIT 0 , 10 ) as t ORDER BY time
I would like to get values without the smallest and the biggest ones, so without entry with 2 and 29 in column NumberOfRepeating.
My query is:
SELECT Note, COUNT(*) as 'NumberOfRepeating'
WHERE COUNT(*) <> MAX(COUNT(*))AND COUNT(*) <> MIN(COUNT(*))
FROM Note GROUP BY Note;
SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes
GROUP BY Note
HAVING count(*) <
(
SELECT max(t.maxi)
FROM (select
Note, COUNT(Note) maxi FROM Notes
GROUP BY Note
) as t
)
AND
count(*) >
(
SELECT min(t.min)
FROM (select
Note, COUNT(Note) min FROM Notes
GROUP BY Note
) as t
)
try this code.
One method would use order by and limit, twice:
select t.*
from (select t.*
from t
order by NumberOfRepeating asc
limit 99999999 offset 1
) t
order by NumberOfRepeating desc
limit 99999999 offset 1;
Try this code,
Select * from Note where NumberOfRepeating < (select MAX(NumberOfRepeating) from Note ) AND NumberOfRepeating > (select MIN(NumberOfRepeating) from Note );
Here in the code, as in your table Note is the name of the table, and NumberOfRepeating is the column name, as in your table.
Try this. It should work
SELECT *
FROM ( SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes
GROUP BY Note
ORDER BY NumberOfRepeating DESC
LIMIT 1, 2147483647
) T1
ORDER BY T1.NumberOfRepeating
LIMIT 1, 2147483647
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='7' ORDER BY `counter` DESC LIMIT 75
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='4' ORDER BY `counter` DESC LIMIT 75
I want to see 2 avg easy to compare in my phpmyadmin. Singe line above will work but i can't combine them :/
Use conditional aggregation if you want the results in two columns:
SELECT AVG(CASE WHEN id = 7 THEN tmp END) as avg_7,
AVG(CASE WHEN id = 4 THEN tmp END) as avg_4
FROM monitor
WHERE id IN (4, 7);
The ORDER BY and LIMIT` are non-sensical in your question because the queries return only one row.
If you prefer two rows, then you have several choices. I think I would go with aggregation:
SELECT id, AVG(tmp) as average
FROM monitor
WHERE id IN (4, 7)
GROUP BY id;
Try:
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='7' ORDER BY `counter` DESC LIMIT 75
UNION
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='4' ORDER BY `counter` DESC LIMIT 75;
I'm using an union statement in mysql but i've some problems sorting the results. The ORDER statement doesn't works at all, the results comes out always sorted by the id field.
Here an example query:
SELECT a.* FROM ( ( select * from ticket_ticket AS t1 WHERE ticket_active=1 ORDER BY t1.ticket_date_last_modified DESC )
UNION ( select * from ticket_ticket AS t2 WHERE ticket_active=0 ORDER BY t2.ticket_date_last_modified DESC, t2.ticket_status_id DESC ) )
AS a LIMIT 0,20;
I want to order the results of the first SELECT by last_modified time, and the second SELECT by time and status. But the ORDER statement get just skipped. The results always come out ordered by the ticket_id ( the PRIMARY KEY ).
What's wrong in this query ?
Thanks!
Ok, i've fixed it writing the query this way:
SELECT a.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=1
ORDER BY ticket_date_last_modified DESC) AS a
UNION ALL
SELECT b.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=0
ORDER BY ticket_date_last_modified DESC, ticket_status_id DESC) AS b LIMIT 0,
20;
You are using a UNION query that will return distinct values, and the order of the returned rows is not guaranteed.
But you don't need an union query for this:
select *
from ticket_ticket AS t1
ORDER BY
ticket_active!=1,
ticket_date_last_modified DESC,
ticket_status_id DESC
LIMIT 0,20;