Mysql select on a table - mysql

I have a mysql table with following columns:
Id1, id2, timestamp
The id2 is an auto increment entry. The id2 is not unique. so you may have a following rows:
1,12, 983475
2,12, 092348
3,23, 987455
4,23, 908457
I need to get following rows where the timestamp is the latest on the id2.
ie the results will be:
1,12,983475
3,23,987455
Also, the numbers 987455 and 983475 are just fictitious...
Help please...

You can use a subquery with the max() aggregate for this:
select y.id1, y.id2, y.timestamp
from yourtable y
join (select id2, max(timestamp) maxtimestamp
from yourtable
group by id2
) y2 on y.id2 = y2.id2 and y.timestamp = y2.maxtimestamp
This could possibly return ties -- if you truly want a single row per distinct id2, then you can use user-defined variables to establish a row number.

SELECT id2 FROM table ORDER BY max(timestamp) DESC LIMIT 0, 1

Select and restrict using max(timestamp)
e.g. select * from table where timestamp=(select max(timestamp) from table

Related

My sql select that has multiple row in the same criteria

I have mysql table like this
I want to get row that has minimum 2 or more than 2 (multiple) row only from this table, so the result would be like this
What do i do?
thank you
Use GROUP BY and HAVING clauses
SELECT t.* FROM my_table t
JOIN (
SELECT cust_id, MIN(transaction_no) AS transaction_no
FROM my_table
GROUP BY cust_id
HAVING COUNT(cust_id) > 1
) agg ON t.transaction_no = agg.transaction_no

GROUP_CONCAT of SUMs

I have two select-queries which both give correct results:
SELECT SUM(value) AS "sum1" FROM table GROUP BY id1
and
SELECT SUM(value) AS "sum2" FROM table GROUP BY id1, id2
sum1 is the sum of all sum2-items and I want a query that gives me a result of sum1 and a GROUP_CONCAT of all sum2-items. But how can I define the GROUP BY inside the GROUP_CONCAT for the sum2-items element?
SELECT SUM(value) AS "sum1", GROUP_CONACAT(SUM(value) AS "sum2" ... (?))
FROM table GROUP BY id1
You should solve it using sub query, use the following query, i believe it will solve your problem
select sum(t.part_sum) as sum1, group_concat(t.part_sum) as sum2 from (select sum(value) as part_sum from table group by id1, id2) as t

MySQL select most occurring or average

I have a MySQL table from which I want to select:
1) Either "most occurring" value, if there is any prevailing
2) Or "average" value, if there is no most occurring value.
Example table 1:
value
1
2
3
4
All values are occurred equally, therefore I want to take AVG(`value`)
Example table 2:
value
1
2
2
3
Value 2 prevails, therefore I want to select the value 2.
What mysql query would do this?
Starting from Gordon's answer I tested and corrected the SQL query in SQL Fiddle:
SELECT IF(t4.numcnts = 1, t1.avgvalue, t2.topvalue) AS result
FROM (select avg(value) as avgvalue from test) t1
CROSS JOIN (select value as topvalue from test group by value order by count(*) desc limit 1) t2
CROSS JOIN join (select count(distinct cnt) as numcnts from
(select count(*) as cnt from test group by value) t3) t4
Here is the Fiddle with the two test tables (switch out test2 for test to see the result when a particular value prevails): http://sqlfiddle.com/#!2/76914/3
My changes were to use an IF instead of a CASEstatement in the SELECTclause and to add the necessary table aliases for the subselects.
The following approach calculates both values and then chooses between them:
select (case when numcnts = 1 then avgvalue else topvalue end)
from (select avg(value) as avgvalue from t) cross join
(select value as topvalue from t group by value order by count(*) desc limit 1) cross join
(select count(distinct cnt) as numcnts from (select count(*) as cnt from t group by value))
Note: if you have ties for the top, but other values as well, then an arbitrary value is returned. You don't specify what to do in this case.
Also, the SQL is untested, so it might have syntax errors.

MySQL: check that a set of queries returns the same row count : : but I don't know what the count is

