ewef :
Table name is User
id| name |daily_salary | expense | days
1 A $100 $50 5
1 B $50 $30 5
1 C $200 $50 5
1 C $100 $50 3
2 D $200 $50 2
3 E $200 $50 1
I would like to have expense/(daily salary*days).
From the table, ID is usually grouped.
But if there are same names, id and name should be grouped.
For example, when id is 1 and name is C, $50/{($200*5)+($100*3)}.
So it should be
id | name | value
1 A $50/$500
1 B $30/$250
1 C $50/$1500
2 D $50/$400
3 E $50/$200
Here is the query that I have tried.
select id, name, (select expense/sum(daily_salary*days) from user group by name)
from user
group by id
The error message says the subquery should return more than one row.
Is there a way to fix this or implement it properly?
Thanks
Barmar :
SELECT id, name, MAX(expense)/SUM(daily_salary * days)
FROM user
GROUP BY id, name
I use MAX(expense)
to get a single expense
value from the group.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=402212&siteId=1