[英]Combine 1 field from multiple rows via AmpScript
I have a data extension which contains rows and columns such as:我有一个包含行和列的数据扩展,例如:
emailAddress orderNumber firstName lastName customerOrder
cust1@gmail.com 1111 Bill Adams 2 brown shoes
cust1@gmail.com 1111 Bill Adams 2 green socks
cust1@gmail.com 1111 Bill Adams 1 orange backpack
cust1@gmail.com 2222 Bill Adams 2 pink gloves
cust2@gmail.com 3333 David Sherwood 5 yellow hats
What I'm trying to do is to create an order received email from this data, preferably without altering it from the source.我想要做的是从这些数据中创建一个收到电子邮件的订单,最好不要从源中更改它。 So ideally the email output would group the customerOrder for each customer, based on the orderNumber.因此,理想情况下,电子邮件输出将根据 orderNumber 对每个客户的 customerOrder 进行分组。 Then the customerOrder is concatenated and inserted into an email (note the above is simplified quite a bit, the customerOrder is actually HTML for insertion into an HTML table within the email).然后将 customerOrder 连接起来并插入到电子邮件中(请注意,上面简化了很多,customerOrder 实际上是用于插入到电子邮件中的 HTML 表中的 HTML)。
So far I've been able to make this much very basic progress:到目前为止,我已经取得了非常基本的进步:
%%[
Set @customerOrder =
LookupOrderedRows("transactionsList",
"0",
"customerOrder",
"orderNumber",
"1111")
]%%
With this code I can see that I have 3 entries for order number 1111. But now I'm stuck.使用此代码,我可以看到订单号 1111 有 3 个条目。但现在我卡住了。 Do I need to create an if/then loop?我需要创建一个 if/then 循环吗? Or is there some way to take the output from the LookupOrderedRows function and parse it for use in the HTML table within the email?或者有什么方法可以从 LookupOrderedRows 函数中获取输出并解析它以在电子邮件中的 HTML 表中使用?
Using one of the lookup examples on my blog, you can do something like this:使用我博客上的查找示例之一,您可以执行以下操作:
%%[
var @rows, @row, @rowCount, @numRowsToReturn, @emailAddress, @i, @prevOrderNumber
set @emailAddress = AttributeValue("emailaddr")
set @numRowsToReturn = 0 /* 0 means all */
set @rows = LookupOrderedRows("transactionsList", @numRowsToReturn, "orderNumber", "emailAddress", @emailAddress)
set @rowCount = rowcount(@rows)
if @rowCount > 0 then
set @prevOrderNumber = ""
for @i = 1 to @rowCount do
var @orderNumber, @firstName, @lastName, @customerOrder
set @row = row(@rows,@i) /* get row based on loop counter */
set @orderNumber = field(@row,"orderNumber")
set @firstName = field(@row,"firstName")
set @lastName = field(@row,"lastName")
set @customerOrder = field(@row,"customerOrder")
/* output headings for first order or when order # changes */
if empty(@prevOrderNumber) or @prevOrderNumber != @orderNumber then
outputline(concat("<br>Order #:", @orderNumber))
outputline(concat("<br>Name: ", @firstName, " ", @lastName))
outputline(concat("<br>Line items:<br>"))
set @prevOrderNumber = @orderNumber
endif
outputline(concat("<br>",@customerOrder))
next @i
else
outputline(concat("<br>No transactionsList rows found"))
endif
]%%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.