Using alias in the same select - sql-server-2008

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

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

How can I extract the start and end of multiple ranges from a column?

I have a database where a column called StatusMotor is so:
+++++++++++++++++++++++++++++++
+ Date-time + MotorStatus+
+++++++++++++++++++++++++++++++
+ 03-02-20 18:35 + Start +
+ 03-02-20 18:35 + Start +
+ 03-02-20 18:36 + Start +
+ 03-02-20 18:35 + Start +
+ 03-02-20 18:36 + Start +
+ 03-02-20 18:36 + Start +
+ 03-02-20 18:36 + Stop +
+ 03-02-20 18:36 + Stop +
+ 03-02-20 18:36 + Stop +
+ 03-02-20 18:36 + Standby +
+ 03-02-20 18:37 + Standby +
+ 03-02-20 18:37 + Start +
+ ... + ....
I have three status (START, STOP, STAND BY) and i would extract moments when Motor works:
the date-time when i have 1st Start or Start after a Stop and Standby,
and when I have last start before a Stop or Standby.
select DateTime, MotorStatus
from TableName
Where MotorStatus like 'Start' and...
I don't know what condition i put to have this range.
How i could to do?
This is a gaps and islands problem, and on MySQL 8+ we can use the difference in row numbers method here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY DateTime) rn1,
ROW_NUMBER() OVER (PARTITION BY MotorStatus ORDER BY DateTime) rn2
FROM yourTable
)
SELECT MIN(DateTime) AS start, MAX(DateTime) AS `end`
FROM cte
WHERE MotorStatus = 'Start'
GROUP BY rn1 - rn2
ORDER BY start;
Demo

Why am I getting a "No value specified for parameter 2" error in Spring boot jdbc template

My sql query looks like:
select T.Col1
from (
select Past_Diagnoses_1 as Col1
from patienthistory
union
select Past_Diagnoses_2 as Col1
from patienthistory
union
select Past_Diagnoses_3 as Col1
from patienthistory
union
select Past_Diagnoses_4 as Col1
from patienthistory
) as T;
I keep getting an error on Spring which says No value specified for parameter 2
I am trying to union 4 columns together into 1 super long column.
This is a simplified code. I have 32 columns but for question I have shown 4
Java code :
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select T.Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_3 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_4 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_5 as Col1\n" +
" from patienthistory\n"
" where Patient_Number = ?\n" +
" ) as T";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("T.Col1")
));
}

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

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

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.