Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL to get list of dates as well as days before and after without duplicates
    primarykey
    data
    text
    <p>I need to display a list of dates, which I have in a table</p> <pre><code>SELECT mydate AS MyDate, 1 AS DateType FROM myTable WHERE myTable.fkId = @MyFkId; </code></pre> <blockquote> <p>Jan 1, 2010 - 1<br> Jan 2, 2010 - 1<br> Jan 10, 2010 - 1</p> </blockquote> <p>No problem. However, I now need to display the date before and the date after as well with a different DateType.</p> <blockquote> <p>Dec 31, 2009 - 2<br> Jan 1, 2010 - 1<br> Jan 2, 2010 - 1<br> Jan 3, 2010 - 2<br> Jan 9, 2010 - 2<br> Jan 10, 2010 - 1<br> Jan 11, 2010 - 2 </p> </blockquote> <p>I thought I could use a union</p> <pre><code>SELECT MyDate, DateType FROM ( SELECT mydate - 1 AS MyDate, 2 AS DateType FROM myTable WHERE myTable.fkId = @MyFkId; UNION SELECT mydate + 1 AS MyDate, 2 AS DateType FROM myTable WHERE myTable.fkId = @MyFkId; UNION SELECT mydate AS MyDate, 1 AS DateType FROM myTable WHERE myTable.fkId = @MyFkId; ) AS myCombinedDateTable </code></pre> <p>This however includes duplicates of the original dates.</p> <blockquote> <p>Dec 31, 2009 - 2<br> Jan 1, 2010 - 2<br> Jan 1, 2010 - 1<br> Jan 2, 2010 - 2<br> Jan 2, 2010 - 1<br> Jan 3, 2010 - 2<br> Jan 9, 2010 - 2<br> Jan 10, 2010 - 1<br> Jan 11, 2010 - 2 </p> </blockquote> <p>How can I best remove these duplicates? I am considering a temporary table, but am unsure if that is the best way to do it.</p> <p>This also appears to me that it may provide performance issues as I am running the same query three separate times.</p> <p>What would be the best way to handle this request?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload