Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 657829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:52:01+00:00 2026-05-13T22:52:01+00:00

I need to display a list of dates, which I have in a table

  • 0

I need to display a list of dates, which I have in a table

SELECT mydate AS MyDate, 1 AS DateType
FROM myTable
WHERE myTable.fkId = @MyFkId;

Jan 1, 2010 – 1
Jan 2, 2010 – 1
Jan 10, 2010 – 1

No problem. However, I now need to display the date before and the date after as well with a different DateType.

Dec 31, 2009 – 2
Jan 1, 2010 – 1
Jan 2, 2010 – 1
Jan 3, 2010 – 2
Jan 9, 2010 – 2
Jan 10, 2010 – 1
Jan 11, 2010 – 2

I thought I could use a union

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

This however includes duplicates of the original dates.

Dec 31, 2009 – 2
Jan 1, 2010 – 2
Jan 1, 2010 – 1
Jan 2, 2010 – 2
Jan 2, 2010 – 1
Jan 3, 2010 – 2
Jan 9, 2010 – 2
Jan 10, 2010 – 1
Jan 11, 2010 – 2

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.

This also appears to me that it may provide performance issues as I am running the same query three separate times.

What would be the best way to handle this request?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:52:01+00:00Added an answer on May 13, 2026 at 10:52 pm

    Although you have accepted the solution, let me give this solution for the reference:

    SELECT MyDate, Min(DateType)
    From
    (
      SELECT MyDate + T1.RecordType AS MyDate, T1.DateType
      FROM
      (
        Select 1 AS RecordType, 2 AS DateType
        Union ALL
        Select 0 AS RecordType, 1 AS DateType
        Union ALL
        Select -1 AS RecordType, 2 AS DateType
      ) AS T1
      CROSS JOIN myTable
      Where myTable.fkId = @MyFkId
    ) AS CombinedTable
    Group By MyDate
    

    Advantage of this solution, myTable is queried only once, current case we are having a filter on fkID so right now performance will not matter, but if we have to evaluate complex query then this technique can work fine with respect to Union.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display the list of names which I am getting from the
I have 2 tables from which i need to run a query to display
I have a page which list, a list of volunteers from the volunteer_list table.
I need to display a single list, ordered by date which contains different types
I have a list of dates in a table in a MySQL database (the
I'm building an index page where I need to display results which have a
I Have project in which a module needs to display the list of data
I need to display the parent from the first Element of input.xml Also I
I need to display upcoming events from a database. The problem is when I
I have a list of values I want to display in a template of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.