Solved

Dividing Rows using conditions

Posted on 2014-01-23
6
373 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
6 Comments
 
LVL 24

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 12

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 24

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

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

896 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

17 Experts available now in Live!

Get 1:1 Help Now