Dynamic name for column in table

Hi,
I have this:

I would like the name “Mes Corriente” and “Mes anterior” to be dynamic with a case statement in SQL.

It’s possible?

1 Like

Welcome,

it is possible but this is more of a SQL question than a grafana question.
Can you please post DDL and DML

--DDL
create table #sample(yadi int, yada float, yodo varchar(50))

--DML
insert into #sample
values(1, 3.5,'habichuela')

Thank u!

Well, reading again I think I expressed myself wrong.

What I’m looking, for example, is this:

SELECT COUNT(Asistentes) as (MONTHNAME(CURRENT_DATE()))

you can but dynamically. this is in ms sql server

--create table facundo(Asistentes int)

declare @sql varchar(150) = ''

select @sql = concat('SELECT COUNT(Asistentes) as [', 
FORMAT(getdate(), 'MMMM'),
 '] from facundo')

print @sql

exec(@sql)

image

Let’s see if I understood. I use mysql. The table is already created with saved data.

Image:

image

Do I have to create some stored procedure or view inside SQL and then from Grafana call that view to be able to do it?

Well you still have not provided the DDL and DML so it is hard to guide you. Since we do not have access to your mysql, we would need the table schema (not an image) and sample data to emulate your data in our mysql.

CREATE TABLE indicadores (
	Asistentes` INT(11) NULL DEFAULT NULL
)

INSERT INTO indicadores (Asistentes) VALUES (1)

use stored procedure.


DELIMITER //
CREATE PROCEDURE `indicadores_sp` ()
BEGIN
	SET @query =CONCAT("SELECT COUNT(Asistentes) as `",
(MONTHNAME(CURRENT_DATE())),
"` from indicadores");
	PREPARE stmt3 FROM @query;
	EXECUTE stmt3;
	DEALLOCATE PREPARE stmt3;
END //

DELIMITER ;

Muchas gracias! Es lo que necesitaba

1 Like