MySQL - Average of Unique rows - php

If I have three columns:
id, user, points
My data is:
+-------+------------------+-------------+
| id | user | points |
+-------+------------------+-------------+
| 1 | A | 100 |
+-------+------------------+-------------+
| 1 | A | 200 |
+-------+------------------+-------------+
| 2 | B | 300 |
+-------+------------------+-------------+
| 2 | B | 400 |
+-------+------------------+-------------+
I would like to have the average of ONLY the max points of each user.
For this exmple I want to get as results: 300 points ((200+400)/2).
When I use the following Mysql query, I get: 250:
SELECT avg(points) FROM table

SQL DEMO
Try this :
SELECT avg(points) FROM (
SELECT max(points) as points FROM table1 group by id
) as T
Firstly get the max points of each user and then get the AVG from them.

You should first get the max group by use and the the avg of this subquery
SELECT AVG(points)
FROM (SELECT MAX(points) FROM your_table GROUP BY user) subt

Related

Count integer value and order by user

So my table looks like this:
| id | user | points |
| 1 | Sam | 1 |
| 2 | Sam | 6 |
| 3 | Phil | 1 |
The query I am currently using is:
SELECT user,COUNT(*) FROM table GROUP BY user order by COUNT(*) DESC
This returns the current value:
Sam: 2
Phil: 1
It looks like it counts the number of rows, not the total points? How can I do this?
The correct return should be Sam: 7.
Use SUM instead of COUNT
SELECT user, SUM(points) FROM table GROUP BY user

Mysql select UNIQUE row on column

I have a table as following:(Ex)
id | vid | time
------------------------
1 | 4 | 1333635317
2 | 4 | 1333635323
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
I want to be just a row (the last row [ID: 4]) of the same rows[id:1,2,4], how it will output the query?
I mean, as a result of these:
id | vid | time
------------------------
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
What do i do?
i trying it as:
SELECT * from tbale as t1 where vid = 4 GROUP BY vid ORDER BY id DESC
but doesn't work ORDER BY in my query.
Get the max time per vid and use in to get those rows from the table.
select * from tablename
where (vid,time) in (select vid,max(time)
from tablename
group by vid)
order by id

sum() by group sql

I have data in a row with same value and id example below:
table_name: test
id | amount
1 | 100
1 | 100
1 | 100
2 | 150
2 | 150
2 | 150
3 | 200
3 | 200
3 | 200
4 | 250
4 | 250
4 | 250
I want to sum only one row in each idusing sql below is not working it sum all the row.
"select *, sum(amount) as total from test group by id";
my question is that possible to sum() only one row each id?
desired output?(edit)
id | amount
1 | 100
2 | 150
3 | 200
4 | 250
total : 700
my question is that possible to sum() only one row each id?
I interpret this as your wanting one value, with a single row from each group. One method is two levels of aggregation:
select sum(amount)
from (select id, max(amount) as amount
from test
group by id
) t;
Looks like you need this
select *,sum(amount) from (select distinct * from test) as t group by id
Try:
select id, sum(amount)/count(*) as total
from test
group by id
Result:
| id | total |
|----|-------|
| 1 | 100 |
| 2 | 150 |
| 3 | 200 |
| 4 | 250 |
try this using subquery-
select sum(amount) from (select distinct id,amount from company7) as tempTable
Try this
create table #test
(id int, amount int)
insert into #test values (1,100),(1,100),(1,100),(2,150),(2,150),(3,200),(3,250)
;with cte
as
(
select sum(distinct amount) as damount,id
from #test
group by id
)
select sum(damount) as total from cte
For other DBMSs than SQL Server
select sum(damount) as total from (select sum(distinct amount) as damount,id
from test
group by id) as it

How to get unique result from below entry?

