Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL_Subtraction_Query_Problem

Author  Topic 

Agenteusa
Starting Member

11 Posts

Posted - 2007-12-04 : 09:09:57
Hi everyone,

I need to make a query that returns some percentages but I have thought it over and can´t figure out how to do it. Let me explain.

I have a table called [Software] and in this table one of the columns is called [Warranty_Expiration] of the type "smalldatetime". Now one client can have more than one software.

What I want is to create an SP that will return me the percentage of the software from one client that has valid warranty. Let's say:

(Total amount of software from one client - Total software from that client that has warranty) / Total amount of software from one client.

I will always have to check that column for getdate() <= [Warranty_Expiration].

Can someone give me a clue..?

Thank you so much...

nr
SQLTeam MVY

12543 Posts

Posted - 2007-12-04 : 09:14:44
select client, PercentInWarranty = sum(case when getdate() <= Warranty_Expiration then 1 else 0 end) / count(*) * 100
from tbl
group by client


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-05 : 03:27:13
quote:
Originally posted by nr

select client, PercentInWarranty = sum(case when getdate() <= Warranty_Expiration then 1 else 0 end)*1.0 / count(*) * 100
from tbl
group by client


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -