简体   繁体   English

MySQL总和和案例查询

[英]MySQL Sum and Case Query

I create a ReportViewer with VB.NET connecting to a MySQL database. 我使用VB.NET创建一个连接到MySQL数据库的ReportViewer。 The data appears like below. 数据如下所示。

    IdProduct       Quantity       TotalPrice      OrderDate
    0001                1              10          29/09/2014
    0002                2              40          29/09/2014       
    0001                4              40          29/09/2014      
    0001                2              20          29/09/2014      
    0001                2              20          29/09/2014    

Based on the records above, I'd like the result to appear like below 根据上面的记录,我希望结果显示如下

   0001   0002  
    9      2
    90     40   

What is Query Sum Case the best use here? 最好的查询和案例是什么? Thanks in advance. 提前致谢。

select idproduct, sum(quantity), sum(totalprice)
from your_table
group by idproduct

NOTE: It's not possible for a query to "dynamically" alter the number or datatype of the columns returned, those must be specified at the time the SQL text is parsed. 注意:查询不可能“动态”更改返回的列的数目或数据类型, 必须在解析SQL文本时指定这些列。

To return the specified resultset with a query, you could do something like this: 要使用查询返回指定的结果集,您可以执行以下操作:

SELECT SUM(IF(t.IdProduct='0001',t.Quantity,NULL)) AS `0001`
     , SUM(IF(t.IdProduct='0002',t.Quantity,NULL)) AS `0002`
  FROM mytable t
 UNION ALL
SELECT SUM(IF(t.IdProduct='0001',t.TotalPrice,NULL)) AS `0001`
     , SUM(IF(t.IdProduct='0002',t.TotalPrice,NULL)) AS `0002`
  FROM mytable t

Note that the datatypes returned by the two queries will need to be compatible. 请注意,两个查询返回的数据类型将需要兼容。 This won't be a problem if Quantity and TotalPrice are both defined as integer. 如果QuantityTotalPrice都定义为整数,则不会有问题。

Also, there's no specific guarantee that the "Quantity" row will be before the "TotalPrice" row; 此外,也没有明确保证“数量”行将在“总计价格”行之前; we observe that behavior, and it's unlikely that it will ever be different. 我们观察到了这种行为,而且不太可能会有所不同。 But, to have a guarantee, we'd need an ORDER BY clause. 但是,要保证这一点,我们需要一个ORDER BY子句。 So, including an additional discriminator column (a literal in the SELECT list of each query), that would give us something we could ORDER BY. 因此,包括一个附加的鉴别符列(每个查询的SELECT列表中的文字),这将为我们提供一些ORDER BY。

Note that it's not possible to have this single query dynamically create another column for IdProduct '0003'. 请注意,不可能使单个查询动态地为IdProduct'0003'创建另一个列。 We'd need to add that to the SELECT list of each query. 我们需要将其添加到每个查询的SELECT列表中。

We could do this in two steps, using a query to get the list of distinct IdProduct, and then use that to dynamically create the query we need. 我们可以分两个步骤进行操作,使用查询获取不同的IdProduct的列表,然后使用该查询动态创建所需的查询。


BUT... with all that said... we don't want to do that. 但......对于所有的说......我们希望这样做。

The normative pattern would be to return Quantity and TotalPrice as two separate columns, along with the IdProduct as another column. 规范模式是将QuantityTotalPrice作为两个单独的列返回,并将IdProduct作为另一个列返回。 For example, the result returned by this statement: 例如,以下语句返回的结果:

SELECT t.IdProduct
     , SUM(t.Quantity) AS `Quantity`
     , SUM(t.TotalPrice) AS `TotalPrice`
  FROM mytable t
 GROUP BY t.IdProduct

And then the client application would be responsible for transforming that resultset into the desired display representation. 然后,客户端应用程序将负责将该结果集转换为所需的显示表示形式。

We don't want to push that job (of transforming the result into a display representation) into the SQL. 我们不想将这项工作(将结果转换为显示形式)推送到SQL中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM