I am using Crystal Reports v10. My current result set looks like this (there are 44 rows, this is an example):
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.