Self-Join SQL statement w/calculated data - mysql

I have a table with the following data:
id date name schedulenum paymentamt
1 12/2/2014 AB 077LR10 100
2 12/2/2014 AN 077LR10 200
3 12/2/2014 CD 077LR10 300
4 3/10/2015 AN 083LR12 200
5 3/10/2015 WC 083LR12 500
6 5/20/2015 AB 105LR20 200
7 5/20/2015 CD 105LR20 150
8 5/20/2015 RH 105LR20 150
9 5/20/2015 RG 105LR20 400
And I would like to write a query that would bring back the following results:
schedulenum paymentamt
077LR10 600
083LR12 700
105LR20 900
Basically I need to create a SQL statement that selects data from Table A that will result in 2 columns. The first column would be a unique schedule number (i.e., 'schedulenum' - there are multiple rows with the same schedulenum) and a total payment amount ('paymentamt') per schedulenum (each row will have a different 'paymentamt'). I think this would require a self-join but not sure how to do it.

Use a group by when getting sums from one table.
select schedulenum, sum(payment) from mytable
where schedulenum = x
group by schhedulenum
...
select schedulenum, sum(payment) from mytable
group by schhedulenum
order by schedulenum

No self-join needed at all. What you want is the 'group by' keyword and aggregate functions.
SELECT schedulenum, sum(paymentamt)
FROM [TABLE]
GROUP BY schedulenum;

Related

how can I write a query in mySQL which shows the average trips in a day?

I have a list of IDs and the detail of the trips they've taken. I want to see how many trips each ID takes in a day on average but I don't know how to write this query. The data I have in my table is something like this:
ID
Ride_id
Date
1
123
2022-3-4
1
124
2022-3-4
1
111
2021-2-8
2
584
2019-4-18
2
256
2019-4-18
2
805
2020-5-8
2
127
2020-5-8
2
457
2020-5-8
3
100
2021-4-7
3
101
2021-4-7
3
202
2021-5-17
3
741
2021-5-17
So basically, the average rides ID=1 takes is 1.5 and the average rides ID=2 takes is 2.5 and so on. I need a query to calculate and show the result like this:
ID
Average_of_daily_trips
1
1.5
2
2.5
3
2
My current query uses only one condition: WHERE ID in ()
First count the trips on each day for each id, then make the average over those counts.
select id, avg(trips)
from (select id, count(*) as trips
from trips
-- where id in(1,2,3)
group by id, date) t
group by id
If you need to, you can uncomment the where clause in the subquery to filter for particular ids ...

MySQL Select records exceeding the cumulative total

Given I have following table
Id
FileSizeMB
1
100
2
100
3
100
4
100
5
100
6
100
I want to select oldest records exceeding a cumulative value, in this case say 500.
So something like this
Id
Cumulative_FileSizeMB
6
100
5
200
4
300
3
400
2
500
1
600
I want to select only records with id 2 and 1 as they are >= 500.
Goal is to delete them.
Thanks
For anyone with same problem.
I have reached this solution using Mysql window functions,
and also there is no need to declare a variable for cumulative total
SELECT * from (
SELECT
id,
FileSizeMB,
SUM(FileSizeMB) OVER (ORDER BY id DESC) AS TotalFileSizeMB
FROM table
) as t1
WHERE TotalFileSizeMB > 500

Count ID in sql query

I have the following data,
id emp_id csa_taken
1 100 2
2 100 2
3 100 0
4 100 2
5 101 2
6 101 2
7 101 0
8 101 0
I expect a result with count where csa_taken=2 for individual employee.
expected result:
emp_id count_csa_taken
100 3
101 2
I have tried the following query with a failed attempt.
Select count(employee_id) From $employeeCSA where csa_taken=2
Please suggest as I am new to sql.
If I understand you correctly you like to count all employees with a cas_taken of two. As there are multiple entries for the csa_taken for one employee you need to group them.
E.g.:
SELECT COUNT(*) FROM $employeeCSA WHERE csa_taken = 2 GROUP_BY employee_id
Please note that COUNT(*) counts the rows (not the fields).
You also need group by. Try like:
Select count(employee_id),emp_id From $employeeCSA where csa_taken=2
group by emp_id
If i understand correctly, then you can try this:
SELECT emp_id,COUNT(emp_id) from dbo.Sample WHERE csa_token = 2 GROUP BY emp_id

How to get sum/expenditure in salary with minimum query

Hi here is the sql statement in mySql.
Can any body suggest best mySql statement for the following.
SELECT A.id, A.salary, A.salary+ IFNULL((SELECT SUM(B.salary) FROM test_salary B WHERE B.id < A.id ORDER BY id DESC),0) AS tot FROM test_salary A
How many times this query runs?
- number of rows available is table + 1.
I want result like this:
id salary tot
1 200 200
2 300 500
3 400 900
4 500 1400
5 600 2000
6 700 2700
7 800 3500
8 900 4400
where salary table has only id and salary field.
It is not clear from what you are trying to do here but the following is at least syntactically correct.
SELECT A.id, A.salary,
A.salary+IFNULL((SELECT SUM(B.salary) FROM test_salary B WHERE B.id = a.id),0)
from a
Based on your edit I can see that your query works. The sub query will run for every row encountered in your main query. An alternative is to use a variable
select a.id,a.salary,
#tot := #tot + a.salary as tot
from test_salary a, (select #tot:=0) t;
I suggest you compare your query to this for performance.

Replacement of correlated query

I have table with name orders:
id id_o value date
1 1 400 2014-09-30
2 1 300 2014-09-30
3 1 200 2014-09-30
4 2 100 2014-09-30
5 2 200 2014-09-30
6 3 50 2014-09-29
7 3 100 2014-09-29
8 4 300 2014-09-29
9 5 600 2014-09-28
I need select every order grouped by id_o with sum(value)< 700 and from this selected table i need display data grouped by datum.
I use multiple select:
select date, sum(mno) as mn
from (
select date,sum(value) as 'mno'
from orders
group by id_o
having sum(value)<700
) table_alias
group by date
This is result:
date mn
2014-09-30 300
2014-09-29 450
2014-09-28 600
Is there any possibility to replace or to simplify this correlated query?
Your inner query is invalid. It groups by id_o, but selects by date. In order to solve this, add an additional column to the inner queries grouping (assuming date is always the same for every id_o). You can enable strong checking by enabling the sql_mode's ONLY_FULL_GROUP_BY. Full example in SQL fiddle.
SELECT
date,
SUM(mno) AS mn
FROM (
SELECT
id_o,
date,
SUM(value) AS mno
FROM orders
GROUP BY
id_o,
date
HAVING
SUM(value) < 700
) totalPerOrder
GROUP BY date
MySQL allows this type of queries, but it's not common to do so. Consider the following data:
id id_o value date
1 1 400 2014-09-29
2 1 300 2014-09-30
3 1 200 2014-09-30
What date(s) would SELECT date, SUM(value) FROM orders GROUP BY id_o return? It could be the first, last, average, most common one, but better make it explicit. Any other DBMS wouldn't let you execute this query.
Other than that, I would rename some of the columns to be more expressive. mn, mn_o and id_o are examples of this. Also value describes nothing, anything can be a value. Even the date field could have been called value. The query itself seems fine (take care if possibly missing indexes though).