?
Solved

Find avg day in sql

Posted on 2014-10-02
1
Medium Priority
?
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

762 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