Link to home
Start Free TrialLog in
Avatar of Lee Redhead
Lee RedheadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How can I count the times something has occured from odd and even number in SQL?

Hello,
I have a very strange problem that I am trying to solve and am stuck.
I currently need to work out how many times an event would have occured depending on the total times it would have been possible. SOund confusing so I will explain.

There is a set interval for an event to happen, so for example every 10 days the colour of an item needs to alternate, so Red,Green,Red,Green,etc.

Now the total time to check this over is say 50 days, so the cycle has happened 5 times.

This means that Red has been shown 3 times and Green has been shown 2 times.

How can I work this out programtically within SQL? So for example the system querys the data and works out that the time of 74 days it would then work out how many times Green has been shown and how many times Red has been shown?

Lee
Avatar of UnifiedIS
UnifiedIS

Divide the days by 10 and round to get the number of cycles.  use module to determine if the number of cycles is odd (red) or even (green)

SELECT CASE ROUND(74/10, 0) % 2 WHEN 1 THEN 'Red'
                        ELSE 'Green'
            END
ASKER CERTIFIED SOLUTION
Avatar of UnifiedIS
UnifiedIS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lee Redhead

ASKER

Prefect, thank you very much.