List columns according to the current date

3

I have the following view:

select * from chamdosrow

It returns as follows:

NOME    1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
----------------------------------------------------------------------------------------------------------------------------------- 
MARCOS  0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
PAULO   0   5   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
JOAO    0   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
JOSE    0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
VITOR   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
DANIEL  0   6   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
CAIO    0   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
MARCEL  0   4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
PEDRO   0   2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

The columns:

1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31

I am referring to the dates of the month, what I need is that I only list the columns from 1 day to the current day, how could I do that?

    
asked by anonymous 03.05.2017 / 13:42

3 answers

3

It would be best to review the data model, but if you really want to proceed, here's an option:

SET @DiaHoje := DAY(CURDATE());

SELECT CONCAT('SELECT nome, ', GROUP_CONCAT(CONCAT(''', c.COLUMN_NAME, ''')), ' FROM chamdosrow;')
INTO @query
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = 'chamdosrow'
  AND CAST(c.COLUMN_NAME AS UNSIGNED) <= @DiaHoje
ORDER BY c.ORDINAL_POSITION;

PREPARE stmt FROM @query;

EXECUTE stmt;

It is quite simple, but if you wish, I can leave here an explanation. Here's also the usual SQLFiddle

The example above works when you directly access a table, in your case, and because you are accessing a view, you can do the following to get the name of the columns dynamically:

SET @DiaHoje := DAY(CURDATE());

SELECT CONCAT('SELECT ', GROUP_CONCAT(CONCAT(''', Dia, ''')), ' FROM chamdosrow;')
INTO @query
FROM (
  SELECT @row := @row + 1 AS Dia 
  FROM INFORMATION_SCHEMA.TABLES t1,
  (SELECT @row:=0) r 
) DiasMes       
WHERE Dia <= @DiaHoje
ORDER BY Dia;

PREPARE stmt FROM @query;
EXECUTE stmt;

Here's the second SQLFiddle .

    
10.05.2017 / 18:24
2

An approach with somewhat less complex logic, manipulating a list with equally spaced field names:

SELECT CONCAT( 
   'SELECT nome, ',
    SUBSTRING(
         ' 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31', 
         1, 
         (DAY(CURDATE())*3)-1
    ),
    ' FROM chamdosrow '
) INTO @query FROM dual;

PREPARE st FROM @query;
EXECUTE st;
    
16.05.2017 / 04:36
2

A solution similar to Gustavo, but without having to list all fields (one detail, I changed the fields from 1 to 9, putting a zero in front):

SELECT
  CONCAT(
    'SELECT ',
    SUBSTRING_INDEX(
      GROUP_CONCAT(CONCAT(''', column_name, ''') ORDER BY column_name),
      ',',
      day(curdate())),
    ' FROM tabela'
  )
FROM
  information_schema.columns 
WHERE
  table_schema=DATABASE() 
  AND table_name='tabela'
INTO @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;

See in SQL Fiddle

    
16.05.2017 / 06:09