0

我是进度 4gl 的新手。我有一个查询,我必须计算特定客户的销售订单数量。

例如:我的表为 so_mstr,so_cust 是我的客户名称字段,so_nbr 是我的销售订单号字段。

在 SQL 我试过这样,

select so_cust,count(distinct so_nbr) from so_mstr group by so_cust.

请帮助我进步。谢谢。

4

1 回答 1

5

Rule #1 -- Progress is NOT SQL. Nothing but pain and agony lies down the path of attempting to use SQL within the 4gl.

The 4gl does support some limited SQL-89 syntax such as:

select count(*) from customer.

but that was put in the product a very long time ago and is really just to satisfy checklist oriented product evaluations from the early 90s. It is sometimes useful for a quick hack in an ad-hoc query but not for serious use. IOW "it makes a good demo". Real SQL is supported via the SQL-92 interface. Which is a separate product from the 4gl and generally used to support reporting tools such as Crystal Reports or whatever.

If there is one "so_mstr" per order and you need to know the number of orders for every customer then the simple 4gl way to count those records is:

define variable i as integer no-undo.

for each so_mstr no-lock where so_mstr.so_domain = "xxx" break by so_mstr.so_cust:
  i = i + 1.
  if last-of( so_mstr.so_cust ) then
    do:
      display so_mstr.so_cust i.
      i = 0.
    end.
end.

Your code is trying to do something with "distinct" so my assumption about uniqueness of sales order numbers per customer might be faulty (but that seems like a really bad design on someones part).

You could do more complicated things with the FOR EACH involving break groups and automatic counting functions but those options are not any faster and they are, IMHO, much less readable. Especially for someone who is just getting started with the 4gl.

(For performance you can also use "field lists" but that really only matters if the connection is over a WAN rather than via shared memory and it complicates the syntax. Which I did not want to do for a simple example.)

于 2014-10-12T12:50:36.630 回答