SQL statement, get query RSQLite - mysql

I am new to SQL and having trouble with RSQLite.
Here is an example of my table:
counts Month
0 June
4 June
2 March
5 July
3 July
I would like to create a search query using dbGetQuery that will count up the number of counts for each month from my totals table. Im looking for output that looks like this:
counts Month
4 June
2 March
8 July
So far I have this but is incorrect. dbGetQuery(conn=db, "SELECT Count(counts) FROM totals group by [Month]")

From your desired output seems that you don't want to count the counts but to sum them. As stated by #AlexK, you also need Month in your select to have it displayed along with the sum result
dbGetQuery(conn=db, "SELECT [Month], sum(counts) as counts FROM totals group by [Month]")

Related

Counting unique records by month

I'm a newbie to Access 2010. I have a table:
ID Mth OrderID Net Sales
1 1 3 36
2 1 2 12
3 1 2 20
4 2 1 10
I'd like to get a summary by Mth of the OrderID count, Quantity of those orders, and Net Sales of the those orders:
Mth Ordercount Quantity Net Sales
1 2 7 68
2 1 1 10
Is there a way to do this?
I'd also like to convert Mth = 1 into Month = Jan 2013 but have it list in date order, rather than alphabetically.
Mth
Jan 2013
Feb 2013
How do I do that?
So far, I've only been working with the design view and have not using an SQL code.
This can be done mostly in the design viewer of access although it would require creating more than one query and using those as a source instead of a table or you could write a sub select in sql code.
For your first question you will need to perform a distinct count on order id's based on month. This question answers the same problem and will provide the output you need.
Once you have a query that provides the number of orders per month you can create a new query that joins the table and your query on month with Net Sales as a total field. Where is quantity coming from in your source data?
To display the month number as month access has a MonthName function you can use. You can add 2013 to this by adding & " 2013" to the end of the expression.
You can sort on month by adding your month field a second time for the sorting but uncheck show box.

Grouping months by quarter over multiple years depending on a dynamic start month

Using MySQL and PHP I am building a JSON array to populate a data table.
For the purposed of my question suppose the data table has the following columns:
Year: 2010,2011,2012,2013...<br/>
Month: 1,2,3,4,5...<br/>
Value: 100, 150, 200 etc...<br/>
The table structure cannot be altered and my solution needs come into the MySQL query
The data can be viewed either monthly, quarterly or yearly. Monthly and yearly is achieved easily through grouping by year and month.
Quarterly data can be grouped by calendar quarter (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec) by this group statement:
GROUP BY year, round((month/3)+0.3,0)
So where Jan, Feb and March might all have 100 for their value the summed result is 300, same for other months.
Now my problem comes when I want to group values by a financial quarter, for example a quarter that starts in Feb, or any other quarters.
I have a statement that works for the quarter grouping using two variables that can be accessed via the SQL query, start_year (i.e. 2014) and start_month (i.e. 2)
GROUP BY year, (((round(((((month-(start_month-1))+((year-start_year)*12))-((year-start_year)*12))/3)+0.33,0)/4)+4)-floor(((round(((((month-(start_month, '%m')-1))+((year-start_year)*12))-((year-start_year*12))/3)+0.33,0)/4)+4)))*12
which basically will assign a 0,3,6,9 value to each calendar month for the purposes of grouping.
In the financial year starting February this works fine for quarters 1-3, however breaks for the final quarter as it includes Nov and Dec 2014 data and Jan from 2015.
As a result I get 5 rows of data instead of 4.
This is because of the preceding GROUP by year clause, an important addition as we might want to generate a table that views sequential quarters for multiple years.
So what I am looking for is a way of grouping the years together by offsetting the start month.
So when the year starts in Jan it will group Jan-Dec but if we change that to starting Feb it will group Feb-Jan.
Any ideas, suggestions most welcome!
Regards,
Carl
I solved a similar problem just now (a Moodle report aggregating assignment scores by year and quarter) with something like this:
select year(from_unixtime(s.timemarked)) as year, quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter, count(distinct data1) as "tickets graded" from mdlassignment_submissions s where grade >= 0 group by year, quarter order by year, quarter;
The relevant part for what you're doing is quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter
As another commenter pointed out, MySQL has a quarter() function, but it doesn't do financial quarters. However, since (as I understand it, at least, based on consulting the relevant wikipedia page) financial quarters are just offset by 1, the % 4 + 1 at the end should convert it.

