sql查询问题

时间:2009-11-23 18:02:23

标签: sql

此查询存在问题。记录被返回但未通过product_id分组,这就是我想要的。如果我从group by选项中删除其他字段,那么我会得到一个错误字段,该字段不包含在group by或aggregate函数中。

感谢您的帮助

 SELECT  
   o.Product_ID, i.producttitle, i.URLimage,i.price,
   i.customfield2, i.season, i.active, i.discontinued, 
   i.opttitle1, i.opttitle2, i.opttitle3,
      SUM(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000' 
              AND '2009-10-31 23:59:00.000' 
           THEN o.sprice ELSE 0 END ) AS totalprice_date1,
      SUM(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' 
              AND '2009-12-31 23:59:00.000' 
           THEN o.sprice ELSE 0 END ) AS totalprice_date2,
      SUM(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000'  
              AND '2009-10-31 23:59:00.000' 
           THEN o.Qty  ELSE 0 END ) AS qtysold_date1,
      SUM(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' 
              AND '2009-12-31 23:59:00.000' 
           THEN o.Qty  ELSE 0 END ) AS qtysold_date2
  FROM  dbo.getskusold o
       inner join imagereport i
          on o.product_id = i.product_id 
  GROUP BY o.Product_ID, i.producttitle, i.URLimage, 
       i.price, i.customfield2, i.season, 
       i.active, i.discontinued, i.opttitle1, 
       i.opttitle2, i.opttitle3

6 个答案:

答案 0 :(得分:3)

如果这是SQL Server,则分组基于group by子句中列出的所有字段,因此,如果同一Product_ID的任何其他字段不同,则它们将被归类为单独的行。

您要么停止输出不同的字段,要么选择聚合它们并指定重复时所需的多个值中的哪一个(最小值,最大值等)。

答案 1 :(得分:0)

如果您希望结果仅按Product_ID分组,请从GROUP BY子句中删除其他字段名称。如果您执行此操作,则还需要从SELECT列表中删除相同的字段名称(或使用一个聚合函数包围字段名称,例如。MAXMIN,{{ 1}},SUM)。如果仍然需要返回这些列,则可以对源表执行另一个AVG,以查询作为嵌套子查询的查询列。

答案 2 :(得分:0)

我认为这就是你想要的......如果没有,那么你必须更多地解释预期的输出。在ms server server+中注意,执行CTE获取prodid总数然后执行到imagereport表的链接会更快。 - 或者使用临时桌子。

SELECT getskusold.Product_ID, 
  max(imagereport.producttitle),
  max(imagereport.URLimage),
  max(imagereport.price),
  max(imagereport.customfield2),
  max(imagereport.season),
  max(imagereport.active),
  max(imagereport.discontinued),
  max(imagereport.opttitle1),
  max(imagereport.opttitle2),
  max(imagereport.opttitle3),

SUM(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000' AND '2009-10-31 23:59:00.000' THEN getskusold.sprice ELSE 0 END ) AS totalprice_date1,
SUM(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' AND '2009-12-31 23:59:00.000' THEN getskusold.sprice ELSE 0 END ) AS totalprice_date2,
SUM(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000' AND '2009-10-31 23:59:00.000' THEN getskusold.Qty  ELSE 0 END ) AS qtysold_date1,
SUM(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' AND '2009-12-31 23:59:00.000' THEN getskusold.Qty  ELSE 0 END ) AS qtysold_date2

FROM  dbo.getskusold
inner join imagereport
on getskusold.product_id = imagereport.product_id 

GROUP BY getskusold.Product_ID

答案 3 :(得分:0)

您正在“分组”分组中的所有字段,如果您想要按产品ID分组,那么这是应该在Group By子句中唯一的分组。那么Select中的所有其他列都需要使用一些聚合函数输出(行Min,Max,Avg等...)

试试这个:

 SELECT o.Product_ID, 
   Min(i.producttitle) MinTitle, Min(i.URLimage) MinImage, 
   Min(i.price) MinPrice, Min(i.customfield2) MinCustom, 
   Min(i.season) MinSeaason, Min(i.active) MinActive, 
   Min(i.discontinued) MinDiscontinued, 
   Min(i.opttitle1) MinOptTitle1, Min(i.opttitle2) MinOptTitle2, 
   Min(i.opttitle3)  MinOptTitle3,
      SUM(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000' 
              AND '2009-10-31 23:59:00.000' 
           THEN o.sprice ELSE 0 END ) AS totalprice_date1,
      SUM(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' 
              AND '2009-12-31 23:59:00.000' 
           THEN o.sprice ELSE 0 END ) AS totalprice_date2,
      SUM(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000'  
              AND '2009-10-31 23:59:00.000' 
           THEN o.Qty  ELSE 0 END ) AS qtysold_date1,
      SUM(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' 
              AND '2009-12-31 23:59:00.000' 
           THEN o.Qty  ELSE 0 END ) AS qtysold_date2
  FROM  dbo.getskusold o
       inner join imagereport i
          on o.product_id = i.product_id 
  GROUP BY o.Product_ID

或更好,(因为它更清晰),首先生成聚合结果集,然后加入图像表:

 Select z.ProductId, i.producttitle, 
    i.URLimage, i.price, i.customfield2, 
    i.season, i.active, i.discontinued,       
    i.opttitle1, i.opttitle2, i.opttitle3,
    z.totalprice_date1, z.qtysold_date1,
    z.totalprice_date2, z.qtysold_date2
 From 
    (Select Product_ID ProductId, 
        Sum(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000' 
              AND '2009-10-31 23:59:00.000' 
           THEN sprice ELSE 0 END ) AS totalprice_date1,
        Sum(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' 
              AND '2009-12-31 23:59:00.000' 
           THEN sprice ELSE 0 END ) AS totalprice_date2,
        Sum(CASE WHEN processdate BETWEEN '2009-08-01 00:00:00.000'  
              AND '2009-10-31 23:59:00.000' 
           THEN Qty  ELSE 0 END ) AS qtysold_date1,
        Sum(CASE WHEN processdate BETWEEN '2009-11-01 00:00:00.000' 
              AND '2009-12-31 23:59:00.000' 
           THEN Qty  ELSE 0 END ) AS qtysold_date2
     From dbo.getskusold
     Group By Product_ID) Z
       Join imagereport i
          on i.product_id = Z.Productid 

答案 4 :(得分:0)

根据我的经验,有很多行与特定产品ID相关联,查询不知道要返回哪一行,但我有部分解决方案。

看看这个:

select (select top 1 [Name] from Product), SupplierID
from Product
group by SupplierID

SupplierID是外键并具有多个值。我要求sql引擎选择每个组的第一行。我这样工作。如果要连接行,请检查this输出。

答案 5 :(得分:-1)

因为你没有做任何聚合组只是一种控制秩序的间接方式(并且它不可靠,因为它不是它的意思)。

如果您只想返回单个产品ID,则必须在其他字段上进行聚合,或者在where语句中添加一个子句,该子句明确告诉数据库您想要返回哪条记录。