How to extract Ageing report using SQL? - sql

How to prepare an ageing report using SQL to identify how much payment has been due for
a. 90-120 days
b. 60-90 days
c. 30-60 days
d. 0-30 days
This should be calculated as on 6th April 2016.
Output should be like -
http://sqlfiddle.com/#!9/d83f4/1

Good question.
You would need to add a column or two in order for you to get this.
You'll need to have a calculated column letting you know the difference in days from today's date to the date of service/sale.
One more column is needed here, and that will "bucket" the record as per your pre-assigned buckets.
You can have a case statement do the work for each of these columns.
If you post some queries showing some work along these lines, we, at SO can help you along to correct your calculation.

Related

SQL QUERY: Sum of amounts equal to last day of reporting month & cutoff date is equal to and greater than reporting month

I am trying to ask this question as best as i can so please forgive me inadvance if its not 100% clear.
I have a set of data which shows the date a transaction is processed.
For the same set of data I have the date of cut-off
what i am trying to achieve is finding the sum of transactions where:
Processed in a given month but the cut-off date is on the last day of the reporting month or future dated
e.g.
From the above on May-22 should have 100 and Jun-22 i should have 200 that has not met the original deadline.
Any help on this would be great. I have tried a few solutions but could not make them work

Given starting Year and Month as separate columns, how to tell if a specific year and month are within next 6 months?

I have a table A with two columns named Year and Month. I need to join it with another table B also with Year and Month columns. The condition I need to impose is that the month in B is within next 6 months of the month in A. For example, if A.Year=2014 and A.Month=09, then B.Year=2015 and B.Month=01 would be selected because it is within the next 6 months.
I've searched on SO but have not been able to find a solution. This thread
gave me a hint of using Year*100+Month calculations. But I am not sure how to add 6 months to such a calculation easily (guess I could use modulo). Does anyone have a good clean solution to this?
Instead of Year*100+Month simply use Year*12+Month.
WHERE B.Year*12+B.Month BETWEEN A.Year*12+A.Month AND A.Year*12+A.Month +6

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.

When a start date and end date span more than one week, I need to split a row up in 2 or more new rows

I would like to begin by saying I'm quite new to sql.
That said, here is my question/problem:
I have a view that has two date columns, a variable column and a text column (for comments).
I need to be able to split up all rows where the two dates are not in the same week. And I need to be able to split the variable value as well, so that it gets evenly distributed, based on how many days were in each week. The comment must be copied as well, so to be shown in each row.
My dataset looks like this:
DateIn DateOut Amount Comment
2014-11-01 2014-11-08 600 Good
And what I want is this:
DateIn DateOut Amount Comment
2014-11-01 2014-11-07 525 Good
2014-11-08 2014-11-08 75 Good
And if the time period spreads over more weeks, I would need it to split up to equivalent number of rows.
I would be very greatful if somebody could take the time to tell me how to achive my goal, using an sql-query.
As this is my first post on the forum, I apologize for any format errors in my post.
First, you need a weeks table. I mean physical table or view, where exists one row for every week possible. (We have dates table here, +/- 30 years from now - allows easily create weeks view and similar).
Then you need link your data to weeks table with left join; join condition should check date ranges overlap with week date range (probably you have to have both week start and week end fields in your weeks table - makes comparisons easier).
Then you need to divide amounts between weeks. Because you know date range length, week length and overlapping date range length, this should be trivial :)

MDX Beginning to Current Date Sum

This is regarding calculating the sum for a set of values starting from the date a measure has entries till the current date.
So far what I could find was the YTD function. This limits the aggregation capability till the current date beginning from the first day of the year. But what my requirement is to start the calculation from the first value, this could be in the last year or may be two years before.
Eg:
Date-----------Value
11/9/2010-----2000
2/10/2011-----500
8/5/2011------1000
With YTD the value is: 1500
What I need is: 3500
I really appreciate any help on this.
Something like:
SUM([Date].[Day].AllMembers, [Measures].[Value])
OR
SUM(OpeningPeriod([Date].[Day]):ClosingPeriod([Date].[Day]), [Measures].[Value]),
where [Date].[Day] - it's a level DAY of your dimension;
[Measures].[Value] - your measure.