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;
Related
I have a mysql query where I am trying to fetch DISTINCT elements:
SELECT DISTINCT(`Prev Date`), `Current Date`
FROM `electric_transaction`
WHERE `Flag`=1
AND `ipas_flag`=1
AND `SlNo` != (
SELECT MAX(`SlNo`) FROM `electric_transaction`
);
I want to fetch all the rows except the last one.
How do I proceed? Please guide me.
You could use an offset of 1 record in your LIMIT clause
SELECT DISTINCT `Prev Date`,`Current Date`
FROM `electric_transaction`
WHERE `Flag` = 1
AND `ipas_flag` = 1
ORDER BY SlNo DESC
LIMIT 1, 1000000
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
Nobody can answer this question?
$result=mysql_query("
SELECT COUNT(*) AS `total` FROM `mytable`
WHERE `myvariable`='1'
ORDER BY `id` DESC
LIMIT 15;"
);
$data=mysql_fetch_array($result);
$count = $data['total'];
echo $count;
This count ALL result from mytable, but how I can do to count last 15 results only? It seems LIMIT 15 not work in this case?
I think this is the query you want:
SELECT SUM(myvariable = '1') AS total
FROM (SELECT myvariable
FROM mytable
ORDER BY id DESC
LIMIT 15) AS subquery
This only looks at the most recent 15 rows, and counts the number of them that have myvariable = 1.
Since you want last 15 after descending order, Order by ascending and select first 15 and do descending order sort
select * from (SELECT * FROM mytable WHERE myvariable='1' ORDER BY id ASC LIMIT 15) ORDER BY id DESC
I have a table in this structure:
editor_id
rev_user
rev_year
rev_month
rev_page
edit_count
here is the sqlFiddle: http://sqlfiddle.com/#!2/8cbb1/1
I need to surface the 5 most active editors during March 2011 for example - i.e. for each rev_user - sum all of the edit_count for each rev_month and rev_year to all of the rev_pages.
Any suggestions how to do it?
UPDATE -
updated fiddle with demo data
You should be able to do it like this:
Select the total using SUM and GROUP BY, filtering by rev_year and rev_month
Order by the SUM in descending order
Limit the results to the top five items
Here is how:
SELECT * FROM (
SELECT rev_user, SUM(edit_count) AS total_edits
FROM edit_count_user_date
rev_year='2006' AND rev_month='09'
GROUP BY rev_user
) x
ORDER BY total_edits DESC
LIMIT 5
Demo on sqlfiddle.
Surely this is as straightforward as :
SELECT rev_user, SUM(edit_count) as TotalEdits
FROM edit_count_user_date
WHERE rev_month = 'March' and rev_year = '2014'
GROUP BY rev_user
ORDER BY TotalEdits DESC
LIMIT 5;
SqlFiddle here
May I also suggest using a more appropriate DATE type for the year and month storage?
Edit, re new Info
The below will return all edits for the given month for the 'highest' MonthTotal editor, and then re-group the totals by the rev_page.
SELECT e.rev_user, e.rev_page, SUM(e.edit_count) as TotalEdits
FROM edit_count_user_date e
INNER JOIN
(
SELECT rev_user, rev_year, rev_month, SUM(edit_count) AS MonthTotal
FROM edit_count_user_date
WHERE rev_month = '09' and rev_year = '2010'
GROUP BY rev_user, rev_year, rev_month
ORDER BY MonthTotal DESC
LIMIT 1
) as x
ON e.rev_user = x.rev_user AND e.rev_month = x.rev_month AND e.rev_year = x.rev_year
GROUP BY e.rev_user, e.rev_page;
SqlFiddle here - I've adjusted the data to make it more interesting.
However, if you need to do this across several months at a time, it will be more difficult given MySql's lack of partition by / analytical windowing functions.
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