Solved

Decimal from datediff and Sum

Posted on 2006-07-01
6
591 Views
Last Modified: 2008-03-06
I have:


qry1)

select
      Disposition,
               AssessmentDate,
      DateDischarged,
      datediff (dd, AssessmentDate, DateDischarged)/30
from Demo
where (DateDischarged is not null and DateDischarged != '')

qry2)

select Disposition, count (Disposition)
from Demo
where (DateDischarged is not null and DateDischarged != '')
group by Disposition

How do you combine these two so that  I dispaly all of the contents from qry 1 and put the count of each 'Disposition' group at the bottom?

for instance, it will looks like:

ALF      2002-02-05       2003-01-14       11
ALF      2004-05-12       2004-07-16       2
ALF      2004-11-05       2005-03-30       4
ALF      2004-11-05       2005-03-30       4

             COUNT                           4

Dischared      2004-10-01       2004-10-22       0
Dischared      2004-12-09       2005-03-17       3
Dischared      2004-04-30       2005-09-30       17
Dischared      2005-01-13       2005-04-11       2
Dischared      2005-02-28       2005-09-30       7
Dischared      2005-04-25       2005-08-19       3
Dischared      2005-04-25       2005-08-19       3

             COUNT                           8        

Also, on datediff (dd, AssessmentDate, DateDischarged)/30, I want to have 10th decimal from the calculation.
0
Comment
Question by:ethanjohnsons
6 Comments
 
LVL 5

Expert Comment

by:CIC Admin
ID: 17024345
You would get more control of your output to program this in the report generator of your application, e.g. MS Access, Crystal, etc.

If you want to do this solely in Transact SQL, you may want to look at using COMPUTE.

You can try something like :

select
     Disposition,
     AssessmentDate,
     DateDischarged,
     datediff (dd, AssessmentDate, DateDischarged)/30
from Demo
where (DateDischarged is not null and DateDischarged != '')
order by Disposition
compute count(disposition) by disposition

You can read more about COMPUTE here : http://msdn2.microsoft.com/en-us/library/ms181708.aspx

As for adding a tenth decimal to your datediff, change :

   datediff (dd, AssessmentDate, DateDischarged)/30

to

   convert (decimal(6,1), datediff (dd, AssessmentDate, DateDischarged)/30 )

But keep in mind that it will always be xxx.0 since days will always be in whole numbers.
0
 

Author Comment

by:ethanjohnsons
ID: 17024629
datediff returns:

343
65
145
.....

If you divide by 30, it shoud return the decimals.  How do you get the decimals?
0
 
LVL 5

Accepted Solution

by:
CIC Admin earned 350 total points
ID: 17024882
Sorry, was totally overlooking the division by 30.  

Try this :

convert(numeric(7,1), convert(float, datediff (dd, AssessmentDate, DateDischarged))/30 )
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 50 total points
ID: 17024947
convert(decimal(6,1), datediff (dd, AssessmentDate, DateDischarged))/30.0
0
 
LVL 28

Assisted Solution

by:imran_fast
imran_fast earned 100 total points
ID: 17026040
try this

select
     Disposition,
     AssessmentDate,
     DateDischarged,
     cast (cast(datediff (dd, AssessmentDate, DateDischarged) as decimal(6,1))/30 as decimal(6,1))
from Demo
where (DateDischarged is not null and DateDischarged != '')
order by Disposition
compute count(disposition) by disposition
0
 

Author Comment

by:ethanjohnsons
ID: 17028514
great!  works fine.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

914 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

12 Experts available now in Live!

Get 1:1 Help Now