MySQL Query : How to show all number and sum it - mysql

I have numeric data and I want to show all plus average value. How should I do to make it in MySQL?
Example : (2.5 is average value)
Data
------
1
2
3
4
2.5

You can do it without union
SELECT
AVG(value)
FROM a
GROUP BY id with rollup
Output
| VALUE |
|-------|
| 1 |
| 2 |
| 3 |
| 4 |
| 2.5 |
Fiddle Demo

Try this:
SELECT Data FROM table
UNION ALL
SELECT AVG(Data) AS Data FROM table

Related

Retrieving the most recent entry per user

If I have a table with the following structure and data:
id | user_id | created_at
-------------------------
1 | 7 | 0091942
2 | 3 | 0000014
3 | 6 | 0000890
4 | 6 | 0029249
5 | 7 | 0000049
6 | 3 | 0005440
7 | 9 | 0010108
What query would I use to get the following results (explanation to follow):
id | user_id | created_at
-------------------------
1 | 7 | 0091942
6 | 3 | 0005440
4 | 6 | 0029249
7 | 9 | 0010108
As you can see:
Only one row per user_id is returned.
The row with the highest created_at is the one returned.
Is there a way to accomplish this without using subqueries? Is there a name in relational algebra parlance that this procedure goes by?
The query is known as a groupwise maximum, which (in MySQL, at least) can be implemented with a subquery. For example:
SELECT my_table.* FROM my_table NATURAL JOIN (
SELECT user_id, MAX(created_at) created_at
FROM my_table
GROUP BY user_id
) t
See it on sqlfiddle.
You can just get the max and group by the user_id:
select id,user_id,max(created_at)
from supportContacts
group by user_id
order by id;
Here is what it outputs:
ID USER_ID MAX(CREATED_AT)
1 7 91942
2 3 5440
3 6 29249
7 9 10108
See the working demo here
Note that the example on the fiddle uses the created_at field as int, just use your format it should make no difference.
EDIT: I will leave this answer as a referece but note that his query will produce undesired results as Gordon stated, please do not use this in production.

Tricky sql query for popularity

ProductId | BrandId | Views
1 | 1 | 3
2 | 1 | 2
3 | 2 | 3
4 | 2 | 4
Need write sql query to return this values:
BrandId | ViewsSummary
1 | 5
2 | 7
Please, how to do it?
It's hardly "tricky" - you're simply looking to group your results with an appropriate aggregate function:
SELECT BrandId, SUM(Views) AS ViewsSummary FROM my_table GROUP BY BrandId
See it on sqlfiddle.

How to get the last updated record?

I have the following records like this in MySQL
RecID| LastModified
1 | 2011-10-29
1 | 2011-11-29
2 | 2011-5-29
3 | 2011-6-28
3 | 2011-8-25
I want the result like this:
RecID| LastModified
1 | 2011-11-29
2 | 2011-5-29
3 | 2011-8-25
How do I do this in MySQL?
Thanks
SELECT RecId, MAX(LastModified) FROM Table GROUP BY RecId

mysql select ordernumber by group

I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)

MySQL query to find duplicate having more than (n) entries

I am having troubles with creating up this sql query to find records having more than (n) entries [n=1] in example
I have table
|--id-|--user_id--|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
want to retrieve duplicates in my table
|--id-|--user_id--|
| 2 | 1 |
| 3 | 1 |
| 5 | 2 |
any help is very much appreciated, thanks for reading though
UPDATE:
I am using Mysql v5.1
This would be my approach
SELECT ID, USER_ID
FROM TABLE
GROUP USER_ID
HAVING COUNT(1) > 1
MINUS
SELECT MIN(ID) ID, USER_ID
FROM TABLE
GROUP BY USER_ID
EDIT: oops, didn't see that you're using MySQL. you might be able to tweak this query to get it working in MySQL
not sure which version of SQL your using but here's the sqlserver answer:
SELECT * from [table_name] GROUP BY user_id HAVING COUNT(*) > n