Sorting Month And Year Results In Microsoft Access - sql

I have the following table in Microsoft Access
TransactionDate
Market
Details
Opening
Closing
Size
Profit/Loss
I want to run a query that shows the Profit/Loss for each month.
I have been able get a query that returns the information in the following format
TransactionDate By Month Sum Of Sum Of Profit/Loss
April 2014 €1,084.99
April 2015 €674.33
April 2016 €2,057.30
August 2014 €237.59
August 2015 -€267.82
December 2014 €375.88
December 2015 -€1,161.97
February 2015 -€603.87
February 2016 -€124.71
January 2015 €75.11
January 2016 -€1,044.35
But what I want now is for it to display in chronological order as oppose to Alphabetical order.
For example
January 2014
February 2014
March 2014
etc.

I will consider that your TransactionDate field is defined as String
If you want to order by this text field in Access, you will have to use DateValue() function.
That would give:
SELECT TransactionDate FROM yourTable ORDER BY DateValue(TransactionDate)
If your field is already formatted as a Date field, then simply use order by TransactionDate to make it work.

Related

Sum of totals grouped by month

I've been out of the dev world for a few years so forgive me if this is a pretty basic question but I have an app that logs bookings for holiday accomodation. I want to produce a report detailing how much income per month a user gets.
My query thus far is as so:-
SELECT SUM(int_ToOwner) AS TotalIncome,
DateName(m,dtm_StartDate) AS BookingMonth
FROM tbl_Bookings
WHERE dtm_StartDate > '2021-12-31'
GROUP BY DatePart(m,dtm_StartDate), int_ToOwner, dtm_StartDate
But that produces the result below. I want it to give me a total for each month instead.
TotalIncome
BookingMonth
553.00
January
849.00
January
885.00
February
1236.00
February
1239.00
February
896.00
March
927.00
March
940.00
March
959.00
March
971.00
March
1167.00
April
1255.00
April
1500.00
April
2461.00
April
1131.00
May
1172.00
May
1275.00
May
2647.00
May
1466.00
June
1480.00
June
1496.00
June
1899.00
June
2167.00
June
1881.00
July
4990.00
July
4991.00
July
2134.00
August
4162.00
August
4883.00
August
5329.00
August
1430.00
September
1630.00
October
1130.00
November
You almost have it but you are also grouping by int_ToOwner and you have the dtm_StartDate twice.
Try:
SELECT SUM(int_ToOwner) AS TotalIncome, DateName(m,dtm_StartDate) AS BookingMonth
FROM tbl_Bookings
WHERE dtm_StartDate > '2021-12-31'
GROUP BY DatePart(m,dtm_StartDate)
A little re-format:
SELECT SUM(int_ToOwner) AS TotalIncome
, DateName(m,dtm_StartDate) AS BookingMonth
FROM tbl_Bookings
WHERE dtm_StartDate > '2021-12-31'
GROUP BY DatePart(m,dtm_StartDate)
, int_ToOwner
, dtm_StartDate
Your GROUP BY tells the database to create groups for data with equal values of
DatePart(m,dtm_StartDate)
int_ToOwner
dtm_StartDate
Then SELECT asks for each group the
calculated SUM of int_ToOwner
DateName(m,dtm_StartDate)
You should search your solution in grouping the correct attributes.

How to calculate Australian Financial Year from a date column in SQL?

I have a table called Sales with a date column called SaleDate. How can I write a SQL query that shows the Australian financial year in addition to the other columns?
More details:
I use Microsoft SQL Server
The Australian financial year starts on 1 July and ends the next year on 30 June.
Example 1: 10 June 2019 is FY 2019
Example 2: 5 July 2019 is FY 2020
For Microsoft SQL Server, the following query will show a new column called FY, which represents the Australian financial year.
SELECT
year(dateadd(MONTH, 6, SaleDate)) AS FY,
SaleDate,
Item
FROM Sales

Pivot Table adding "2" to value in Report Filter