plan_id | elementclass | table_no | ress_id | UserID | Status
1 | elementclass1 | 1 | 0 | 0006100022 | N
1 | elementclass1 | 1 | 2 | 0006100022 | N
1 | elementclass2 | 2 | 0 | 0006100021 | N
1 | elementclass4 | 3 | 0 | 0006100023 | N
in above row I am expecting as this
if row is having same elementclass,table_no but different ress_id in that condition only take that row which is non zero.If with above condition tow rows having 0 it can take any row .if both rows have non zero then also it can take any one.
Now
for rest of others it can take values with 0.We can use group by to plan_id as there may be multiple plans.
Desired result
plan_id | elementclass | table_no | ress_id | UserID | Status
1 | elementclass1 | 1 | 2 | 0006100022 | N
1 | elementclass2 | 2 | 0 | 0006100021 | N
1 | elementclass4 | 3 | 0 | 0006100023 | N
Please help.
thanks
SELECT * FROM TableName a
WHERE a.ress_id = (SELECT MAX(b.ress_id) FROM TableName b WHERE b.table_no = a.table_no)
GROUP BY a.plan_id,a.table_no
This gives you:
1 result per plai_id and table_no
each result has biggest ress_id in it
First get the maximum ress id per element class. Then select the related records. There may be duplicates. Hence group by element class and ress id.
The following statement does not precisely do what you asked for, but maybe it suffices. In case of a tie you won't get one of the records, but one of the records' plan ids, one of the records' table nos, one of the records' user ids and one of the records' statusses. So the user id may be taken from one record and the status from another when elementclass and ress_id are equal.
select plan_id, mytable.elementclass, table_no, mytable.ress_id, userid, status
from mytable
join
(
select elementclass, max(ress_id) as max_ress_id
from mytable
group by elementclass
) agg on agg.elementclass = mytable.elementclass and agg.max_ress_id = mytable.res_id
group by mytable.elementclass, mytable.ress_id;
(It is possible to write a statement to access complete records in case of ties, but this is much more complicated - at least in MySQL.)
Try this:
SELECT T1.*
FROM TableName T1 JOIN
(SELECT elementclass,table_no,MAX(ress_id) as ress_id
FROM TableName
GROUP BY elementclass,table_no
)T2 ON T1.elementclass=T2.elementclass AND T1.table_no=T2.table_no AND T1.ress_id=T2.ress_id
Explanation:
Here, we are creating a temporary table T2 with maximum of ress_id for each elementclass and table_no. Then we join this table with the original table with these 3 fields and select all records from the original table T1.
Result:
PLAN_ID ELEMENTCLASS TABLE_NO RESS_ID USERID STATUS
-------------------------------------------------------------------
1 elementclass1 1 2 0006100022 N
1 elementclass2 2 0 0006100021 N
1 elementclass4 3 0 0006100023 N
See result in SQL Fiddle.

sum two different table and the subtract its total

i have this problem with me on how to sum total value from two different table and then after getting its total i want to subtract it. for example i have table "rsales" and "sales" and i have these ff vlue below.
data from "rsales"
id | total | pcode |
1 | 100 | 2143 |
2 | 100 | 2143 |
3 | 50 | 2222 |
4 | 50 | 2222 |
data from "sales"
id | total | pcode |
7 | 100 | 2143 |
8 | 50 | 2222 |
my problem is this. i want to sum all "total" values from sales and sum "total"value from rsales group by pcode.and then after getting its sum i want to subtract it. my page must be something like this.
total pcode
| 100 | 2143 |
| 50 | 2222 |
i have this ff code but it doesnt wor for me
sql "select sum(rsales.total)- sum(sales.total) as t1 where pcode = rsales.pcode"
Use:
SELECT
SUM(r.total)-(
SELECT SUM(s.total)
FROM sales AS s WHERE r.pcode=s.pcode
) as total,
r.pcode
FROM rsales AS r
GROUP BY r.pcode;
Output:
+--+--+--+--+--+-
| total | pcode |
+--+--+--+--+--+-
| 100 | 2143 |
| 50 | 2222 |
+--+--+--+--+--+-
2 rows in set
Have you tried something like this?
SELECT
SUM(L.total) as lTotal, SUM(R.total) as rTotal
FROM
sales L
INNER JOIN rsales R
ON
R.pcode = L.pcode
GROUP BY L.pcode
If you get expected values from both tables you can easily add Additions and Subtruction in FROM clause.
There's no joins needed to do this. This solution works if some pcodes are only in one table:
select SUM(total), pcode from (
select sum(total) as total, pcode from rsales group by pcode
union all
select SUM(-total) as total, pcode from sales group by pcode) salesTables
group by pcode

Categories