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
Related
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
I have this query:
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
Is there a way to add these values count_1 + count_2 + count_3 + count_4 + count_5 + count_6 and so on.. more efficiently? MySQL keeps crashing for me when I add huge numbers of fields.
Regardless of whether the db design is right or wrong if you use an aggregation function you should use group by
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
GROUP BY name
I need your your help!!
I want to create a report with multi-value parameters with SSRS.
So, I wrote this mdx script:
WITH
-- Geography metadata
MEMBER [Measures].[Geographie]
AS StrToValue ( #SelectionGeographie + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Geographie_Label]
AS StrToValue( #SelectionGeographie + ".Hierarchy.CurrentMember.Member_Caption" )
-- Activity metadata
MEMBER [Measures].[Activite]
AS StrToValue( #SelectionActivite + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Activite_Label]
AS StrToValue( #SelectionActivite + ".Hierarchy.CurrentMember.Member_Caption" )
-- Date metadata
MEMBER [Measures].[Temps]
AS StrToValue( #Annee + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Temps_Label]
AS StrToValue( #Annee + ".Hierarchy.CurrentMember.Member_Caption" )
-- Perimetre metadata
MEMBER [Measures].[Perimetre]
AS StrToValue( #Perimetre + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Perimetre_Label]
AS StrToValue( #Perimetre + ".Hierarchy.CurrentMember.Member_Caption" )
SELECT NON EMPTY {
-- display the parameters attributes on columns
[Measures].[Geographie],
[Measures].[Geographie_Label],
[Measures].[Activite],
[Measures].[Activite_Label],
[Measures].[Temps],
[Measures].[Temps_Label],
[Measures].[Perimetre],
[Measures].[Perimetre_Label],
[Measures].[11 VA]
} ON COLUMNS,
( STRTOSET ( "{" + #SelectionGeographie + "}") ,
STRTOSET ("{" + #SelectionActivite + "}" ))
ON ROWS
FROM [MyCube]
WHERE STRTOTUPLE ( "(" +#Annee + "," + #Perimetre + ")" )
But It works with one value in parameter and not with muti-value parameters.
I have null result in my metadata members when I have multi value.
Any idea ?
Thank U
WITH
-- Geography metadata
MEMBER [Measures].[Geographie] AS "[Geographie]."+ #NiveauGeographie +".Currentmember.Uniquename"
MEMBER [Measures].[Geographie_Label] AS "[Geographie]."+ #NiveauGeographie
+".CurrentMember.Member_Caption"
SELECT NON EMPTY { [Measures].[Geographie],
[Measures].[Geographie_Label], [Measures].[11 VA]
} ON COLUMNS,
( STRTOSET ( "{" + #SelectionGeographie + "}") ,
STRTOSET ("{" + #SelectionActivite + "}" ))
ON ROWS
FROM [MyCube]
WHERE STRTOTUPLE ( "(" +#Annee + "," + #Perimetre + ")"
Below is what I have
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 1 + +
+ 1 + + 1 +
+ 2 + 1 + +
+ 2 + + 2 +
++++++++++++++++++++++++
What I want is
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 1 + 1 +
+ 2 + 1 + 2 +
++++++++++++++++++++++++
I want to combine the rows and show data for user in one row against multiple rows like I have in table.
Any idea how to do it?
Note : I don't have any row who have data for all fields. And I don't have any user with below scenario.
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 3 + 1 + +
+ 3 + 1 + 1 +
++++++++++++++++++++++++
Only 1 data in one row and two row per user.
I tried with
SELECT id, concat(field1), concat(field2) from myTable
GROUP BY id;
but its not coming.
data at sqlfiddle
You need to use one of MySQL's aggregate functions when aggregating grouped data. Either use GROUP_CONCAT() in place of (the non-aggregate string function) CONCAT(), or else (better for numeric data) use SUM():
SELECT id, SUM(field1), SUM(field2) FROM myTable GROUP BY id
Demo
Below is what I have
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 1 + +
+ 1 + 23 + +
+ 1 + + 1 +
+ 1 + + 33 +
+ 2 + 55 + +
+ 2 + + 2 +
+ 2 + + 23 +
++++++++++++++++++++++++
What I want is
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 23 + 33 +
+ 2 + 55 + 23 +
++++++++++++++++++++++++
I want to combine the rows (with greatest data) and show data for user in one row against multiple rows like I have in table.
Any idea how to do it?
Note : I don't have any row who have data for all fields. Only 1 data in one row and two or more rows per user.
I tried with
SELECT id, GROUP_CONCAT(MAX(field1)), GROUP_CONCAT(MAX(field2)) from myTable
GROUP BY id;
but its giving error as
Invalid use of group function:
data at sqlfiddle
This question is bit advanced to my earlier question, showing data in one row (from multiple rows)
SELECT id, MAX(field1), MAX(field2) FROM myTable GROUP BY id;
This simple query should do the trick.
SELECT id, MAX(field1), MAX(field2)
FROM myTable
GROUP BY id;
it groups all the rows with the same id and selects the maximum value within each group for each column