title: Node.js Postgres fetch data from table but dont allow the same values to follow the previous fetched row
body: <p>I dont know if my title explains what I am trying to do. basically I have a table with rows stored in it looking like this.</p>
  <p><strong>id
  name
  address</strong></p>
  <p>The data inside this table can look like this:</p>
  <p>1 xy 12345</p>
  <p>2 xy 12345</p>
  <p>3 zx 44455</p>
  <p>4 zx 44455</p>
  <p>Now when I fetch the data using select, I get the same rows in the same positions they have been inserted into the database.</p>
  <p>What I want to do tho, is when there are multiple different values for the address field stored, get the result but change the positions so the query result will look like this</p>
  <p>1 xy 12345</p>
  <p>2 zx 44455</p>
  <p>3 xy 12345</p>
  <p>4 zy 44455</p>
  <p>I need to get the results in this way so the same address value never follows the previous one. If there are multiple address value stored, it has to change on every row.</p>
  <p>It must never be two times in a row address 12345 or two times in a row 44455.</p>
  <p>The logic needs to apply for as many different addresses. So if there are 4 different values stored in the table, it has to switch every row.</p>
  <p>I am wondering, if there is a way to do this directly using postgres queries.I am using typescript but I could apply the plain SQL to typescript. I know I can do it with Node or javascript with some arrayfunctions, but I would like to get the result formatted directly from the database.</p>