开发者

SQL Grouping using left join

开发者 https://www.devze.com 2023-01-30 16:39 出处:网络
I have 2 tables with related data. One table is for products and the other price. In the price table one product may appear several times. How can I r开发者_运维技巧eturn the result by grouping.

I have 2 tables with related data. One table is for products and the other price. In the price table one product may appear several times. How can I r开发者_运维技巧eturn the result by grouping.

Below is my Query but the output is not with Group

   SELECT distinct 
          p.Product, 
          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, p.Qty 


You should probably leave the Qty out of the group by, like this:

 SELECT p.Product, 
        MAX(pr.netprice) 

   FROM Products p 
   LEFT OUTER JOIN Price pr ON p.Product=pr.Product 
   WHERE p.brand='' 
   GROUP BY p.Product
0

精彩评论

暂无评论...
验证码 换一张
取 消