1

Suppose I have data extension and it has multiple rows. I want to retrieve all values from one column and get only distinct values. Please find below screenshot enter image description here

I want to create a picklist which I need distinct values from the City column.

1 Answer 1

4

First, you'll need to add a field in your Data Extension that has a static value (e.g. LookupKey, with a default value of 1). You can only do a lookup with an equals operator. Then you'll need to set it for your existing rows -- probably easiest with an export and overwrite import.

Once that's done, then you can use that LookupKey value in your lookup...something like this:

%%[

var @i, @cities, @row, @rowCount, @maxRows
var @City, @prevCity, @Area, @Salon, @Number

set @maxRows = 0 /* up to 2000 */

set @cities = LookupOrderedRows("Cities" ,@maxRows, "Number asc", "lookupKey","1")
set @rowCount = RowCount(@cities)
set @prevCity = ""

for @i = 1 to @rowCount do

  set @row = Row(@cities,@i)
  set @City = Field(@row, "City")
  set @Area = Field(@row, "Area")
  set @Salon = Field(@row, "Salon")
  set @Number = Field(@row, "Number")

  if @rowCount == 1 then

    output(concat("<ul>"))

  endif 

  if empty(@prevCity) or @City != @prevCity then

     set @prevCity = @City

     output(concat("<li>",@City,"</li>"))

  endif

  if @i == @rowCount then

    output(concat("</ul>"))

  endif 


next @i 

]%%

Reference

4
  • I have done just few changes. changed rowCount == rowcount to i == rowCount bcz that should be executed only one time during last loop counter. It seems that there is problem while displaying the output bcz when i print variable values they show output as expected but cities are not displaying....
    – Puja
    Commented Sep 18, 2017 at 5:27
  • I have changed write to outputline and it works.... You are awesome Adam.. Thanks for your help
    – Puja
    Commented Sep 18, 2017 at 5:46
  • Adam just small question.. Can i use variable from AMP script in client side javascript?? Suppose if i want to use city in javascript how can i do this??
    – Puja
    Commented Sep 18, 2017 at 10:11
  • Yes, you can pass values back and forth between SSJS and AMPScript, but keep in mind SSJS and AMPScript are both server-side, so they're evaluated before the page is displayed, not after like regular, client-side JavaScript. Commented Sep 18, 2017 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .