I have two table receivable & receive.
table: receivable
tranId roll month amount
1 1111 October-2019 10
2 1112 October-2019 10
3 1113 October-2019 10
4 1114 October-2019 10
5 1115 October-2019 10
Table: receive
tranId roll month amount
1 1111 October-2019 10
2 1111 September-2019 10
3 1113 October-2019 10
4 1114 October-2019 10
5 1115 October-2019 10
6 1116 October-2019 10
7 1117 October-2019 10
8 1118 October-2019 10
9 1119 October-2019 10
10 1120 October-2019 10
In both table month columns are string & table receive roll+month column is unique. i want to create a receivable & receive statement report of a student (Where roll=1111) from those table like.
Expected result:
month rcvamount rcvvmonth rcvvamount
---------------------------------------------------------
October-2019 10 October-2019 10
---------------------------------------------------------
September-2019 10 - -
my query is:
SELECT receive.month, sum(receive.amount) AS rcvamount, receivable.month AS rcvvmonth,
receivable.amount AS rcvvamount
FROM receive
LEFT JOIN receivable ON receive.month = receivable.month
WHERE receive.roll = 1111
GROUP BY receive.month
and resutl is:
month rcvamount rcvvmonth rcvvamount
----------------|-------|----------------------------------
October-2019 | 50 | October-2019 10
----------------|-------|----------------------------------
September-2019 10 - -
revamount in October-2019 should be 10 from receive table.
You can aggregate before joining. Or use union all and aggregate:
SELECT month, SUM(ramount) AS rcvamount,
SUM(raamount) AS rcvvamount
FROM ((SELECT month, amount as ramount, null as raamount
FROM receive
WHERE roll = 1111
) UNION ALL
(SELECT month, NULL as ramount, amount as raamount
FROM receiveable
WHERE roll = 1111
)
) r
GROUP BY month;
One nice feature of UNION ALL is that it handles missing data in either table.
Related
hi guys i really newbie in sql, i need help to generate percentage of attendance, here is the table:
Table Schedule
Schedule_ID Course_ID Lecture_ID Start_Date End_Date Course_Days
1 1 11 2019-09-09 2019-12-08 2,4,6
2 3 4 2019-09-09 2019-12-08 3,4
3 4 13 2019-09-09 2019-12-08 2,5
4 5 28 2019-09-09 2019-12-08 3
5 2 56 2020-01-27 2020-04-26 2,4
6 7 1 2020-01-27 2020-04-26 4,5
7 1 11 2020-01-27 2020-04-26 2,4,6
8 7 22 2020-01-27 2020-04-26 2,3
9 8 56 2020-01-27 2020-04-26 5
10 3 37 2020-01-27 2020-04-26 5,6
Reference of days of week used in this data.
1: Sunday, 2:Monday, 3:Tuesday, 4:Wednesday, 5:Thursday, 6:Friday, 7:Saturday
Table course_attendance
ID STUDENT_ID SCHEDULE_ID ATTEND_DT
1 1 2 2019-09-10
2 1 2 2019-09-11
3 1 2 2019-09-17
4 1 2 2019-09-18
......
46 2 1 2019-12-02
47 2 1 2019-09-11
48 2 1 2019-09-18
49 2 1 2019-09-25
50 2 1 2019-10-09
51 2 1 2019-10-16
....
111 6 1 2019-09-23
112 6 1 2019-09-30
113 6 1 2019-10-07
114 6 1 2019-10-14
table student
ID NAME
1 Jonny
2 Cecilia
3 Frank
4 Jones
5 Don
6 Harry
i need to show up like this :
STUDENT_ID NAME Course_ID Attendance rate
1 Jonny 1 82%
2 Cecilia 1 30%
3 Frank 3 100%
4 Jones 2 100%
5 Don 2 25%
6 Harry 4 40%
EDIT this my last step to get percentage:
result:
with main as (
select ca.STUDENT_ID,
ca.SCHEDULE_ID,
s.COURSE_ID,
co.NAME as course_name,
st.NAME,
count(ca.ID) as total_attendance,
((CHAR_LENGTH(s.COURSE_DAYS) - CHAR_LENGTH(REPLACE(s.COURSE_DAYS , ',', '')) + 1) * 13) as attendance_needed
from univ.course_attendance ca
left join univ.schedule s on ca.SCHEDULE_ID = s.ID
left join univ.student st on ca.SCHEDULE_ID = st.ID
left join univ.course co on ca.SCHEDULE_ID = co.ID
group by ca.STUDENT_ID, ca.SCHEDULE_ID
)
select *,total_attendance/attendance_needed as attendance_percentage
from main
order by 1,2;
This can be done following three steps.
Step 1: Calculate the total number of days a particular course of a schedule has. It's a good thing the start_date is always on Monday and the end_date is always on Sunday, which makes the week complete and saves some trouble. By calculating the total number of weeks a course go through and the number of days a week has for that course, we can get the total number of days a particular course of a schedule has.
Step 2:Calculate the total number of days a student for a schedule. This is done fairly easily. Note: As the majority part of the table has been skipped and the OP has yet to provide the complete data set, I could only have 14 existing rows provided.
Step 3: Calculate the percentage for the attendance using the result from the above two steps and get other required columns.
Here is the complete statement I wrote and tested in workbench:
select t2.student_id as student_id,`name`,course_id, (t2.total_attendance/t1.total_course_days)*100 as attendance_rate
from (select schedule_id,course_id,
length(replace(course_days,',',''))*(week(end_date)-week(start_date)) as total_course_days
from Schedule) t1
JOIN
(select count(attend_dt) as total_attendance,student_id,schedule_id
from course_attendance group by student_id, schedule_id) t2
ON t1.schedule_id=t2.schedule_id
JOIN
student s
ON t2.student_id=s.id;
Here is the result set ( the attendance_rate is not nice due to the abridged course_attendance table):
student_id, name, course_id, attendance_rate
2, Cecilia, 1, 15.3846
6, Harry, 1, 10.2564
1, Jonny, 3, 15.3846
I want to create a SQL query that count the number of babies born in month A, then it should count the babies born in month B but the second record should have the sum of month A plus B. For example;
Month | Number
--------|---------
Jan | 5
Feb | 7 <- Here were 2 babies born but it have the 5 of the previous month added
Mar | 13 <- Here were 6 babies born but it have the 7 of the two previous months added
Can somebody maybe please help me with this, is it possible to do something like this?
I have a straight forward table with babyID, BirthDate, etc.
Thank you very much
Consider using a subquery that calculates a running count. Both inner and outer query would be aggregate group by queries:
Using the following sample data:
babyID Birthdate
1 2015-01-01
2 2015-01-15
3 2015-01-20
4 2015-02-01
5 2015-02-03
6 2015-02-21
7 2015-03-11
8 2015-03-21
9 2015-03-27
10 2015-03-30
11 2015-03-31
SQL Query
SELECT MonthName(BirthDate) As BirthMonth, Count(*) As BabyCount,
(SELECT Count(*) FROM BabyTable t2
WHERE Month(t2.BirthDate) <= Month(BabyTable.BirthDate)) As RunningCount
FROM BabyTable
GROUP BY Month(BirthDate)
Output
BirthMonth BabyCount RunningCount
January 3 3
February 3 6
March 5 11
I have a table like this:
date amt sr
----------------------
2013-07-01 -10 1
2013-07-02 40 2
2013-07-03 10 3
2013-07-04 -10 4
2013-07-06 10 5
2013-07-07 -10 6
2013-07-08 10 7
2013-07-09 10 8
2013-07-10 10 9
I want this result:
date amt sr
--------------------------
2013-07-08 60 7
Get next date of last minimum negative amount date
Assuming a table called date_value with a date and int columns called dat and value
Then you can get the desired result by:
SELECT (
SELECT MIN(dat) FROM dbo.date_value
WHERE dat > ( SELECT MAX(dat) FROM dbo.date_value WHERE val < 0 )
) AS 'date',
SUM(val) AS 'amt'
FROM dbo.date_value
I have a table named Prices. Which contain ID, fkProductID (foregnkey of Products Table), PriceDate, Price, fkStore (the supplier ID who gives the prices)
Now I need to find the best price for each product for each month and the SUPPLIER name also.
Also some suppliers are giving duplicates prices. For example product id 8 got two times 1200 (ID# 8 and 3). If possible i only need to show the first supplier or a column with count values..
ID fkProductID PriceDate Price fkStore
-----------------------------------------------------
1 8 26-10-2014 1250 13
2 8 10-09-2014 1200 13
3 8 25-10-2014 1200 1
4 8 13-10-2014 1500 1
5 8 03-09-2014 1000 1
6 8 15-09-2014 1300 15
7 8 09-09-2014 950 21
8 8 10-10-2014 1200 23
9 8 09-09-2014 950 27
10 15 10-10-2014 3500 5
11 15 11-10-2014 3400 6
12 15 09-09-2014 3100 6
13 15 10-09-2014 3200 14
14 15 16-09-2014 3100 17
-----------------------------------------------------
my expected result.
-----------------------------------------------------
ID fkProductID Month Price Supplier
-----------------------------------------------------
7 8 September 950 21
2 8 October 1200 1
13 15 September 3100 13
11 15 October 3400 6
=================================================================
SCHEMA
SQL FIDDLE
You can do what you want with the substring_index()/group_concat() trick:
select substring_index(group_concat(idPrices order by Price, idPrices), ',', 1) as id,
fkProductId, monthname(PriceDate) as mon,
min(Price) as price,
substring_index(group_concat(fkStore order by Price, idPrices), ',', 1) as spec_id
from prices p
group by fkProductId, monthname(PriceDate);
I have a table
date d_id r_id p_id q_sold onhand
2012-10-10 5 1 3025 3 10
2012-10-10 5 1 3022 12 20
2012-10-10 5 1 3023 15 33
2012-10-11 5 1 3025 3 10
2012-10-11 5 1 3022 12 20
2012-10-11 5 1 3023 15 33
2012-10-12 5 1 3025 3 10
2012-10-12 5 1 3022 12 20
2012-10-12 5 1 3023 15 33
2012-10-13 5 1 3025 3 10
2012-10-13 5 1 3022 12 20
2012-10-13 5 1 3023 15 33
2012-10-14 5 1 3025 3 10
2012-10-14 5 1 3022 12 10
2012-10-14 5 1 3023 15 33
2012-10-15 5 1 3025 3 5
2012-10-15 5 1 3022 12 5
2012-10-15 5 1 3023 15 33
I would like to get the result of the q_sold divided by average of the onhand over a 5 day period, while displaying the other data for a specific date like the 2012-10-15.
I create a query
set #stdate = '2012-10-10';
set #endate = '2012-10-15';
SELECT date, d_id,r_id,p_id,q_sold,onhand,qty_sold/AVG(qty_onhand)
FROM stp_vwsales_info_tots
WHERE date BETWEEN #stdate and #endate and d_id=5
GROUP BY d_id,r_id,p_id
But the result being showed is incorrect, it displays the data for the 2012-10-10 instead of 2010-10-15
date d_id r_id p_id q_sold onhand avg
2012-10-10 5 1 3022 12 20 0.7579
2012-10-10 5 1 3023 15 33 0.4545
2012-10-10 5 1 3025 3 10 0.3273
Can anyone help?
Use your #stdate and #endate as DATE(#stdate/#endate) or DATE_FORMAT(#stdate/#endate,'%Y-%m-%d') otherwise you have to convert #stdate/#endate from string to date through MySQL
i think what your looking for is something called a simple moving average.
to calculate this you'll need to use an inline subquery - so performance won't be the best.
assuming you want to calculate the average over the previous 5 day period, try something like this:
SELECT
date, d_id, r_id, p_id, q_sold, onhand,
( SELECT q_sold/AVG(t2.onhand)
FROM stp_vwsales_info_tots AS t2
WHERE p_id=t1.p_id AND DATEDIFF(t1.date, t2.date) BETWEEN 0 AND 4
) AS 'moving_avg'
FROM stp_vwsales_info_tots AS t1
GROUP BY t1.p_id,t1.date
order by t1.date;