# Newbie Grouping Question

**URL:** <https://forum.cloverdx.com/t/newbie-grouping-question/1589>\
**Category:** CloverDX Platform\
**Created:** [February 26, 2015, 12:00am UTC](https://forum.cloverdx.com/t/newbie-grouping-question/1589 "2015-02-26T00:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dflkid](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dflkid](https://forum.cloverdx.com/u/dflkid)\
**Post date:** [February 26, 2015, 12:00am UTC](https://forum.cloverdx.com/t/newbie-grouping-question/1589/1 "2015-02-26T00:00:00Z")

</div>

I’m new to Clover and am at a lost as to how to attack this scenario. I have a flat file containing Products that I need to break down and insert or update into two DB tables (Product and ProductVariants). A “Product” would represent the high level product (think a tshirt style) and a “ProductVariant” would represent a specific type of that product (that same tshirt but in a specifc size). So I need to group the data in the master file by style and insert/update a Product table with that data, then I need to create a ProductVariant record containing the detailed product information. Hopefully that makes sense, but a simplistic schema is below to illustrate…

**Flat File:**  
Style|Description|Size  
ABCD|TShirt|Small  
ABCD|TShirt|Medium  
EFGH|TShirt|Small  
EFGH|TShirt|Medium

**Product Table:**  
ProductID|Style|Description  
1|ABCD|TShirt  
2|EFGH|TShirt

**Product Variant Table:**  
ProductID|Style|Size|Color  
1|ABCD|Small  
1|ABCD|Medium  
2|EFGH|Small  
2|EFGH|Medium

---

<div class="post-metadata">

**Author:** ![imriskal](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@imriskal](https://forum.cloverdx.com/u/imriskal)\
**Post date:** [February 26, 2015, 3:03pm UTC](https://forum.cloverdx.com/t/newbie-grouping-question/1589/2 "2015-02-26T15:03:49Z")

</div>

I would do the following:

1. Sort the input by Style.
2. Add an ID to each style. This can be done in Reformat where you maintain two global variables, one for the current ID and one for the current style. If the style changes when compared to the previous record, the ID is increased by one and it remains the same until the next change of style.
3. Send ProductID, Style and Description through the first output port of the Reformat and get rid of duplicates using Dedup component.
4. Send ProductID, Size and Color through the second output port of the Reformat. Information about style is redundant here, I would skip it in this flow.
5. Both flows continue to their own DBOutputTable which saves the records into a DB table.

I hope the description is clear but do not hesitate to ask any follow up questions.

---

<div class="post-metadata">

**Author:** ![dflkid](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dflkid](https://forum.cloverdx.com/u/dflkid)\
**Post date:** [February 27, 2015, 5:00am UTC](https://forum.cloverdx.com/t/newbie-grouping-question/1589/3 "2015-02-27T05:00:39Z")

</div>

Thank you for the direction. I’m a little confused on step #2, can you explain a little further?

---

<div class="post-metadata">

**Author:** ![imriskal](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@imriskal](https://forum.cloverdx.com/u/imriskal)\
**Post date:** [February 27, 2015, 1:24pm UTC](https://forum.cloverdx.com/t/newbie-grouping-question/1589/4 "2015-02-27T13:24:55Z")

</div>

Something like this:

```auto
string style = "";
integer productId = 0;

function integer transform() {
	if ($in.0.Style != style) {
		style = $in.0.Style;
		productId++; 
	}
	
	$out.0.ProductID = productId;
	$out.0.Style = style;
	$out.0.Description = $in.0.Description;
	
	$out.1.ProductID = productId;
	$out.1.Size = $in.0.Size;
	$out.1.Color = $in.0.Color;
	
	return ALL;
}

```
