开发者

Transform and pivot like in mysql question

I hope someone could show me the light.

I'm trying since a long time to get the result o some sales by week, day and year in one query with no chance.

I have found a syntax that can help me but dont work in mysql.

    TRANSFORM 
   Sum(Cantidad) AS Ventas 
SELECT 
   Producto, Cantidad 
FROM 
   Pedidos 
WHERE 
   Fecha Between #01-01-1998# And #12-31-1998# 
GROUP BY 
   Producto 
ORDER BY 
   Producto 
PIVOT 
   DatePart("m", Fecha) 

My real query is this one:

select sum(orders.final_price) AS total
     , orders.id_comercial
     , vendedores.nombre
from orders
   , vendedores
WHERE orders.id_comercial = vendedores.id
  AND DATE(orders.date_purchased) = DATE(NOW())
GROUP开发者_C百科 BY orders.id_comercial

I really will appreciate some ideas, it's maybe to late in teh night here in spain ;)

The result of my query is this one:

total         id_comercial         nombre
740.83        24        AITOR ANGULO
2069.37       29        FERNANDO I
482.05        32        JOSE ANDRES
961.32        33        ALBERTO FERNANDEZ -CANTABRIA-
908.66        34        GONZALO
49.54         38        LUIS LOPEZ
4082.42       39        JULEN DEL CURA
1512.87       43        ALBERTO POBLACION
1268.91       44        MASSIMO
3269.08       45        JOSE RAMON BURGA
56.49         47        EMPRESA
2791.65       48        MARCO MORILLO
1445.57       61        TINO
869.73        63        SONIA
1052.02       381       DAVID ARIAS

Really thnx to all.


For example, this will give you the day, 7-day and year-to-date totals

select sum(case when DATE(orders.date_purchased) = DATE(NOW()) then orders.final_price end) AS TotalToday
     , sum(case when DATE(orders.date_purchased) >= DATE(NOW() -7) then orders.final_price end) AS TotalLast7Days
     , sum(case when YEAR(orders.date_purchased) = YEAR(NOW()) then orders.final_price end) AS TotalYearToDate
     , orders.id_comercial
     , vendedores.nombre
from orders
   , vendedores
WHERE orders.id_comercial = vendedores.id
  AND orders.date_purchased >= date(concat(year(now()),'-01-01'))
GROUP BY orders.id_comercial, vendedores.nombre

FYI date(concat(year(now()),'-01-01')) gives you the first day of the current year

But if you want a pivot table, then

select sum(case when MONTH(orders.date_purchased) = 1 then orders.final_price end) AS Jan
     , sum(case when MONTH(orders.date_purchased) = 2 then orders.final_price end) AS Feb
      ....
     , sum(case when MONTH(orders.date_purchased) = 12 then orders.final_price end) AS Dec
     , orders.id_comercial
     , vendedores.nombre
from orders
   , vendedores
WHERE orders.id_comercial = vendedores.id
  AND orders.date_purchased >= date(concat(year(now()),'-01-01'))
GROUP BY orders.id_comercial, vendedores.nombre

There is no MySQL (or any other decent RDBMS for that matter) equivalent of the Access operator TRANSFORM, which automatically produces a dynamic number of columns from the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