Dennis
8/19/2019 - 10:53 AM

Loop with a Grouping

%%[
set @rows1 = LookupRows("lookupTableName","matchbackVariable",matchbackVariable)
if RowCount(@rows1) > 0 then
  set @row1 = Row(@rows1,1)
  set @groupingVariable = Field(@row1,"groupingVariable")
endif

set @oneItem = "0"
set @multipleItems = "0"
set @previousItemNumber = ""
set @rows2 = LookupOrderedRows("lookupTableName",0,"groupingVariable ASC","matchbackVariable",matchbackVariable)
for @i = 1 to rowCount(@rows2) do
  set @row2 = Row(@rows2,@i)
  set @groupingVariable = Field(@row2,"groupingVariable")
  set @variable1 = Field(@row2,"fieldHeaderForVariable1")
  set @variable2 = Field(@row2,"fieldHeaderForVariable2")

  set @rows3 = LookupRows("lookupTableName","matchbackVariable",matchbackVariable,"groupingVariable",@groupingVariable)
  set @groupingVariableQuantity = RowCount(@rows3)

  if @groupingVariableQuantity == "1" then
    set @oneItem = "1"
    set @multipleItems = "0"
  elseif @previousItemNumber == @groupingVariable then
    set @oneItem = "0"
    set @multipleItems = add(@multipleItems, 1)
  else
    set @oneItem = "0"
    set @multipleItems = "1"
  endif

  if @oneItem == "1" or @multipleItems == "1" then
]%%
  <table cellpadding="0" cellspacing="0" border="0" width="100%">
    <tr>
      <td align="left" valign="top">
        (CONTENT TO BE LOOPED AND GROUPED)
        I love when camiah sends me %%=v(@variable1)=%% from %%=v(@variable2)=%%.
      </td>
    </tr>
  </table>
%%[
  else
  endif

  set @oneItem = "0"
  set @multipleItems = @multipleItems
  set @previousItemNumber = @groupingVariable

next @i endif
]%%