Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have a table contains two fields named ME and STE with data type decimal.I want to find

(ME used for last 90 Days / Total STE for last 90 days) * 24

and show this in another field.
I need to solve this using sql. How can I write query for solving this. Please help me to solve this.
Posted
Comments
gvprabu 9-May-13 6:57am    
give some values for actual table data and required output.
CSR89 9-May-13 7:04am    
I have tried this one. SELECT ME FROM table_name WHERE CreatedDate >= dateadd(d,-90,getdate())/(SELECT STE FROM table_name WHERE CreatedDate >= dateadd(d,-90,getdate()))*24. But showing error as "Implicit conversion from data type datetime to decimal is not allowed. Use the CONVERT function to run this query."
gvprabu 9-May-13 7:04am    
how we will find the last 90 days, In that table any date time column is there. then why u told 2 columns like ME and STE?
gvprabu 9-May-13 7:05am    
first update ur Question with table structure and sample data.
gvprabu 9-May-13 7:06am    
what ever u tried that is not correct way... send the table structure and required output

Try:
SQL
SELECT (SUM(ME) / SUM(STE)) * 24 FROM myTable WHERE DateInserted >= DATEADD(day, -90, GetDate())
 
Share this answer
 
Comments
CSR89 9-May-13 7:11am    
Hi OriginalGriff, I have one more doubt. In above question I have one more field named NAME and I have 10 entries. How can i use the query(your solution) to find my solution that is (ME used for last 90 Days / Total STE for last 90 days) * 24 ) individually for each 10 entry.
OriginalGriff 9-May-13 7:17am    
Add
GROUP BY Name
to the end of the query.
CSR89 9-May-13 7:21am    
If same name is repeating with different entry, then what to do? At that time also I need solution for all entry induvidually
OriginalGriff 9-May-13 7:25am    
Then you need to look at what makes them "different entries" - I can't say because I have no idea of your data other than what you have told me above!
CSR89 9-May-13 7:29am    
ok
I'll do it like this:

SQL
SELECT SUM(ME)/SUM(STE)*24 FROM myTable WHERE DateNew>=DATEADD(YourDay,-90,GetDate())
 
Share this answer
 
Comments
gvprabu 9-May-13 8:34am    
what is the difference between ur solution and Solution 1.? Don't post same answers like existing one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900