像 mysql 问题中那样进行转换和旋转

发布于 2024-10-22 20:45:42 字数 1331 浏览 1 评论 0原文

我希望有人能给我带来光明。

很长一段时间以来,我一直在尝试在一次查询中按周、按天和按年获取一些销售结果,但没有机会。

我找到了一种可以帮助我的语法,但在 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) 

我真正的查询是这个:

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 BY orders.id_comercial

我真的很欣赏一些想法,可能在西班牙已经很晚了;)

我查询的结果是这个:

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

真的谢谢大家。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2024-10-29 20:45:42

例如,这将为您提供当天、7 天和年初至今的总计,

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

仅供参考 date(concat(year(now()),'-01-01')) 为您提供今年的第一天

但是如果你想要一个数据透视表,那么

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

MySQL(或任何其他像样的 RDBMS)就没有相当于 Access 运算符 TRANSFORM 的功能,它会自动从数据中生成动态数量的列。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文