2
I have a query in SQL Server using PIVOT that returns the data in column as image below:
The query is this:
SELECT * FROM(
SELECT ftd.DateAndTime AS dt, ftd.Val AS Val, CONVERT(VARCHAR, ttd.TagIndex) as tagIndex
FROM FloatTableHour ftd INNER JOIN TagTableHour ttd ON ttd.TagIndex = ftd.TagIndex
WHERE ftd.Marker = 'S'
AND ftd.DateAndTime BETWEEN '2019-01-14 00:00:000' AND '2019-01-14 23:59:059'
AND ttd.TagName IN(
'[AGUA]FT[0].Tot_NR',
'[AGUA]FT[0].Tot_D',
'[AGUA]POCO[0].IntTotPrdHou',
'[AGUA]POCO[0].IntTotPrdMin',
'[AGUA]POCO[0].OutEfePrd',
'[AGUA]POCO[0].OutEfePrdMin',
'[AGUA]LT_MAX_DA[0]', '[AGUA]LT_MIN_DA[0]'))
teste PIVOT(SUM(Val) FOR tagIndex in([9], [8], [16], [17], [30], [18], [0], [4]) ) colunas
ORDER BY dt
However, due to the limitation of the 10GB database, I am migrating to Mysql and need to perform the same query, I was able to do so to this point:
SELECT ftd.DateAndTime AS dt, ftd.Val AS Val, ttd.TagIndex as tagIndex FROM FloatTableHour ftd
INNER JOIN TagTableHour ttd ON ttd.TagIndex = ftd.TagIndex
AND ftd.DateAndTime
BETWEEN '2019-01-14 00:00:000'
AND '2019-01-14 23:59:059'
AND ttd.TagName IN(
'[AGUA]FT[0].Tot_NR',
'[AGUA]FT[0].Tot_D',
'[AGUA]POCO[0].IntTotPrdHou',
'[AGUA]POCO[0].IntTotPrdMin',
'[AGUA]POCO[0].OutEfePrd',
'[AGUA]POCO[0].OutEfePrdMin',
'[AGUA]LT_MAX_DA[0]',
'[AGUA]LT_MIN_DA[0]')
But your return is as in the image:
https://answall.com/questions/7999/converter-linha-para-coluna
– bfavaretto