I've been trying to develop a cashflow statement in access 2007. This would be so easily done in excel using formulas such as
= SUM (B6:M6) / CountIF(B6:M6)>0
but I cant seem to wrap my head around this when it comes to access. And I need this for every company we enter data on. The cashflow statement is supposed to look like this (Since I can't yet post a pic):
----------------------------------------------------------------------------------------------------------------
Particulars | Jan | Feb | Mar | Apr | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Average |
Sales---------------------->
Salary------>
Transportation----->
and about 10 other items in the row, all with entries for Jan till Dec, however, sometimes we take 6 months worth of data and sometimes for all 12 months. (Imagine a basic excel sheet with items on the first column and headers for the next 12-13 columns).
In access, I made tables for each Item with columns as the months, eg. tblRcpt--> |rcpt_ID|Jan|Feb|... and so on till dec for all the items. Then they will be arranged and presented in an entry form which would be designed to look similar to the above table while later I would query and link them together to presentthe complete cashflow statement.
Now comes the question, I need to Average together the columns (as you can see in the right most column), BUT I only want to average together those months that have been filled (Sometimes in accounting people enter '0' where there is no data), so I cant just sum the columns and divide by twelve. It has to be dynamic, all functions seem to center around counting and averaging ROWs, not COLUMNs.
Thanks for just bearing with me and reading this, any help would be much appreciated.
Try this
(Jan + Feb + ... + Dec) /
( case when Jan = 0 then 0 else 1 end
+ case when Feb = 0 then 0 else 1 end
+ case when Dec = 0 then 0 else 1 end )
as Avg
Your table structure should be:
Particulars | Month | Amount
Sales 1 500
Sales 2 1000
Salary 1 80000
...and so on. You can either not enter rows when you don't have a value for that month, or you can handle them in the SQL statement (as I have below):
SELECT Particulars, AVG(Amount) AverageAmount
FROM MyTable
WHERE NULLIF(Amount, 0) IS NOT NULL
GROUP BY Particulars;
Related
In MS Access 365 I have 2 tables and I want to count the occurrence of a year from the first table in part of a string of the second table, purely with SQL (so far I used VBA, but I want to simplify).
The first table (tDistinctYears) contains all the years, in which one of our members paid:
ID
PaymentYear
1
2015
2
2016
3
2017
3
2018
4
2019
5
2020
6
2021
7
2022
The second table (tPayments) has all payments from members with one column containing a membership number and the other one containing payment years. Sometimes a member pays for one year, sometime for several years. The table therefore looks like that:
MembershipNr
YearPayment
11
2016
11
2017
11
2018
26
2017
26
2018;2019
26
2020;2021;2022
38
2016
38
2017
38
2018;2019;2020;2021
I want a query which tells me how many members paid in which year:
PaymentYear
Count
2015
0
2016
2
2017
3
2018
3
2019
2
2020
2
2021
2
I used the following SQL query, which I found using various answers on stackoverflow:
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tDistinctYears.PaymentYear like "*" & tPayments.YearPayment & "*"
WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <= YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
But what I get is this:
PaymentYear
Count
2015
0
2016
2
2017
3
2018
1
2019
0
2020
0
2021
0
It seems as if the above query does not use the “like” expression in the JOIN ON section.
Can someone help me, please?
I think you are close just alter column in where condition tPayments.YearPayment should be first and tDistinctYears.PaymentYear should be inside like operator.
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tPayments.YearPayment like "*" &
tDistinctYears.PaymentYear
& "*" WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <=
YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
On my dataset I select information from four different years sorted by date and how many subscriptions I had on said date, which looks something like this:
Date Year Subs Day
15/09/2014 2015 57 1
16/09/2014 2015 18 2
17/09/2014 2015 16 3
14/09/2015 2016 10 1
15/09/2015 2016 45 2
16/09/2015 2016 28 3
12/09/2016 2017 32 1
13/09/2016 2017 11 2
14/09/2016 2017 68 3
24/08/2017 2018 23 1
25/08/2017 2018 53 2
26/08/2017 2018 13 3
What I'm trying to do is create an 'Year' Column Group to align them horizontally, but when I do that, this is the result:
result
Expected result:
expected result
Is this achievable in SSRS? I've tried removing the group =(Details), which gives me the desired result, except it only returns one line of information.
Any insight aprreciated.
By default, the Details group causes you to get one row per row in the dataset. In your case, I would suggest grouping the Rows by the Day column and create a column group by Year.
First, create the two groups and add columns inside the column group.
Then, add a row outside and above the Day row group. Place the headings here and then delete the top row. It should look like this:
Now these 4 columns will repeat to the right for each year and you will get rows based on the number of days in your dataset.
I have two tables client and grouping. They look like this:
Client
C_id
C_grouping_id
Month
Profit
Grouping
Grouping_id
Month
Profit
The client table contains monthly profit for every client and every client belongs to a specific grouping scheme specified by C_grouping_id.
The grouping table contains all the groups and their monthly profits.
I'm struggling with a query that essentially calculates the monthly residual for every subscriber:
Residual= (Subscriber Monthly Profit - Grouping monthly Profit)*(average subscriber monthly profits for all months / average profits for all months for the grouping subscriber belongs to)
I have come up with the following query so far but the results seem to be incorrect:
SELECT client.C_id, client.C_grouping_Id, client.Month,
((client.Profit - grouping.profit) * (avg(client.Profit)/avg(grouping.profit))) as "residual"
FROM client
INNER JOIN grouping
ON "C_grouping_id"="Grouping_id"
group by client.C_id, client.C_grouping_Id,client.Month, grouping.profit
I would appreciate it if someone can shed some light on what I'm doing wrong and how to correct it.
EDIT: Adding sample data and desired results
Client
C_id C_grouping_id Month Profit
001 aaa jul 10$
001 aaa aug 12$
001 aaa sep 8$
016 abc jan 25$
016 abc feb 21$
Grouping
Grouping_id Month Profit
aaa Jul 30$
aaa aug 50$
aaa Sep 15$
abc Jan 21$
abc Feb 27$
Query Result:
C_ID C_grouping_id Month Residual
001 aaa Jul (10-30)*(10/31.3)=-6.38
... and so on for every month for avery client.
This can be done in a pretty straight forward way.
The main difficulty is obviously that you try to deal with different levels of aggregation at once (average of the group and the client as well as the current record).
This is rather difficult/clumsy with simple SELECT FROM GROUP BY-SQL.
But with analytical functions aka Window functions this is very easy.
Start with combining the tables and calculating the base numbers:
select c.c_id as client_id,
c.c_grouping_id as grouping_id,
c.month,
c.profit as client_profit,
g.profit as group_profit,
avg (c.profit) over (partition by c.c_id) as avg_client_profit,
avg (g.profit) over (partition by g.grouping_id) as avg_group_profit
from client c inner join grouping g
on c."C_GROUPING_ID"=g."GROUPING_ID"
and c. "MONTH" = g. "MONTH";
With this you already get the average profits by client and by grouping_id.
Be aware that I changed the data type of the currency column to DECIMAL (10,3) as a VARCHAR with a $ sign in it is just hard to convert.
I also fixed the data for MONTHS as the test data contained different upper/lower case spellings which prevented the join to work.
Finally I turned all column names into upper case to, in order to make typing easier.
Anyhow, running this provides you with the following result set:
CLIENT_ID GROUPING_ID MONTH CLIENT_PROFIT GROUP_PROFIT AVG_CLIENT_PROFIT AVG_GROUP_PROFIT
16 abc JAN 25 21 23 24
16 abc FEB 21 27 23 24
1 aaa JUL 10 30 10 31.666
1 aaa AUG 12 50 10 31.666
1 aaa SEP 8 15 10 31.666
From here it's only one step further to the residual calculation.
You can either put this current SQL into a view to make it reusable for other queries or use it as a inline view.
I chose to use it as a common table expression (CTE) aka WITH clause because it's nice and easy to read:
with p as
(select c.c_id as client_id,
c.c_grouping_id as grouping_id,
c.month,
c.profit as client_profit,
g.profit as group_profit,
avg (c.profit) over (partition by c.c_id) as avg_client_profit,
avg (g.profit) over (partition by g.grouping_id) as avg_group_profit
from client c inner join grouping g
on c."C_GROUPING_ID"=g."GROUPING_ID"
and c. "MONTH" = g. "MONTH")
select client_id, grouping_id, month,
client_profit, group_profit,
avg_client_profit, avg_group_profit,
round( (client_profit - group_profit)
* (avg_client_profit/avg_group_profit), 2) as residual
from p
order by grouping_id, month, client_id;
Notice how easy to read the whole statement is and how straight forward the residual calculation is done.
The result is then this:
CLIENT_ID GROUPING_ID MONTH CLIENT_PROFIT GROUP_PROFIT AVG_CLIENT_PROFIT AVG_GROUP_PROFIT RESIDUAL
1 aaa AUG 12 50 10 31.666 -12
1 aaa JUL 10 30 10 31.666 -6.32
1 aaa SEP 8 15 10 31.666 -2.21
16 abc FEB 21 27 23 24 -5.75
16 abc JAN 25 21 23 24 3.83
Cheers,
Lars
Im trying to figure out the total for the quarter when the only data shown is a running total for the year:
Id Amount Periods Year Type Date
-------------------------------------------------------------
1 65 2 2014 G 4-1-12
2 75 3 2014 G 7-1-12
3 25 1 2014 G 1-1-12
4 60 1 2014 H 1-1-12
5 75 1 2014 Y 1-1-12
6 120 3 2014 I 7-1-12
7 30 1 2014 I 1-1-12
8 90 2 2014 I 4-1-12
In the data shown above. The items in type G and I are running totals for the period (in qtrs). If my query returns period 3, is there a sql way to get the data for the qtr? The math would involve retrieving the data for the 3rd period - 2nd period.
Right now my sql is something like:
SELECT * FROM data WHERE Date='4-1-12';
In this query, it will return row #1, which is a total for 2 periods. I would like it to return just the total for the 2nd period. Im looking to make this happen with SQLite.
Any help would be appreciated.
Thank alot
You want to subtract the running total of the previous quarter:
SELECT Id,
Year,
Type,
Date,
Amount - IFNULL((SELECT Amount
FROM data AS previousQuarter
WHERE previousQuarter.Year = data.year
AND previousQuarter.Type = data.Type
AND previousQuarter.Periods = data.Periods - 1
), 0) AS Amount
FROM data
The IFNULL is needed to handle a quarter that has no previous quarter.
I have been working on this practice problem for SQL class for the past 30 minutes. I am having a hard time including rows that have NULL values in it or a numerical value of 0. I will post the question and then the query that I have written:
"Write a query to display the tour name, outing date, and number of registered clients for each
outing of that tour on each date. Include only outings that were scheduled to occur after
October 27, 2013. Include tours with no outings and outings with no registered clients. Sort the result by the number of clients in descending order, and then by outing date in ascending order."
SELECT TOUR_NAME,OUT_DATE,Count(DISTINCT CLIENT_NUM) AS "Num Clients"
FROM TOUR RIGHT JOIN OUTING USING (TOUR_ID) JOIN REGISTER USING (OUT_ID)
WHERE To_Char(OUT_DATE,'YYYY-MM-DD') > '2013-10-27'
GROUP BY TOUR_NAME,OUT_DATE
ORDER BY "Num Clients" DESC,OUT_DATE;
I cannot figure out how to pull rows with empty cells. It currently only pulls complete rows.
-EXPECTED RESULTS:
--TOUR_NAME --OUT_DATE --Num Clients
Weekend Weekday 29-OCT-13 26
Downtown 28-OCT-13 25
Deluxe Day Away 28-OCT-13 23
Quick Break 30-OCT-13 19
Downtown 27-OCT-13 18
Downtown 30-OCT-13 18
Deluxe Day Away 31-OCT-13 12
Washington Heights 31-OCT-13 10
Weekend Weekday 13-NOV-13 0
Downtown 14-NOV-13 0
Beltway 15-NOV-13 0
Weekend Weekday 15-NOV-13 0
Quick Break 16-NOV-13 0
Power Shots 0
Perfect Endings 0
Primary Point 0
MY ACTUAL RESULTS:
TOUR_NAME OUT_DATE Num Clients
Weekend Weekday 29-OCT-13 26
Downtown 28-OCT-13 25
Deluxe Day Away 28-OCT-13 23
Quick Break 30-OCT-13 19
Downtown 27-OCT-13 18
Downtown 30-OCT-13 18
Deluxe Day Away 31-OCT-13 12
Washington Heights 31-OCT-13 10
It's not including any rows that has a null value or a zero count value within that row.
I appreciate any help. Thank you.