We read values from a set of sensors, occasionally a reading or two is lost for a particular sensor , so now and again I run a query to see if all sensors have the same record count.
GROUP BY sensor_id HAVING COUNT(*) != xxx;
So I run a query once to visually get a value of xxx and then run it again to see if any vary.
But is there any clever way of doing this automatically in a single query?
You could do:
HAVING COUNT(*) != (SELECT MAX(count) FROM (
SELECT COUNT(*) AS count FROM my_table GROUP BY sensor_id
) t)
Or else group again by the count in each group (and ignore the first result):
SELECT count, GROUP_CONCAT(sensor_id) AS sensors
FROM (
SELECT sensor_id, COUNT(*) AS count FROM my_table GROUP BY sensor_id
) t
GROUP BY count
ORDER BY count DESC
LIMIT 1, 18446744073709551615
SELECT sensor_id,COUNT(*) AS count
FROM table
GROUP BY sensor_id
ORDER BY count
Will show a list of the sensor_id along with a count of all the records it has, you can then manually check to see if any vary.
SELECT * FROM (
SELECT sensor_id,COUNT(*) AS count
FROM table
GROUP BY sensor_id
) AS t1
GROUP BY count
Will show all the counts that vary, but the group by will lose information about which sensor_ids have which counts.
---EDIT---
Taken a bit from both mine and eggyal's answer and created this, for the count that is most frequent I call the id default, and then for any values that stand out I have given them separate rows. This way you maintain the readability of a table if you have many results Multi Row, but also have a simple one row column if all counts are the same One Row. If however you are happy with the concocted strings then go with eggyal's answer.
Might be a bit over the top but here goes:
select 'default' as id,t5.c1 as count from(
select id,count(*) as c1 from your_table group by id having count(*)=
(select t4.count from
(
select max(t3.count2) as max,t3.count as count from
(
select count(*) as count2,t2.count from
(
SELECT id,COUNT(*) AS count
FROM your_table
GROUP BY id
) as t2
GROUP BY count
) as t3
) as t4)) as t5 group by count
union all
select t5.id as id,t5.c1 as count from(
select id,count(*) as c1 from your_table group by id having count(*)<>
(select t4.count from
(
select max(t3.count2) as max,t3.count as count from
(
select count(*) as count2,t2.count from
(
SELECT id,COUNT(*) AS count
FROM your_table
GROUP BY id
) as t2
GROUP BY count
) as t3
) as t4)) as t5

MySql Sql MAX and SUM error

select sum(value) as 'Value',max(value)
from table_name where sum(value)=max(sum(value)) group by id_name;
The error is: Invalid use of group function (ErrorNr. 1111)
Any idea?
Thanks.
Can you maybe try
SELECT Value, MXValue
FROM (
select sum(value) as 'Value',max(value) MXValue
from table_name
group by id_name
) as t1
order by value desc
LIMIT 0,1
From MySQL Forums :: General :: selecting MAX(SUM())
Or you could try something like
SELECT id_name,
Value
FROM (
select id_name,sum(value) as 'Value'
from table_name
group by id_name
) t
WHERE Value = (
SELECT TOP 1 SUM(Value) Mx
FROM table_name
GROUP BY id_name
ORDER BY SUM(Value) DESC
)
Or even with an Inner join
SELECT id_name,
Value
FROM (
select id_name,sum(value) as Value
from table_name
group by id_name
) t INNER JOIN
(
SELECT TOP 1 SUM(Value) Mx
FROM table_name
GROUP BY id_name
ORDER BY SUM(Value) DESC
) m ON Value = Mx
The =max(sum(value)) part requires comparing the results of two grouped selects, not just one. (The max of the sum.)
Let's step back, though: What information are you actually trying to get? Because the sum of the values in the table is unique; there is no minimum or maximum (or, depending on your viewpoint, there is -- the value is its own minimum and maximum). You'd need to apply some further criteria in there for the results to be meaningful, and in doing so you'd probably need to be doing a join or a subselect with some criteria.