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.
Related
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]")
In MS Access 2010, assume a query contains the following results:
Date Activity Hours
1.9. Reading 1
1.9. Writing 2
2.9. Reading 1
3.9. Talking 1
4.9. Reading 3
1.10. Talking 2
1.10. Writing 1
2.10. Reading 2
3.10. Talking 2
4.10. Reading 1
the Report should show the sum of hours spent each month grouped by activity, something like
Month Activity Hours
September Reading 5
Writing 2
Talking 1
October Reading 3
Writing 1
Talking 4
using the wizard I mange to get a report which looks like
Month Activity Hours
September Reading 1
Reading 1
Reading 3
Writing 2
Talking 1
October Reading 2
Reading 1
Writing 1
Talking 2
Talking 2
which is nearly what I'd like to have but.. ?
I tried =sum(Hours), but then all Hours fields simply contain the total sum of all hours, and I still get several lines for the same activity.
Actually the report is more complicated. In a form prior to the report, the user can enter a date range (e.g. September 1 - October 30). The report should sum over the whole time range and not break up to months, that is:
Date range Activity Hours
9/1 - 10/30 Reading 8
Writing 3
Talking 5
Create a query with a SQL like this:
SELECT MonthName(Month([Date])), Activity, SUM(Hours)
FROM yourTable
WHERE [Date] >= xxx AND [Date] <= yyy
GROUP BY MonthName(Month([Date])), Activity
The key element is the GROUP BY clause, it sums the Hours per Month + Activity.
Then base your report on that query.
BTW, it is not a good idea to call a column "Date", it's a reserved word in Access.
I'm not a db expert. I'm just working on a project where we need to show page views on daily basis between two dates selected by a user from a calendar. I have the following
SQL query that brings brings total number of page views like
SELECT DATE_FORMAT(created_at,'%d %b %Y') as this_date, count(*) as Views
from promotion_insights WHERE f_id = '2' AND p_id = '12'
AND created_at BETWEEN '2012-08-15' AND '2012-08-19'
GROUP BY this_date
Result comes like
----------------------
this_date View
---------------------
15 Aug 2012 3
16 Aug 2012 2
----------------------
I have also a calendar table and a store procedure.calendar table has one column of dates named (datefield). I have already generated dates by calling a stored procedure so don't worry about that. Now what I want, is to make a right join on date basis to above table having (this_date and view columns) with calendar table to show all_dates between selected by the user and put 0 for the dates having 0 views.
count(*) is also making trouble by returing 1 instead 0
Expected output by the join I want is like this:
----------------------
this_date View
---------------------
15 Aug 2012 3
16 Aug 2012 2
17 Aug 2012 0
18 Aug 2012 0
19 Aug 2012 0
----------------------
Any help would be highly highly appreciated.
You can readily do this with a calendar table -- a good idea in most databases.
SELECT DATE_FORMAT(c.date,'%d %b %Y') as this_date, count(*) as Views
from calendar c left outer join
promotion_insights pi
on c.date between '2012-08-15' AND '2012-08-19' and
c.date = pi.created_at
WHERE f_id = '2' AND p_id = '12'
GROUP BY c.date
Note: this assumes that created_at is stored as a date, not a date time. Having a time component could throw off the comparisons.
If you want to do this in SQL you need a row generator to generated a row for each day in your date range, and then join your query to the generated rows to get results for each day in you date range (even if there is no data present for some days in the range).
If you are using Oracle you can use ALL_OBJECTS or DUAL to generate rows. As you are using DATE_FORMAT in your query you seem to use MySQL. A question on how to make a row generator in MySQL was posted before. It states:
Hate to say this, but MySQL is the only RDBMS of the big four that doesn't have this feature.
I am trying to develop an access based database for a small company. I have made few tables, some of them are the "2011/2012 Total Production in $" and ""2011/2012 Total Production in CY."
All four tables have a column "Actual Production"
Now, using query tool, i have to achieve two objectives.
get the maximum and minimum production of each month in a year
display the date of when the Actual Production was maximum/minimum in the month
I have accomplished first task which was simple; Make a query, get relevant fields, Summary Max & Min and finally distrubute in month wise. I.e Jan Max 5000, Min 2000...Feb Max 6000 Min 1000
Now what i desire is to display the date of max or min production for the month. So if April had MAX 181,218.00 in its month, I want it to display the date when it occurred (i,e April 10th 2012)
I am a beginner with Access, so please be as simple as possible.
Sample data gleaned from comment:
Month Sum of Prod Min Prod in $ Max Prod in $
------------- ------------- ------------- -------------
January 2011 $1,184,096.98 $20,486.40 $171,470.40
February 2011 $1,558,072.20 $44,962.20 $116,359.20
March 2011 $1,744,442.19 $19,200.00 $141,065.10
April 2011 $1,698,608.63 $27,500.70 $181,218.00
May 2011 $1,826,915.38 $37,996.00 $130,066.00
June 2011 $2,317,890.71 $42,645.00 $144,323.30
The above data were few of the fields gnerated by Query.
What I am looking for is
Month Date of Min Prod Min Prod in $ Date of Max Prod Max Prod in $
------------- ------------- ------------- ------------- -------------
January 2011 Jan 15 $20,486.40 Jan 10 $171,470.40
February 2011 Feb 20 $44,962.20 Feb 27 $116,359.20
March 2011 March 10 $19,200.00 March 1 $141,065.10
and so forth.
Is it possible to use a query to generate this result?
thanks!
Relation to the comment.
Relevant fields in one of the tables are.
Date of Activity Actual Production
------------- ------------- -------
1/3/2012 $20,486.40
1/4/2012 $44,962.20
1/5/2012 $19,200.00
I got lost in the details of your question. So I'll show you sample data and queries to get something like what I hope you want from that data.
Here is the contents of tblAbbas.
activity_date actual_production
1/3/2012 $20,486.40
1/4/2012 $44,962.20
1/5/2012 $19,200.00
2/1/2012 $3.00
2/2/2012 $2.00
2/3/2012 $1.00
Here is the SQL for a query named qryMonthStart. The purpose of this query is to determine the first day of the month which includes the activity_date.
SELECT
DateSerial(Year(activity_date),Month(activity_date),1)
AS month_start,
activity_date,
actual_production
FROM tblAbbas;
The query below uses qryMonthStart as its data source and gives me this result set.
month_year SumOf_production min_prod_date MinOf_production max_prod_date MaxOf_production
January 2012 $84,648.60 1/5/2012 $19,200.00 1/4/2012 $44,962.20
February 2012 $6.00 2/3/2012 $1.00 2/1/2012 $3.00
And the query SQL ...
SELECT
Format(grpby.month_start,"mmmm yyyy") AS month_year,
grpby.SumOf_production,
qmin.activity_date AS min_prod_date,
grpby.MinOf_production,
qmax.activity_date AS max_prod_date,
grpby.MaxOf_production
FROM
(
(
SELECT
month_start,
Sum(actual_production) AS SumOf_production,
Min(actual_production) AS MinOf_production,
Max(actual_production) AS MaxOf_production
FROM qryMonthStart
GROUP BY month_start
) AS grpby
INNER JOIN qryMonthStart AS qmin
ON
(grpby.MinOf_production = qmin.actual_production)
AND (grpby.month_start = qmin.month_start)
)
INNER JOIN qryMonthStart AS qmax
ON
(grpby.MaxOf_production = qmax.actual_production)
AND (grpby.month_start = qmax.month_start)
ORDER BY grpby.month_start;
Beware that query will fail ("data type mismatch in criteria expression") if you have Null for activity_date. The simplest way to prevent that is to clean out the Nulls then prohibit them in the activity_date column (set the Required property to Yes in table design view). If you decide you must allow Nulls in activity_date, you've got more work ahead.
Also note that query will give you multiple rows for the same month_year if the actual_production values in more than one of the daily records for that month matches the monthly minimum (MinOf_production). And the same situation will apply for the monthly maximum (MaxOf_production).
I need to display a history all our Products we've sold by day, week, month and year. This data will be sent to google charts API to display a line graph of the results.
So if i have a table called Products and it looks like:-
Products
ProductID INT
DateCreated DATETIMEOFFSET
and the user asks to see the history for ProductID 1. How could i retrieve this?
eg output.
Graph 1 (Dates vs Sale Count)
Monday 1st Dec: 0
Tuesday 2nd Dec: 3
Wed 3rd Dec: 1
Graph 2 (Weeks vs Sale Count)
Week 49 2008: 21
Week 50 2008: 11
Week 51 2008: 45
Week 52 2008: 0
Graph3 (Months vs Sale Count)
Dec 08: 102
Jan 09: 23
I'm not sure if the 'by day' can be done ... or any of it.
cheers :)
Update 1 : got part of it working...
After spending a bit of time, i got the first one working... but still need help on the other two AND making it all part of one query...
from p in Products
where p.ProductId == 69
group p.DateCreated by p.DateCreated.Date into grouping
orderby grouping.Key
select new { Date = grouping.Key, Count = grouping.Count() }
var data = from p in ctx.Products
where p.ProductID == *productId*
group p by p.DateCreated.DayOfWeek into groupedProducts
select new { DayOfWeek = groupedProducts.Key, Count = groupedProcuts.Count() };
Without testing I think that may do it for you
To do it per-year something like this:
var data = from p in ctx.Products
where p.ProductID == *productId*
group p by n.CreateDate.Year into gn
select new {
Count = from a in gn
group a by a.CreateDate.DayOfYear into aa
select new { Count = aa.Count(), Key = new DateTime(gn.Key , 1, 1).AddDays(aa.Key) }
};
(Sorry about the variable names :P)
I don't know how to do it with a LINQ expression, but you could write a stored procedure using the PIVOT operator. See Using PIVOT and UNPIVOT.
Use DateTime.Date, DateTime.Month and DateTime.Year. However there is a bug in LINQ-to-SQL that sometimes translates those to invalid SQL. As a workaround you can group by SqlMethods.DateDiffDay, SqlMethods.DateDiffMonth and SqlMethods.DateDiffYear from say '1980-01-01'.
The week part is not so easy. Start of the week depends on Sql Server settings so it's not reliable. Alternatively you can use SqlMethods.DateDiffDay from some date that starts the week in your case and divide day difference by 7. If you need the week number in a given year you should calculate day difference from the most recent week start day that is less or equal to start of the year.