如何将Group By应用于此SQL查询

时间:2009-09-16 22:13:43

标签: sql pervasive-sql

我正在尝试列出要计算的库存清单,前提是该商品在过去2个月内有销售: 我正在使用Pervasive SQL,它是一个BusinessVision表。 此查询有效,但我不知道如何聚合以显示一个项目:

SELECT "INVENTORY"."CODE", "INVENTORY"."INV_DESCRIPTION", 
"INVENTORY"."BVSTKUOM", "INVENTORY"."INV_COMMITTED", 
 "INVENTORY"."ONHAND",
"SALES_HISTORY_HEADER"."IN_DATE"
FROM "INVENTORY" INNER JOIN "SALES_HISTORY_DETAIL" ON "SALES_HISTORY_DETAIL"."CODE" = "INVENTORY"."CODE" INNER JOIN "SALES_HISTORY_HEADER" ON "SALES_HISTORY_HEADER"."NUMBER" = "SALES_HISTORY_DETAIL"."NUMBER"
    where "INVENTORY"."PROD" like 'A6O%' AND "SALES_HISTORY_HEADER"."IN_DATE" > '20090731'

确定。此查询将为我提供零件号的重复结果,具有不同的日期。我只想为每个零件编号出现一次,只要它在过去2个月内售出。 例如:

A6001-O15P   HP 700-101-O White 15" Perf   yds        0.00000      915.00000   20090810 
A6001-O15P   HP 700-101-O White 15" Perf   yds        0.00000      915.00000   20090811 
A6001-O15P   HP 700-101-O White 15" Perf   yds        0.00000      915.00000   20090812 

我希望只是这样:

A6001-O15P   HP 700-101-O White 15" Perf   yds        0.00000      915.00000

并在下一行下一个产品。 我怎么能这样做?

某些答复后的编辑部分: 感谢关于DISTINCT的建议。 我不知道为什么它会重复结果。查看(部分)查询结果(不包括“SALES_HISTORY_HEADER”。“IN_DATE”:

CODE           INV_DESCRIPTION                 BVSTKUOM   INV_COMMITTED   ONHAND    
--------------------------------------------------------- --------------- ----------
A6001-O15NP    HP 700-101-O White 15" NP       yds        0.00000         180.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 

使用GROUP BY或DISTINCT时,我得到以下结果:

CODE           INV_DESCRIPTION                 BVSTKUOM   INV_COMMITTED   ONHAND    
--------------------------------------------------------- --------------- ----------
A6001-O15NP    HP 700-101-O White 15" NP       yds        0.00000         50.00000  
A6001-O15NP    HP 700-101-O White 15" NP       yds        0.00000         180.00000 
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         0.00000   
A6001-O15P     HP 700-101-O White 15" Perf     yds        0.00000         915.00000 

我希望只有2行,180行和915行。

4 个答案:

答案 0 :(得分:1)

我不知道Pervasive SQL,但通常你必须按照你想要分组的所有列分组,在这种情况下是所有列(“SALES_HISTORY_HEADER”。“IN_DATE”除外,这不是你想要的输出)

SELECT "INVENTORY"."CODE", "INVENTORY"."INV_DESCRIPTION", 
"INVENTORY"."BVSTKUOM", "INVENTORY"."INV_COMMITTED", 
 "INVENTORY"."ONHAND"
FROM "INVENTORY" INNER JOIN "SALES_HISTORY_DETAIL" ON "SALES_HISTORY_DETAIL"."CODE" = "INVENTORY"."CODE" INNER JOIN "SALES_HISTORY_HEADER" ON "SALES_HISTORY_HEADER"."NUMBER" = "SALES_HISTORY_DETAIL"."NUMBER"
where "INVENTORY"."PROD" like 'A6O%' AND "SALES_HISTORY_HEADER"."IN_DATE" > '20090731'
group by "INVENTORY"."CODE", "INVENTORY"."INV_DESCRIPTION", 
"INVENTORY"."BVSTKUOM", "INVENTORY"."INV_COMMITTED", 
 "INVENTORY"."ONHAND"

答案 1 :(得分:1)

因此普及控制中心(8.70.014.000)没有解释计划或任何看起来像剖析的东西,所以我不能超越个人的看法。但对我来说,GROUP BY感觉有点快:

    SELECT t.code, 
           t.inv_description,
           t.bvstkuom,
           t.inv_committed,
           t.onhand,
           shh.in_date
      FROM "INVENTORY" t
INNER JOIN "SALES_HISTORY_DETAIL" shd ON shd.code = t.code
INNER JOIN "SALES_HISTORY_HEADER" shh ON shh.number = shd.number
  GROUP BY t.code, 
           t.inv_description,
           t.bvstkuom,
           t.inv_committed,
           t.onhand,
           shh.in_date

请注意,Pervasive要求您包含所有未执行聚合函数的列。

使用DISTINCT的版本:

    SELECT DISTINCT t.code, 
           t.inv_description,
           t.bvstkuom,
           t.inv_committed,
           t.onhand,
           shh.in_date
      FROM "INVENTORY" t
INNER JOIN "SALES_HISTORY_DETAIL" shd ON shd.code = t.code
INNER JOIN "SALES_HISTORY_HEADER" shh ON shh.number = shd.number

您正在处理哪种版本的BV / Pervasive?如果需要,我可以测试v6 / 2000i。

答案 2 :(得分:0)

你确定你没有找到SELECT DISTINCT,如果有重复的话,它会返回一行吗?如果你从SELECT中排除IN_DATE,我认为这就是你想要的。

SELECT DISTINCT "INVENTORY"."CODE", "INVENTORY"."INV_DESCRIPTION", 
"INVENTORY"."BVSTKUOM", "INVENTORY"."INV_COMMITTED", 
 "INVENTORY"."ONHAND"
FROM "INVENTORY" INNER JOIN "SALES_HISTORY_DETAIL" ON "SALES_HISTORY_DETAIL"."CODE" = "INVENTORY"."CODE" INNER JOIN "SALES_HISTORY_HEADER" ON "SALES_HISTORY_HEADER"."NUMBER" = "SALES_HISTORY_DETAIL"."NUMBER"
    where "INVENTORY"."PROD" like 'A6O%' AND "SALES_HISTORY_HEADER"."IN_DATE" > '20090731'

答案 3 :(得分:0)

嗯,事实证明重复的问题是因为我忘了过滤掉仓库。我应该在where子句中添加:

WHERE“INVENTORY”。“WHSE”= '00'

这解决了问题