简体   繁体   English

如何使用LIKE进行GROUP和AVG行

[英]How to GROUP and AVG rows using LIKE

I am using Crystal Reports v10. 我正在使用Crystal Reports v10。 My current result set looks like this (there are 44 rows, this is an example): 我当前的结果集看起来像这样(有44行,这是一个例子):

item code     item desc          cost
21010DF       DOUG FIR 2x10-10   300.00
21012DF       DOUG FIR 2x10-12   310.50
21014DF       DOUG FIR 2x10-14   313.25
21016DF       DOUG FIR 2x10-16   316.10
21018DF       DOUG FIR 2x10-18   319.56
2410DF        DOUG FIR 2x4-10    271.69
2412DF        DOUG FIR 2x4-12    273.12
2414DF        DOUG FIR 2x4-14    275.98
12CDX         PLYWOOD 1/2 CDX     15.00
34TGADV       PLYWOOD T&G ADV     24.00
58CDX         PLYWOOD 5/8 CDX     18.00

I've been asked to group these items and provide an average cost for each group. 我被要求对这些项目进行分组,并为每个组提供平均成本。 The grouping rules are defined by the organization (they are random - some are logically groups while some rows are left out of of the logical group). 分组规则由组织定义(它们是随机的 - 一些是逻辑组,而一些行不在逻辑组之外)。

The result set needs to look like this: 结果集需要如下所示:

item desc                 avg cost
DOUG FIR 2x10 (10-14)     300.00
DOUG FIR 2x10-16          316.10
DOUG FIR 2x10-18          319.56
DOUG FIR 2x4 (10-14)      271.69
PLYWOOD CDX                16.50    
PLYWOOD T&G ADV            24.00

So far, this is what I got but it doesn't seem to be coming close to what I need: 到目前为止,这是我得到的,但它似乎没有接近我需要的东西:

SELECT item_cd, item.item_desc, AVG(inv.cst) as avg cost
FROM item, inv
WHERE item.item_id = inv.item_id
AND item.item_cd LIKE '210%DF'
GROUP BY item.item_cd, item.item_desc

Can anyone provide advice on how to achieve this? 谁能提供有关如何实现这一目标的建议?

Well, if there is no logic to the grouping, then it has to be manual, sorry. 好吧,如果分组没有逻辑,那么它必须是手动的,抱歉。 In your case, for that particular result set, you can do the following: 在您的情况下,对于该特定结果集,您可以执行以下操作:

SELECT  CASE WHEN item_cd IN ('21010DF','21012DF','21014DF') THEN 'DOUG FIR 2x10 (10-14)'
        WHEN item_cd IN ('2410DF','2412DF','2414DF') THEN 'DOUG FIR 2x4 (10-14)'
        WHEN item_cd IN ('12CDX','58CDX') THEN 'PLYWOOD CDX' ELSE item.item_desc END item_desc,
        AVG(inv.cst) as avg cost
FROM item 
LEFT JOIN inv
ON item.item_id = inv.item_id
WHERE item.item_cd LIKE '210%DF'
GROUP BY CASE WHEN item_cd IN ('21010DF','21012DF','21014DF') THEN 'DOUG FIR 2x10 (10-14)'
         WHEN item_cd IN ('2410DF','2412DF','2414DF') THEN 'DOUG FIR 2x4 (10-14)'
         WHEN item_cd IN ('12CDX','58CDX') THEN 'PLYWOOD CDX' ELSE item.item_desc END

But you should do what @jclozano said in a comment, and have the items that need to be grouped on a column marking that they belong to that specific group. 但你应该做@jclozano在评论中所说的内容,并且需要将这些项目分组在一个标记它们属于该特定组的列上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM