Why sum() function isn't giving the exact value in this case? - mysql

select s_id, s_f_name, s_l_name, s_roll_no, s_class,
SUM(mid_1_english + mid_2_english)/2 as mid_english,
SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics,
semester_final_english,
semester_final_mathematics,
SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as total_english,
SUM((mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total_mathematics,
SUM((mid_1_mathematics + mid_2_mathematics)/2 + (mid_1_english + mid_2_english)/2 + semester_final_mathematics + semester_final_mathematics) as total
from elaborate_term_result_sheet
group by s_f_name, s_l_name
order by s_roll_no;
After running this query created, it does show the out put, but value is getting a bit fluctuated
mid_1_english + mid_2_english = mid_english
mid_1_mathematics + mid_2_mathematics = mid_mathematics
mid_english + semester_final_english = total_english
mid_mathematics + semester_final_mathematics = total_mathematics
total = total_english + total_mathematics

select s_id, s_f_name, s_l_name, s_roll_no, s_class,
SUM(mid_1_english + mid_2_english)/2 as mid_english,
SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics,
semester_final_english,
semester_final_mathematics,
SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as total_english,
SUM((mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total_mathematics,
SUM((mid_1_english + mid_2_english)/2 + semester_final_english + (mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total from result_sheet_1 group by s_f_name, s_l_name order by s_roll_no;
the only change that gave the right output is
SUM((mid_1_english + mid_2_english)/2 + semester_final_english + (mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total
the snapshot of the output of this query

Related

Use a column created using AS to do aggregate functions

Can MySQL detect the columns created using AS?
Example;
SUM( Mark1 + Mark2) AS '2022_SUM'
SUM( Mark3 + Mark4) AS '2021_SUM'
(2022_SUM + 2021_SUM) AS 'SUM1'
It hits unknown column in field list.
Just write:
SELECT
SUM(Mark1 + Mark2) as '2022_SUM',
SUM(Mark3 + Mark4) as '2021_SUM',
SUM(Mark1 + Mark2) + SUM(Mark3 + Mark4) as 'SUM1'
FROM someTableName
OR
SELECT
2022_SUM,
2021_SUM,
2022_SUM + 2021_SUM as 'SUM1'
FROM (
SELECT
SUM(Mark1 + Mark2) as '2022_SUM',
SUM(Mark3 + Mark4) as '2021_SUM'
FROM someTableName
) x

SUM function in sql returns miscalculated value

Good day! I've been running around in circles for finding solution for this problem, i have summed my columns
SELECT sum(sp1)
, sum(sp2)
, sum(sp3)
, sum(sp4)
, sum(fsg)
, sum(fgg)
, sum(ffg)
, sum(fsf)
, sum(fgf)
, sum(fff)
, sum(g1)
, sum(g2)
, sum(g3)
, sum(gb35)
, sum(gb452mm)
, sum(gb453mm)
, sum(gb454mm)
, (sum(sp1) + sum(sp2) + sum(sp3) + sum(sp4) + sum(fsg) + sum(fgg) + sum(ffg) + sum(fsg) + sum(fgf)
+ sum(fff) + sum(g1) + sum(g2) + sum(g3) + sum(gb35) + sum(gb452mm) + sum(gb453mm) + sum(gb454mm)) total
, monthx
, yearx
, monthval
FROM tbl_incoming
WHERE yearx = '" & AnnualSelectYearFrm.ComboBox3.Text & "'
GROUP
BY monthx
, yearx
, monthval
ORDER
BY monthval
where the total(sum of all columns) of the colums are miscalculated, i think some of it are not in the computation, i've been wondering why and what is causing this. Please help.
try this:
(spa1+spa2+spa3+spa4)
else try this also:
sum(spa1+spa2)
instead of this:
(sum(sp1) + sum(sp2) + sum(sp3) + sum(sp4) + sum(fsg) + sum(fgg) + sum(ffg) + sum(fsg) + sum(fgf)
+ sum(fff) + sum(g1) + sum(g2) + sum(g3) + sum(gb35) + sum(gb452mm) + sum(gb453mm) + sum(gb454mm)) total

Order by in full text search in mysql does not work

I need to sort my result in full text search. this is my code
'SELECT presentations.id, ' +
'presentations.72_png , ' +
'presentations.topic_id, ' +
'presentations.name, ' +
'presentations.presenter_id , ' +
'presentations.synopsis , ' +
'presentations.tags , ' +
'presentations.teaching_guide_text , ' +
'presentations.video_type, ' +
'presenters. first_name, ' +
'presenters.last_name, ' +
'presenters.title, ' +
'presenters.company, ' +
'MATCH (presentations.name) AGAINST (%s) AS rel1 ,' +
'MATCH (presentations.synopsis) AGAINST (%s) AS rel2,' +
'MATCH (presentations.tags) AGAINST (%s) AS rel3,' +
'MATCH (presentations.teaching_guide_text) AGAINST (%s) AS rel4,' +
'MATCH (presenters.first_name,presenters.last_name) AGAINST (%s) AS rel5 ,' +
'MATCH (presenters.title) AGAINST (%s) AS rel6 ,' +
'MATCH (presenters.company) AGAINST (%s) AS rel7 , ' +
'MATCH(presentations.search_leadership) AGAINST (%s) AS rel8 ,' +
'MATCH(presentations.search_business) AGAINST (%s) AS rel9 ,' +
'MATCH(presentations.search_academic) AGAINST (%s) AS rel10 ,' +
'MATCH(presentations.search_industry) AGAINST (%s) AS rel11 ' +
'FROM presentations ' +
'INNER JOIN presenters ON ' +
'(presentations.presenter_id = presenters.id) ' +
'WHERE presentations.deleted=0 AND ' +
'presentations.published=1 AND ' +
'(MATCH (presentations.name, ' +
'presentations.synopsis , ' +
'presentations.tags , ' +
'presentations.teaching_guide_text) AGAINST (%s) OR ' +
'MATCH (presenters.first_name , ' +
'presenters.last_name , ' +
'presenters.title , ' +
'presenters.company) AGAINST (%s) OR ' +
'MATCH (presentations.search_leadership, ' +
'presentations.search_business , ' +
'presentations.search_academic , ' +
'presentations.search_industry) AGAINST (%s)) AND ' +
'academic_only = 0 ' +
'ORDER BY (rel6 * 1000)+ (rel7 * 1000) + (rel8 * 90) + (rel9 * 80) + (rel10 * 70) + (rel11 * 60) DESC ',
[word, word, word, word, word, word, word, word, word, word,
word, word, word, word, ])
and this code doesn't give me sort order. if I remove the "'ORDER BY (rel6 * 1000)+ (rel7 * 1000) + (rel8 * 90) + (rel9 * 80) + (rel10 * 70) + (rel11 * 60) DESC '" it also give the same result as previous. what's wrong with my SQL code?

Using alias in the same select

Once I set an a column name like this SUM(someColumn) AS [ColumnName], how can I use it afterwards ?
Let's say I build a query like this :
SELECT SUM(foo.A + foo.B + foo.C + foo.D + foo.E) AS [TotalFoo],
SUM(bar.A + bar.B + bar.C + bar.D + bar.E) AS [TotalBar],
-- I need [TotalFooBar]
And in the same select I want the total of [TotalFoo] and [TotalBar]. Is it possible to refere to these columns ? I could do it the long way :
SELECT SUM(foo.A + foo.B + foo.C + foo.D + foo.E) AS [TotalFoo],
SUM(bar.A + bar.B + bar.C + bar.D + bar.E) AS [TotalBar],
SUM((foo.A + foo.B + foo.C + foo.D + foo.E) +
(bar.A + bar.B + bar.C + bar.D + bar.E)) AS [TotalFooBar]
It's ok in this exemple but I have way more columns and it's hard to follow. And then what if I have to use this column ([TotalFooBar])? I'll have to rewrite the whole SUM everytime ?
What I'm looking for is something like this :
SELECT SUM(foo.A + foo.B + foo.C + foo.D + foo.E) AS [TotalFoo],
SUM(bar.A + bar.B + bar.C + bar.D + bar.E) AS [TotalBar],
SUM([TotalFoo] + [TotalBar]) AS [TotalFooBar]
Is it even possible ?
No, you can't do this in the same SELECT statement, but you can use subquery:
SELECT
TotalFoo,
TotalBar,
TotalFoo + TotalBar AS TotalFooBar
FROM
(
SELECT SUM(foo.A + foo.B + foo.C + foo.D + foo.E) AS [TotalFoo],
SUM(bar.A + bar.B + bar.C + bar.D + bar.E) AS [TotalBar],
...
FROM ...
) AS sub
...
or a CTE:
WITH CTE
AS
(
SELECT SUM(foo.A + foo.B + foo.C + foo.D + foo.E) AS [TotalFoo],
SUM(bar.A + bar.B + bar.C + bar.D + bar.E) AS [TotalBar],
...
FROM ...
)
SELECT
TotalFoo,
TotalBar,
TotalFoo + TotalBar AS TotalFooBar
FROM CTE

Why does this MySQL Select take forever

Good Evening,
I have this query and i can't manage to fix it. It never finishes but it finishes on MSSQL 2000, i want to have it on MySQL. Any advice ?
SELECT T_Coll.Nom_REGION AS Region_Col,
T_Coll.Nom_STE AS Societe_Col,
T_Coll.Nom_ETS AS Ets_Col,
T_Coll.Nom_CDP AS CDP_Col,
T_Coll.ID_Coll,
T_Coll.NomColl AS NomColl,
T_Coll.Nom_Coll AS NOM,
T_Coll.Prenom_Coll AS Prenom,
T_Coll.Trig_Coll,
T_Coll.EXTERN,
T_DETAIL.AN,
T_DETAIL.Mois_Modif,
T_DETAIL.T_AFFAIRE_ID,
T_DETAIL.T_LOT_ID,
T_DETAIL.T_SOUS_LOT_ID,
T_Type.NomType,
T_DETAIL.AUTRE_LIBELLE,
T_DETAIL.JOUR01 + T_DETAIL.JOUR02 + T_DETAIL.JOUR03 + T_DETAIL.JOUR04 + T_DETAIL.JOUR05 + T_DETAIL.JOUR06 + T_DETAIL.JOUR07 + T_DETAIL.JOUR08 + T_DETAIL.JOUR09 + T_DETAIL.JOUR10 + T_DETAIL.JOUR11 + T_DETAIL.JOUR12 + T_DETAIL.JOUR13 + T_DETAIL.JOUR14 + T_DETAIL.JOUR15 + T_DETAIL.JOUR16 + T_DETAIL.JOUR17 + T_DETAIL.JOUR18 + T_DETAIL.JOUR19 + T_DETAIL.JOUR20 + T_DETAIL.JOUR21 + T_DETAIL.JOUR22 + T_DETAIL.JOUR23 + T_DETAIL.JOUR24 + T_DETAIL.JOUR25 + T_DETAIL.JOUR26 + T_DETAIL.JOUR27 + T_DETAIL.JOUR28 + T_DETAIL.JOUR29 + T_DETAIL.JOUR30 + T_DETAIL.JOUR31 AS Total_Jours,
T_DETAIL.JOUR01 + T_DETAIL.JOUR02 + T_DETAIL.JOUR03 + T_DETAIL.JOUR04 + T_DETAIL.JOUR05 + T_DETAIL.JOUR06 + T_DETAIL.JOUR07 + T_DETAIL.JOUR08 + T_DETAIL.JOUR09 + T_DETAIL.JOUR10 + T_DETAIL.JOUR11 + T_DETAIL.JOUR12 + T_DETAIL.JOUR13 + T_DETAIL.JOUR14 + T_DETAIL.JOUR15 + T_DETAIL.JOUR16 + T_DETAIL.JOUR17 + T_DETAIL.JOUR18 + T_DETAIL.JOUR19 + T_DETAIL.JOUR20 + T_DETAIL.JOUR21 + T_DETAIL.JOUR22 + T_DETAIL.JOUR23 + T_DETAIL.JOUR24 + T_DETAIL.JOUR25 + T_DETAIL.JOUR26 + T_DETAIL.JOUR27 + T_DETAIL.JOUR28 + T_DETAIL.JOUR29 + T_DETAIL.JOUR30 + T_DETAIL.JOUR31 + T_DETAIL.REGUL AS Total_Jours_et_Reg,
T_DETAIL.REGUL,
T_Cam.ID AS ID_Cram,
T_Cam.STATUT,
T_Cam.T_COLLABORATEUR_ID AS ID_Cram_Coll,
T_DETAIL.ID AS ID_Cram_Detail,
T_DETAIL.MOIS,
T_DETAIL.JOUR01,
T_DETAIL.JOUR02,
T_DETAIL.JOUR03,
T_DETAIL.JOUR04,
T_DETAIL.JOUR05,
T_DETAIL.JOUR06,
T_DETAIL.JOUR07,
T_DETAIL.JOUR08,
T_DETAIL.JOUR09,
T_DETAIL.JOUR10,
T_DETAIL.JOUR11,
T_DETAIL.JOUR12,
T_DETAIL.JOUR13,
T_DETAIL.JOUR14,
T_DETAIL.JOUR15,
T_DETAIL.JOUR16,
T_DETAIL.JOUR17,
T_DETAIL.JOUR18,
T_DETAIL.JOUR19,
T_DETAIL.JOUR20,
T_DETAIL.JOUR21,
T_DETAIL.JOUR22,
T_DETAIL.JOUR23,
T_DETAIL.JOUR24,
T_DETAIL.JOUR25,
T_DETAIL.JOUR26,
T_DETAIL.JOUR27,
T_DETAIL.JOUR28,
T_DETAIL.JOUR29,
T_DETAIL.JOUR30,
T_DETAIL.JOUR31,
T_Supr.Suppression,
T_Coll.TYPE
FROM T_Coll
INNER JOIN T_Cam ON T_Cam.T_COLLABORATEUR_ID = T_Coll.ID_Coll
INNER JOIN T_DETAIL ON T_DETAIL.T_Cam_ID = T_Cam.ID
INNER JOIN T_Type ON T_Type.TYPE = T_DETAIL.TYPE
LEFT OUTER JOIN T_Supr ON T_Supr.ID_Cram_Detail = T_DETAIL.ID
WHERE (T_Supr.Suppression IS NULL)
ORDER BY T_Coll.NomColl,
T_DETAIL.AN,
T_DETAIL.Mois_Modif,
T_DETAIL.T_AFFAIRE_ID,
T_DETAIL.T_LOT_ID,
T_DETAIL.T_SOUS_LOT_ID
I just can't manage to make this work under MySQL !
Thanks in advance !
All of the columns used in the joins, where clause and the order by should probably be indexed. You cannot expect any kind of decent performance in any database without indexes. There is nothing very complex about your query, so this is the most likely problem.
Do not return any columns you don't need. For instance why are you returning T_Supr.Suppression when the value will always be nulll based on your where clause?
In mysql you should look at the Explain Plan to see where the problem is with a slow query. In SQL Server look at the Execution PLan. You need to learn how to read these to effectively program in SQL.