由弦乐群总结另一个领域,你组弦乐、领域

2023-09-07 22:11:56 作者:撩妹能力者.

我刚刚找到了一份工作维持一个应用程序,存储部分产品在表中。在表中有该产品的数量。有在表中的随机数的记录,这样一种产品可以有1到n的记录内。因为我不是在设计应用程序,有人做表这种方式,(如果是我,我会创建一个新表只是产品,然后按编号,将工作的伟大我presume,但我不能改变它现在。)。现在我有一个问题,因为我从来没有这样做过(而不是在MSSQL而不是SQLite的这个问题)。你将如何按产品名称和总结分组的项目的数量是多少?

这不工作,但probabbly我可以更清楚我想要什么:

  SELECT名称,数量
从产品
GROUP BY名称
 

(有SUM的地方在这里,但如果我把总和的数量,这将probabbly只是返回所有而不仅仅是分组数量的总和。

又如(这些表中的记录):

 名称数量
意达1
意达7
ItemB 8
ItemC 1
ItemB 2
 
半导体测试设备市场现状 国产化仍不足10

查询来获取所有元素的期望的结果,但归纳为:

 意达8
ItemB 10
ItemC 1
 

解决方案

您需要 SUM

  SELECT名称,SUM(数量)的总
从产品
GROUP BY名称
 

这将 SUM 根据独特的名称组)

I just recently got a job maintaining an app that stores some products in a table. In the table there is the quantity of the product. There are a random number of records in the table so one product could have 1 to n records inside. Because I wasn't designing the app someone made the table this way, (If it was me, I'd create a new table just for products then group by id, would work great I presume, but I cant change it now.). Now I have a problem since I never did this before (Not on MSSQL and not on SQLite for that matter). How would you group by product name and sum the quantity of grouped items?

This doesn't work but probabbly I can be more clear what i want:

SELECT Name, Quantity
FROM products
GROUP BY Name

(there is SUM somewhere here, but if I put sum on quantity this will probabbly just return the sum of all quantities not just the ones grouped.

Another example (these are records in the table):

NAME     Quantity
ItemA    1
ItemA    7 
ItemB    8
ItemC    1
ItemB    2

The desired result of the query to fetch all elements but grouped would be:

ItemA  8
ItemB 10
ItemC  1

解决方案

You need SUM on the Quantity:

SELECT Name, Sum(Quantity) as Total
FROM products
GROUP BY Name

This will SUM the Quantity according to the unique Name groups :)