MySQL, How can I count the amount something happens in a month, and display the sum beside month names?

I need to display 2 columns. First column should have the month name. Second column should show me how many times something was released within each month. e.g:
Month number_of_releases
January 4
March 9
December 2
So far, I have this:
SELECT DISTINCT MONTHNAME(date) AS 'Month',
/*Here is where I need help!*/ AS 'number_of_releases'
FROM table_name;
Without knowing how you are calculating number_of_releases, it's hard to say for certain... but you probably want to group your table by monthname and use a suitable aggregate function to yield the number of releases.
For example, to obtain a count of the number of records within each month:
SELECT MONTHNAME(date) AS Month, COUNT(*) AS number_of_releases
FROM table_name
GROUP BY Month

Date ranking in Access SQL?

I have a query pulling the last six months of data from a table which has a column, UseDates (so as of today in June, this table has dates for December 2011 through May 2012).
I wish to include a "rank" column that associates a 1 to all December dates, 2 to all January dates, etc -- up to 6 for the dates corresponding one month prior. If I were to open up this query a month from now, the 1 would then be associated with January, etc.
I hope this makes sense!
Example, if I ran the query right now
UseDate Rank
12/31/2011 1
1/12/2012 2
...
5/23/2012 6
Example, if I ran the query in August:
UseDate Rank
2/16/2012 1
3/17/2012 2
...
7/21/2012 6
Example, if I ran the query in March:
UseDate Rank
9/16/2011 1
10/17/2011 2
...
2/24/2012 6
SELECT
UseDates,
DateDiff("m", Date(), UseDates) + 7 AS [Rank]
FROM YourTable;
You can use month function for UseDates and subtract it from the result of now function. If it goes negative, just add 12. Also you may want to add 1 since you start with 1 and not 0. Apparently it should work for half a year date ranges. You'll get into trouble when you need to "rank" several years.
You can rank with a count.
SELECT
Table.ADate,
(SELECT Count(ADate)
FROM Table b
WHERE b.ADate<=Table.ADate) AS Expr1
FROM Table3;
You have to repeat any where statement in the subquery:
SELECT
Table.ADate,
(SELECT Count(ADate)
FROM Table b
WHERE b.ADate<=Table.ADate And Adate>#2012/02/01#) AS Expr1
FROM Table3
WHERE Adate>#2012/02/01#

MSAccess - Design query to group by anniversary date

Lets say I have a table with these fields
LeaveDate
LeaveType
I want to write a query that groups by an annivesary date.
For example say 8th Feb.
So for this year any dates after 8 Feb would be "2010" and any dates before 8 Feb would show "2009".
I want this to occur for all years data.
Understand??
Malcolm
Here is how I did it
SELECT Year([tblFoo]![Leave_date])-IIf(DateDiff("d",[tblFoo]![Leave_date],DateSerial(Year([tblFoo]![Leave_date]),2,8))>0,1,0) AS Year_group, Count(tblFoo.ID) AS CountOfID
FROM tblFoo
GROUP BY Year([tblFoo]![Leave_date])-IIf(DateDiff("d",[tblFoo]![Leave_date],DateSerial(Year([tblFoo]![Leave_date]),2,8))>0,1,0);
This counts the number of records for each “Year”. We use something similar for working out birthday years which change from person to person. In that case use can just replace the fixed 2 and 8 with the month and day they were born
You could create a query with a calculated column for anniversary date, then group by that column.