Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
Sql 左连接返回更多记录_Sql - Fatal编程技术网

Sql 左连接返回更多记录

Sql 左连接返回更多记录,sql,Sql,我有两个表和相关数据。 一张桌子是用来放产品的。另一个价格。在价格表中,一种产品可能出现多次。 如何返回显示产品的结果而不包含重复行 我的问题是 select p.Product, sum(p.Qty), max(pr.netprice) from Products p left outer join Price pr on p.Product=pr.Product where

我有两个表和相关数据。 一张桌子是用来放产品的。另一个价格。在价格表中,一种产品可能出现多次。 如何返回显示产品的结果而不包含重复行

我的问题是

select             p.Product, sum(p.Qty), max(pr.netprice)
from               Products  p
  left outer join  Price pr
  on               p.Product=pr.Product
where              p.brand=''
group by           p.Product,pr.Product
但返回更多行,因为右表有多条记录


请帮助使用distinct关键字。这将删除重复项。尽管如此,如果给定产品的价格不同,如果删除Max(),则每个产品的每个唯一价格都会有一条记录


尝试在select中输入distinct。我不确定它是否有效,但试试看。

试试这个

选择p.产品、p.数量、最大值(请购单净价) 来自产品p p.Product=pr.Product上的左外连接价格pr 其中p.brand=“”
按p.产品分组,按p.数量分组

我认为不应该采用不同的方式,我认为如果正确地进行分组,应该会得到您想要的结果。另外,我认为您不需要根据两个表中的值进行分组。。你应该真正理解你想要什么。给我们举个例子数据,这样会更容易回答你的问题。试试这个:

select p.Product, sum(p.Qty), max(pr.netprice)
from Products p
left outer join Price pr on p.Product = pr.Product
where p.brand = ''
group by p.Product -- only group on param.

如果你想要这个总数,就用上面的答案。这将为您提供一份独特的产品清单,其中包含每种产品的总数量和最高价格

select p.Product, sum(p.Qty), max(pr.netprice) 
from Products p 
left outer join Price pr on p.Product = pr.Product 
where p.brand = '' 
group by p.Product

这样做怎么样:

SELECT p.Product, sum(p.Qty), 
       (SELECT max(pr.netprice) 
          FROM Price pr 
         WHERE p.Product=pr.Product
       )
  FROM  Products  p
 WHERE  p.brand=''

您当前的查询有什么问题?从group子句中删除第二个字段(,pr.Product),结果会变得更好吗?提供一些示例数据,这将使您更容易获得帮助。在问题中,他想对数量求和,因此您不能对其进行分组。但这是您真正想要的吗?当然你没有得到重复的,但是你也没有得到你自己样本代码中的数量总和。是的,你是对的,我没有得到重复的,但是数量不是总和。。我怎样才能得到它从产品中选择不同的p.产品,p.数量,最大值(pr.netprice)p.产品上的左外连接价格pr.Product=pr.Product其中p.brand=''按p.产品分组,p.数量(但仍不按产品分组)你看过我的答案了吗?你还应该给我们一些样本数据,这样我们就更容易自己测试了。回答得好。我认为需要强调的是,通过从group by中删除pr.Product,您删除了次要表上的分组。这将消除原始查询中的重复行。再考虑一下,我认为第一个解决方案也应该有效,因为产品上的连接和产品上的分组中的连接。但是,他不需要对查询中未使用的列进行分组。
SELECT p.Product, sum(p.Qty), 
       (SELECT max(pr.netprice) 
          FROM Price pr 
         WHERE p.Product=pr.Product
       )
  FROM  Products  p
 WHERE  p.brand=''