Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 当为另外两列的某些值计算时,SQL求和一列_Mysql_Sql - Fatal编程技术网

Mysql 当为另外两列的某些值计算时,SQL求和一列

Mysql 当为另外两列的某些值计算时,SQL求和一列,mysql,sql,Mysql,Sql,我需要检索两列的总和,价格和数量 我的表格包含以下列: 身份证件 文件编号 客户到岸价 客户识别码 产品代码 量 价格 起源 我需要的是得到每个客户的cif和产品代码的数量和价格之和 提前谢谢 编辑: 这是我最初的查询,它会得到重复的产品代码和客户cif SELECT id, doc_id, customer_cif, customer_id, product_code, origin, SUM(quantity) as totQuantity, SUM(price) as totPrice

我需要检索两列的总和,价格和数量

我的表格包含以下列:

身份证件 文件编号 客户到岸价 客户识别码 产品代码 量 价格 起源 我需要的是得到每个客户的cif和产品代码的数量和价格之和

提前谢谢

编辑:

这是我最初的查询,它会得到重复的产品代码和客户cif

SELECT id, doc_id, customer_cif, customer_id, product_code, origin,
SUM(quantity) as totQuantity, 
SUM(price) as totPrice
FROM sellings
WHERE created_at BETWEEN '2019-03-01' AND '2019-05-30'
AND (customer_cif = '01176854J' OR customer_cif = '01176862K')
GROUP BY customer_cif, product_code, doc_id
如果没有doc\u id,我会出现以下错误:

SQL错误[1055][42000]:选择列表的表达式1不在组中 BY子句,并包含非聚合列“sellout.sci.doc_id”,其中 在功能上不依赖于GROUPBY子句中的列;这是 与sql\u模式不兼容=仅\u完整\u组\u由

试试这个:


按客户cif、产品代码从我的表格组中选择客户cif、产品代码、数量和价格,从字面上解释您的请求,即选择客户cif、产品代码,表中的数量+价格,但这毫无意义-也许如果您能理解我如何解释您的问题,它将帮助您将问题细化为反映您实际想要做的事情的问题。我尝试过这么做,但我得到了重复的产品代码,我将把我的查询放入编辑中
select
customer_cif ,product_code,
 sum(coalesce(quantity, 0)+coalesce(price, 0) )
from your_Table
group by customer_cif ,product_code;