1

我正在尝试列出要盘点的库存清单,前提是该商品在过去 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

4 回答 4

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"
于 2009-09-16T22:22:59.553 回答
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 进行测试。

于 2009-09-17T03:04:56.780 回答
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'
于 2009-09-16T22:29:22.363 回答
0

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

其中“库存”。“WHSE”='00'

这解决了这个问题

于 2009-10-09T22:16:14.127 回答