PARAMETERS cBdd, d1, d2

TEXT TO cCmd NOSHOW textmerge
select 
i.id,
i.name as tipo_papel_suave,
year(c.date) as aņo,
month(c.date) as mes,
if(substr(cust.ruc,3,1)='9' OR substr(cust.ruc,3,1)='6','CANAL INSTITUCIONAL','CANAL HOGAR') as canal,
'CONSUMIDOR FINAL' AS tipo_cliente,
'LOJA' as provincia,
'LOJA' AS ciudad,
max(brand.name) as marca,
round(max(d.price),4) as precio_comun,
round(avg(d.price),4) as precio_prom,
round(max(d.price),4) as maxprecio,
round(min(d.price),4) as minprecio,
sum(if(d.price>0,d.quantity,0)) as cant_venta,
sum(if(cust.id=0 and d.price>0,d.quantity,0)) as cant_vend_cf,
round(sum(if(d.descunit>0, d.quantity,0)),0) as cant_desc_venta,
round(sum(d.quantity*d.descunit),4) as desc_venta,
max(D.pdescunit) as porc_desct,
'desc. por volumen' as tipo_desc,
sum(if(d.price=0,d.quantity,0)) as bonif_unidad,
sum(if(d.price=0,d.quantity,0)) as bonif_present,
round(sum(d.quantity*(d.price-d.descunit)),4) as venta_vend
from invoice as c 
inner join invoicedet as d on c.id=d.id
left join customer as cust on cust.id=c.idcustomer
inner join item as i on d.iditem=i.id
left join brand  on i.idbrand=brand.id
where !c.void AND
i.id in (select id from fss1.iromar) AND
c.date between <<d1>> and <<d2>>
group by 1,2,3,4,5
order by 1,2,3,4,5

ENDTEXT

nn = SQLCONNECT('fss1')


SQLEXEC(nn,'USE '+cBdd)

SQLEXEC(nn,cCmd,'curData')


SELECT curData
GO top

cFileName = GETFILE('xls')

COPY TO &cFileName XLS 

SQLDISCONNECT(nn)