I have a file with pivot tables where the filter values are updated on a monthly basis based on the last month's date. VBA code for this:
Dim SelectDate As String
SelectDate = Worksheets("Raw").Range("A3").Value
Worksheets("Main").PivotTables("PivotTable4").PivotFields("Date").CurrentPage = SelectDate
The value in cell A3 is string based on the following function:
=A1&" "&A2
For example: January 2017
Updating the pivot tables worked fine for several months, but for this month my values no longer update correctly and after doing some investigation I realized that my pivot table has created two entries in the filter for the month January 2017 and January 20172, while November 2016 went missing:
However, when I check my underlying data, I only have one value for each month:
June 2016
February 2016
March 2016
January 2016
November 2016
December 2016
August 2016
May 2016
October 2016
July 2016
September 2016
April 2016
January 2017
February 2017
Note that when I create an new Pivot table with the same data, the filter only shows January 2017.
Any suggestions why the filter for the existing pivot table would reflect both January 2017 and January 20172?
Thanks for your help!
Here is what worked for me. This happened from what I have read elsewhere either because I added now data lines or because I did a find-and-replace. But then I changed the area of the source data in the pivot table from say
'MASTER (3)'!$A$5:$EQ$600 to 'MASTER (3)'!$A$5:$EQ$6
and then back to
'MASTER (3)'!$A$5:$EQ$600
This resolved the problem

SQL Server 2012: How to order by year then month in chronological order

I have a simple query that averages numbers and breaks them down by year and month. The problem I am having is I can't find a way to order the results by year then month....in chronological order. Here is an example of my results;
Month Year AverageAmount
---------------------------------
April 2012 167582.1139
August 2012 206124.9323
December 2012 192481.8604
February 2012 227612.0485
January 2012 214315.2187
July 2012 195320.075
June 2012 196174.3195
March 2012 201199.9894
May 2012 190526.0571
November 2012 203441.5135
October 2012 216467.7777
September 2012 217635.9174
April 2013 206730.5333
August 2013 197296.0563
As you can see in the table above, the months are in alphabetical order... what I need is the results to be in chronological order, ie...
Month Year AverageAmount
----------------------------------
January 2012 214315.2187
February 2012 227612.0485
March 2012 201199.9894
April 2012 167582.1139
May 2012 190526.0571
June 2012 196174.3195
April 2012 206730.5333
July 2012 195320.075
August 2012 206124.9323
September 2012 217635.9174
October 2012 216467.7777
November 2012 203441.5135
December 2012 192481.8604
January 2013 187268.3027
February 2013 179755.0666
March 2013 200131.6533
Query:
SELECT
datename(month, col1) Month,
year(col2) Year,
avg(col3) AverageAmount
FROM
myDB
GROUP BY
datename(month, datefunded), year(datefunded)
ORDER BY
year(datefunded), datename(month, datefunded)
Any help would be greatly appreciated
Just use this ORDER BY:
ORDER BY
YEAR(datefunded), MONTH(datefunded)
That'll sort your data by the numerical values of year and month of that datefunded
Have a look at this answer: Convert month name to month number in SQL Server
Maybe that solves the sorting for you? You can use one of the suggestions to convert the month to a number, so that e.g. March > 3. Then you sort by that.

MS Access grouping query spanning start and end dates

I would like to get a running tally of how many widgets were/are rented at any one time, by month, by year. Data is held in an MS Access 2003 db;
Table name: rent_table
Fields:
rentid
startdate
enddate
rentfee
rentcost
bookingfee
Something like; Count number of rentid's that fall between month/year, then group them?
e.g. if a widget was rented from 5th Jan 2014 to 8th April 2014 it would appear as a count in Jan, Feb, Mar and April tally's.
Many thanks.
EDIT
More details (sorry);
Access db is fronted by classic ASP.
If possible I don't want to create any new tables.
No input is required in order to run the report.
There are around 350-400 widgets that could be rented at any one time.
Each widget is rented exclusively.
Report output example;
Month | Year | NumRented
Jan 2014 86
Feb 2014 113
...
Can a query pick up dates within dates? So literally do a count of the table where date >Dec 31st 2013 AND <1st Feb 2014 (to grab a count for all of January 2014) and would that include the example of the rent starting on the 5th Jan? So I could just do twelve counts for each year?
create a calendar table, e.g.
table = cal_yyyymm with one column dt_yyyymm as numeric field
populate the table with ... say 5 or 10 years of data
201401 201402 201403 ... 60 or 120 rows, a small table
make a sql
Select
dt_yyyymm,
count(*) as cnt
From cal_yyyymm
Left Join rent_table
On format(startdate,"yyyymm") >= dt_yyyymm
And dt_yyyymm >= format(enddate,"yyyymm")
think about the complications in the data -- --
widget was rented from 5th Jan 2014 to 8th Jan 2014
and again rented from 11th Jan 2014 to 21st Jan 2014
does this count at 1 or 2 in the month?
if it is 1, then the sql gets more complicated because
the rent_table first needs to have its dates converted
to yyyymm format, and second needs to be de-duped on rentid,
and third then joined to cal_ On the dates...