Solved

Dividing Rows using conditions

Posted on 2014-01-23
6
387 Views
Last Modified: 2014-01-24
Hi,
I am using MS SQL Server 2012. I want to use this query in SSRS.
I have the following table:
Year     Month   AccNo    Balance
2011     2           45         900
2011     3           45         700
2011     4          45          800
2011     1          50           20
2011     2          50           15
2011     3          50           40

I want to the following Division:
Balance of AccNo 45 / Balance of AccNo50
Where AccNo45.Year = AccNo50.Year
AND   AccNo45.Month = AccNo50.Month

The Answer should be something like this:
Year     Month     Ratio
2011     2            900/15 (=60)
2011     3            800/40 (=20)

Thanks.
0
Comment
Question by:fireblues
[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
6 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 350 total points
ID: 39805591
You need to self-join the same table:
SELECT a.Year, a.Month,  a.Balance/b.Balance AS Ratio
FROM AccountBalance a INNER JOIN AccountBalance b
ON a.Year = b.Year AND a.Month = b.Month
AND a.AccNo = 45 and b.AccNo = 50

Open in new window

0
 
LVL 13

Expert Comment

by:Koen Van Wielink
ID: 39805594
ARe 45 and 50 the only 2 accounts you're interested in? I assume this is only a small set of the total data? If there are more accounts, how do you identify which account should be divided by which? If you want to standardize this in a query you need to know why you need to divide the 45 balance by the 50 balance and not the other way around.
0
 

Author Comment

by:fireblues
ID: 39805605
@Chaau - I'll check the result and will let you know.
@Chaau and @Kvwielink: Thanks for the comments.
Yes, at the moment I am interested in division of only two values. Actually AccNo 50 is Quantity and AccNo45 is Sales. So I want to know the Ratio or say price.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 25

Expert Comment

by:chaau
ID: 39805692
Just wanted to add: add " and b.Balance > 0" at the end of my statement to protect against a potential division by zero situation.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39806877
>>The Answer should be something like this:
Year     Month     Ratio
2011     2            900/15 (=60)
2011     3            800/40 (=20)<<
Why is month 3 800/40 and not 700/40?
0
 

Author Comment

by:fireblues
ID: 39807259
Yup, it would be 700/40 instead of 800/40.
Thanks Everyone :)
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

717 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