Home > Mobile >  SOLVED -SQL How to make an OR Function only display one result
SOLVED -SQL How to make an OR Function only display one result

Time:10-20

For example, I have two columns, Date and Hours and from a variety of dates, I only want it to display one result.

SELECT Day, Hours
FROM Hours
WHERE (Day)=#2021-04-22# OR (Day)=#2021-04-23#;

Displays

Day         Hours
2021-04-22  9
2021-04-22  2
2021-04-23  6
2021-04-23  3

But I would like for it to only choose to display one of the two dates.

CodePudding user response:

You can use the Timer() function to alternate the selection of the two dates for the selection of the date to appear random:

Select 
    [Day], Hours
From 
    Hours
Where 
    [Day] = (
            Select Top 1 [Day] 
            From Hours
            Where [Day] In (#2021-04-22#, #2021-04-23#)
            Order By [Day] * (1 - 2 * (Timer() * 100 Mod 2))
            )

Timer() * 100 returns a new even or uneven integer every 1/100 second. The fancy expression returns [Day] * 1 or [Day] * -1, thus the sorting and then the top selected Day will alter accordingly.

CodePudding user response:

Select TOP N is used to specify to display only N number of records. In this example, it will return only one record and select the day and hours (2 rows)

SELECT Day, Hours
FROM Hours
WHERE Day = (
            SELECT TOP 1 Day FROM Hours
            WHERE (Day)=#2021-04-22# OR (Day)=#2021-04-23#
            )
  • Related