Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-07
3
Medium Priority
?
185 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:Lee Redhead
  • 2
3 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 24794917
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
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 2000 total points
ID: 24794998
Here's with variables and the counts

DECLARE @Days int
SET @Days = 50
DECLARE @CycleDays int
SET @CycleDays = 10
SELECT CASE ROUND(@Days/@CycleDays, 0) % 2 WHEN 1 THEN 'Red'
                        ELSE 'Green'
            END,
      ROUND(ROUND(@Days/@CycleDays, 0) / 2,0) AS GreenCount,
      ROUND(@Days/@CycleDays, 0) - ROUND(ROUND(@Days/@CycleDays, 0) / 2, 0) as RedCount

0
 

Author Closing Comment

by:Lee Redhead
ID: 31600622
Prefect, thank you very much.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question