#!/bin/bash aplicaciones=$(mysql -u ibercivis ibercivis -N -e "select distinct(id) from app,estadisticas.media_diaria where beta=0 and app.id=media_diaria.appid and media is not null order by user_friendly_name") primerid=$(echo $aplicaciones | cut -f 1 -d " ") miselect="SELECT tb$primerid.fecha" for i in $aplicaciones do name=$(mysql -u ibercivis ibercivis -N -e "select user_friendly_name from app where id=$i") miselect="$miselect ,tb$i.media $name" mifrom="$mifrom, (select fecha, round(media,2) media from media_diaria where appid = $i) tb$i" miwhere="$miwhere and tb$primerid.fecha=tb$i.fecha" done mifrom=$(echo $mifrom | sed s/","//1) miwhere=$(echo $miwhere | sed s/"and"//1) miselect="$miselect FROM $mifrom WHERE $miwhere order by tb$primerid.fecha desc" echo $miselect