I need to create a view like this:
STUDENT JANUARY FEBRUARY MARCH ........ DECEMBER
miki 10.23 23.23 0 0
Goku 10 0 0 0
I have a table studentMovement(id_studentmovement,id_student,month,year,cost,date) that represents every sigle cost for the student in this month and year. it contains the real cost that some students had done.
Another table is Month(id_month), that has all month id (January==1 February==2 etc).
Table Students(id_student,name)
Another view is Years(id_year), it contains some year.
I want to get for a year, for all students in the table student(also the student that don't have outputs) and for all month(if there is an output in this month the value is 0) , I want get the sum all cost. SO my view is this
CREATE
ALGORITHM = UNDEFINED DEFINER = "***" #"%" SQL SECURITY DEFINER VIEW "schema"."v_student_table" AS select
"s"."id_student" AS "id_student",
"s"."name" AS "name",
"m"."id_month" AS "id_month",
coalesce(sum("sm"."amount"),0) AS "amount",
coalesce(month("sm"."date"),"month"."id_month") AS "month",
coalesce(year("sm"."date"),"year"."id_year") AS "anno"
from
((("schema"."studentMovement" "sm"
left join "schema"."students" "s" on "s"."id_student"="sm"."id_student")
left join "schema"."month" "m" on "m"."id_month"="sm".id_month")
inner join "schema"."years" "y" on "y"."id_year"="sm".id_year")
group by
coalesce(year("sm"."date"),"year"."id_year"),
coalesce(month("sm"."date"),"month"."id_month"),
"s"."id_student",
"s"."name",
"m"."id_month"
The problem is if there isn't a cost for a month in a year, I need to put 0 like value but with this query it doesn't do this.
the problem is the studentMovement has the real outputs. in the example above Goku has only one cost that is in jenuary but the student goku is not present for FEBRUARY etc, Anyone can help me?
You seem to want something like this:
select year(sm.date) as year, s.id_student, s.name,
sum(amount) as year_amount,
sum(case when month(sm.date) = 1 then amount else 0 end) as january,
sum(case when month(sm.date) = 2 then amount else 0 end) as february,
. . .
sum(case when month(sm.date) = 12 then amount else 0 end) as december,
"m"."id_month" AS "id_month",
coalesce(sum("sm"."amount"),0) AS "amount",
from schema.students s join
schema.studentMovement sm
on s.d_student = sm.id_student
group by year(sm.date) as year, s.id_student, s.name;
The . . . is the logic for the remaining months.
Related
I have this query:
SELECT Customer, SUM(SoldUnits) AS SoldUnits
FROM Uploads
WHERE Year = 2021
AND Week = 11
GROUP BY Customer;
And Returns me:
Customer
SoldUnits
CUSTOMER A
55
CUSTOMER B
32
CUSTOMER D
17
CUSTOMER C exist, but it doesn't have data for the week 11 and I want to show CUSTOMER C with 0 SoldUnits. How Can I do that?
Assuming your table does have all customers you want to appear in the report, you could do a conditional summation and remove the WHERE clause:
SELECT Customer,
SUM(CASE WHEN Year = 2021 AND Week = 11
THEN SoldUnits ELSE 0 END) AS SoldUnits
FROM Uploads
GROUP BY Customer;
I have two tables: Student and fee
student
sid
name
roll_no
1
John
22
2
Karina
32
3
Navin
42
fee
fid
s_id
month
fee
1
2
January
1000
2
3
January
1200
3
2
Fabruary
1000
I want to get students (who not paid fee) for Fabruary : like this...
Student id
Name
Roll No
January
February
1
John
22
0
0
3
Navin
42
1200
0
My code is :
SELECT s.sid,s.name,s.roll_no,f.fee
from student s
left join
fee f
ON f.fid = s.sid
AND f.month = 'January'
where s.sid NOT IN (SELECT s_id from fee
where month = 'February')
order by c.id;
I got zero value in both months for all students
------Thanks in advance-------
After correction this code works in mysql workbench but not in java application.
SELECT
s.sid,s.name,s.roll_no, ifnull(f.fee,0) as pre_month,0 as current_month
from student s
left join
fee f
ON f.s_id = s.sid
AND f.month = 'January'
where s.sid NOT IN
(SELECT s_id from fee
where month = 'February')
order by s.sid;
For the results you want, I would suggest conditional aggregation and a having clause:
SELECT s.sid, s.name, s.roll_no,
SUM(CASE WHEN f.month = 'January' THEN f.fee ELSE 0 END) as january,
SUM(CASE WHEN f.month = 'February' THEN f.fee ELSE 0 END) as february
FROM student s LEFT JOIN
fee f
ON f.fid = s.sid AND f.month = 'January'
GROUP BY s.sid, s.name, s.roll_no
HAVING SUM(CASE WHEN f.month = 'February' THEN f.fee ELSE 0 END) = 0 AND
SUM(CASE WHEN f.month = 'January' THEN f.fee ELSE 0 END) > 0;
Here is a db<>fiddle.
Note that storing month names in a column is usually a really bad idea. It does not distinguish between the years, for instance. It is much better to use a date.
You can use a left join to join the students with their fees, and then a having clause to filter the list down to those who don't have one for February. The below query should get you a list of all students who did not pay a fee for February.
It also looked like you might be using the wrong column on the fee table for the join. It looked like you might want s_id instead of fid.
select s.*
from students s
left join fee f on f.s_id=s.sid and f.month='February'
having f.fid is null;
I need to create a view like this:
STUDENT JANUARY FEBRUARY MARCH ........ DECEMBER
miki 10.23 23.23 0 0
Goku 10 0 0 0
Luffy 0 0 0 0
I have a table studentMovement(id_studentmovement,id_student,month,year,cost,date,id_university,university_name)
that represents every sigle cost for the student in this month and year. it contains the real cost that some students had done in a certain course in a certain university.
Table Students(id_student,name)
Table University(id_university,university name);
I want to get for all university, all student and for single student the cost for every months ( also if the student doesn't spent anything in a certain university:
This sql query is this:
select year(sm.date) as year, s.id_student, s.name,
sum(amount) as year_amount,
sum(case when month(sm.date) = 1 then amount else 0 end) as january,
sum(case when month(sm.date) = 2 then amount else 0 end) as february,
. . .
sum(case when month(sm.date) = 12 then amount else 0 end) as december,
u.id_university as id_university,
u.university_name as university_name
from ((schema.students s left join
schema.studentMovement sm
on s.d_student = sm.id_student ) inner join schema.university u on u.id_university=sm.id_university)
group by year(sm.date) as year, s.id_student, s.name, u.id_university,
u.university_name
THe query put wrong value and some value are null.Anyone can help me?
Null values appear from your do Left Join Statement. Because it returns all records from the left table, and the matched records from the right table. The result is NULL from the right side, if there is no match.
select
year(sm.date) as year,
s.id_student,
s.name,
sum(amount) as year_amount,
sum(case when month(sm.date) = 1 then amount else 0 end) as january,
sum(case when month(sm.date) = 2 then amount else 0 end) as february,
. . .
sum(case when month(sm.date) = 12 then amount else 0 end) as december,
u.id_university as id_university,
u.university_name as university_name
from
((schema.students s
inner join schema.studentMovement sm on s.d_student = sm.id_student )
inner join schema.university u on u.id_university=sm.id_university)
group by
year(sm.date),
s.id_student,
s.name,
u.id_university,
u.university_name
If you want to not see Null change your Left Join to Inner Join instead because Inner Join selects records that have matching values in both tables.
Try just using inner joins in the from clause:
FROM schema.students s JOIN
schema.studentMovement sm
ON s.d_student = sm.id_student JOIN
schema.university u
ON u.id_university = sm.id_university
Maybe you noticed some wrong values in the amount field: try to delete the double quotes
COALESCE(SUM(sm.amount), 0) AS amount,
I am unable to write a query which needs the following :
I have 4 tables of Game products:
Table 1 (Soccer)
Product_Name
SoccerBall
SoccerShoes
SoccerShins
Table2 (Cricket)
Product_Name
CricketBall
CricketStumps
CricketBat
Table3(Rugby)
Product_Name
RugbyBall
Table4(Pingpong)
Product_Name
Pingpongball
I also have a table generating a revenue about all the products which is as follows:
Table5
Userid OrderSno Product_Name OrderTime Revenue
123 66243 CricketBall 12Jan2012 35
123 66553 CricketBat 15June2013 60
123 36476 SoccerBall 15Dec2013 15
The result table should be something like this :
Ordertime(Sorted) Cricket(3months) Cricket(6months) Cricket(Lifetime)
12Jan2012 0 0 0
15June2013 0 0 35
15 Dec2013 0 60 95 (60+35)
Soccer(3 months) Soccer(6months) Soccer(Lifetime)
0 0 0
0 0 0
0 0 0
The above table gives the revenue generated for every product purchased before that particular product was purchased. This is based on the orderdate/time sorted.
For example : The first order placed by user 123 was on 12Jan2012. So that user had not purchased anything before that since it was his first order. Hence the first row of the result table should be 0.
Coming to the 2nd row, the 2nd purchase that he made was on 15June2013. SO the result table should contain all the revenue for the respective product type before 2nd order was made. Hence in this case 35 would be there under Cricket field (Since the Product_name belongs to Cricket table) and it would fall into the Lifetime field. This is because the order purchase date is 15June2013. So 3 months before this nothing was purchased. Similarly 6 months before this date nothing was purchased. But before 1 year or more than that Cricket Ball was purchased which generated a revenue of 35. Hence the value of 35 should fall into Lifetime field of Cricket based on the Product_Name of Cricket Ball.
The same thing should happen for all the products. I know the query is complex and i am not sure whether this is feasible or not. Since i am new to any help regarding this would be appreciated.
Well I am not sure if this exactly what you want but I think its work taking a look at. If you want all the sport in one table a Union would do it.
Select Product_Name
,YEAR(ordertime)
,SUM(case when MONTH(ordertime) in (1,2,3) then revenue else 0 end) 'Q1'
,SUM(case when MONTH(ordertime) between 1 and 6 then revenue else 0 end) 'Q2'
,SUM(case when MONTH(ordertime) between 1 and 9 then revenue else 0 end) 'Q3'
,SUM(revenue) 'Q4'
from PingPong
group by Product_Name, YEAR(ordertime)
My Database table is::
attendence date admission number attendence
2013-10-2 LSTM-0008/2013-2014 present
2013-10-19 LSTM-0008/2013-2014 absent
2013-9-20 LSTM-0008/2013-2014 present
above one is my database table.
i want to display table like this based on database table:
MonthName totalWorkingDays Present absent
october 26 1 1
november 26 1 0
i wrote mysql query like this:
SELECT DISTINCT monthname(attendencedate)as monthname , COUNT (*) as totalworking days,
(SELECT COUNT(*) FROM lstms_attendence WHERE attendence='present' AND addmissionno='LSTM-0008/2013-2014') as present,
(SELECT COUNT(*) FROM lstms_attendence WHERE attendence='absent' AND addmissionno='LSTM-0008/2013-2014') as absent
FROM lstms_attendence
WHERE addmissionno='LSTM-0008/2013-2014'
GROUP BY attendencedate;
its not working for me any one give me suggestions.
Try this:
SELECT monthname(attendencedate) AS monthname,
COUNT(*) AS totalworking_days,
SUM(CASE WHEN attendence = 'present' THEN 1 ELSE 0 END) AS present,
SUM(CASE WHEN attendence = 'absent' THEN 1 ELSE 0 END AS absent
FROM lstms_attendence
WHERE addmissionno = 'LSTM-0008/2013-2014'
GROUP BY monthname(attendencedate);
It will SUM 1 for every row that has attendence = 'present' in the present column and 0 otherwise. The same for attendence = 'absent'