Solved

Find avg day in sql

Posted on 2014-10-02
1
124 Views
Last Modified: 2014-10-12
I want to find avg cid_date. @day_num is going to be 30, 60, 90, 120, 150, 180 days only. It will be from a dropdownlist from asp.net page.
Am I on the right track? To set @day_num to negative looks a bit odd. My result looks fine.

declare @ave_Days as int
DECLARE @day_num int
SET @day_num = -60

set ave_Days = (SELECT COUNT(DISTINCT cid_DATE) / case @day_num                                                              
                                                                    WHEN -60 THEN 2
                                                                    WHEN -90 THEN 3
                                                                    WHEN -120 THEN 4
                                                                    WHEN -150 THEN 5
                                                                    WHEN -180 THEN 6
                                                                    ELSE 1 end
                                    FROM         dbo.mytable
                                    where cid_DATE >= DATEADD(DD, @day_num,getdate()) and cid_DATE < GETDATE())
                                   
print ave_Days
0
Comment
Question by:VBdotnet2005
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40357901
You don't need a CASE statement:

set ave_Days = (SELECT COUNT(DISTINCT cid_DATE) / ABS(@day_num / 30)
                                     FROM         dbo.mytable
                                     where cid_DATE >= DATEADD(DD, @day_num,getdate()) and cid_DATE < GETDATE())
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now