"Maureen" wrote
tblInventory + tblInvoiceDetail
I would like to collect sales history for analysys. I'd like sales for
various periods (based on tbleInvoiceDetail.date) for each SKU in inventory
M-last 30 days
Q-last 90 days
X-last 180 days
Y-last 366 days
L-Between day 366 and 732
Z-Life to date
I would also like to do the same for tblCustomer + tblInvoiceDetail
collecting the sales history for each customer
Hi Maureen,
Will start with "simple case" where SKU
is also a field in tblInvoiceDetail.
--------------
Z-Life to date
--------------
Click on "Create Query in Design View"
In the Show Table dialog box,
click on tblInvoiceDetail,
click Add,
and then click Close.
Right-mouse click on your table
and choose Properties.
In the Alias row, type in
t1
then close the Properties dialog box.
Drag and drop "SKU" from your table
down to a field row in the lower grid.
Select "Ascending" in the "Sort" row
for this column.
Drag and drop "Qty" from your table
down to a field row in the next column
of the grid.
Change your SELECT query to a groupby
by clicking on the sigma icon in top menu.
In "Qty" column of grid, change "Group By"
to "Sum"
Save query (say "qrySalesLifeToDateBySKU").
In SQL View, your query might look like:
SELECT
t1.SKU,
SUM(t1.Qty) AS [Sum of Qty]
FROM
tblInvoiceDetail AS t1
GROUP BY t1.SKU
ORDER BY t1.SKU;
If your tblInvoiceDetail had a field that gave
total cost in currency (like "ExtCost"), you
could add this field and sum over it as well.
SELECT
t1.SKU,
SUM(t1.Qty) AS [Sum of Qty],
SUM(t1.ExtCost) AS [Sum of ExtCost]
FROM
tblInvoiceDetail AS t1
GROUP BY t1.SKU
ORDER BY t1.SKU;
If you only get a price for the SKU
(say "Cost"), then you could sum over
the expression (Qty*Cost). In the field
row of a column of your grid, type in
something like:
SumExtCost: t1.Qty*t1.Cost
and set its Total row to Sum.
SELECT
t1.SKU,
SUM(t1.Qty) AS [Sum of Qty],
SUM(t1.Qty*t1.Cost) AS SumExtCost
FROM
tblInvoiceDetail AS t1
GROUP BY t1.SKU
ORDER BY t1.SKU;
You don't have to settle for alias that
Access autogenerates for your "Sum"..
in SQL View, you can change to something
like:
SELECT
t1.SKU,
SUM(t1.Qty) AS TotalSKUQty,
SUM(t1.Qty*t1.Cost) AS SumExtCost
FROM
tblInvoiceDetail AS t1
GROUP BY t1.SKU
ORDER BY t1.SKU;
--------------
M-last 30 days
--------------
All the other "by SKU" queries will start
the same as above, but you need to bring
"tblInvoiceDetail.date" down into a field row
of another column of your grid.
"Date" is an Access reserved word, so it would
be best if you can change its name in the table to
something like "InvDate"...if you cannot, always
remember to enclose it within brackets in queries.
But it is a "gotcha" waiting to happen. In the following
I am going to refer to it as "InvDate"....
In Total row for InvDate, change "Group By" to
"Where"
Then in its "Criteria" row, type in
So your SQL might look like:
SELECT
t1.SKU,
SUM(t1.Qty) AS TotalSKUQty,
SUM(t1.Qty*t1.Cost) AS SumExtCost
FROM
tblInvoiceDetail AS t1
WHERE ((tbl1.InvDate)>=Date()-30)
GROUP BY t1.SKU
ORDER BY t1.SKU;
-----------
Q-last 90 days
-----------
same as above, but Criteria row might look like:
"X" and "Y" above would be similar except
use 180 and 366 respectfully, instead of 90.
---------------
L-Between day 366 and 732
---------------
same as above, but Criteria row might look like:
=Date()-732 And <Date() - 365
or
BETWEEN Date()-732 AND Date()-366
////////////////tblCustomer + tblInvoiceDetail////////////////
Click on "Create Query in Design View"
In the Show Table dialog box,
click on tblCustomer,
click Add,
click on tblInvoiceDetail,
click on Add again,
and then click Close.
You should now show your 2 tables
in the query designer.
Right-mouse click on tblCustomer
and choose Properties.
In the Alias row, type in
c1
then close the Properties dialog box.
Right-mouse click on the tblInvoiceDetail table
and choose Properties.
In the Alias row, type in
t1
then close the Properties dialog box.
If you do not get a "correct" join line
between the 2 tables (its possible for
Access to assume wrongly, so you
may first have to right-mouse click
on a join line and choose delete).
Then click and hold down on c1 table's
join field (possibly "CustomerID")
and "drag and drop"
over on t1 table's join field
(possibly also "CustomerID").
If, in your query, you want to show
*all* Customers even if they do not
show up in the invoice detail table,
right-mouse click on the join line,
choose properties, and select the
option to show all Customers.
Bring "CustomerName" (or however
you identify a distinct Customer) down
into field row.
Change to Group By query.
The rest is similar to above depending
how you want to define a group and
what you want to sum, and over what
dates. For example:
SELECT
c1.CustomerName,
SUM(t1.Qty) AS TotalCustQty,
SUM(t1.Qty*t1.Cost) AS SumExtCost
FROM
tblCustomer AS c1
INNER JOIN
tblInvoiceDetail AS t1
ON c1.CustomerID = t1.CustomerID
GROUP BY c1.CustomerName
ORDER BY c1.CustomerName;
or all Customers:
SELECT
c1.CustomerName,
SUM(NZ(t1.Qty)) AS TotalCustQty,
SUM(NZ(t1.Qty)*NZ(t1.Cost)) AS SumExtCost
FROM
tblCustomer AS c1
LEFT JOIN
tblInvoiceDetail AS t1
ON c1.CustomerID = t1.CustomerID
GROUP BY c1.CustomerName
ORDER BY c1.CustomerName;
or all Customers last 30 days:
SELECT
c1.CustomerName,
SUM(NZ(t1.Qty)) AS TotalCustQty,
SUM(NZ(t1.Qty)*NZ(t1.Cost)) AS SumExtCost
FROM
tblCustomer AS c1
LEFT JOIN
tblInvoiceDetail AS t1
ON c1.CustomerID = t1.CustomerID
WHERE ((tbl1.InvDate)>=Date()-30)
GROUP BY c1.CustomerName
ORDER BY c1.CustomerName;
////////// tblInventory + tblInvoiceDetail////////////
Getting *all* SKU's from tblInventory
would be similar to above.
SELECT
t2.SKU,
SUM(NZ(t1.Qty)) AS TotalSKUQty,
SUM(NZ(t1.Qty)*NZ(t1.Cost)) AS SumExtCost
FROM
tblInventory AS t2
LEFT JOIN
tblInvoiceDetail AS t1
ON t2.SKU = t1.SKU
GROUP BY t2.SKU
ORDER BY t2.SKU;
or all SKU's last 30 days:
SELECT
t2.SKU,
SUM(NZ(t1.Qty)) AS TotalSKUQty,
SUM(NZ(t1.Qty)*NZ(t1.Cost)) AS SumExtCost
FROM
tblInventory AS t2
LEFT JOIN
tblInvoiceDetail AS t1
ON t2.SKU = t1.SKU
WHERE ((tbl1.InvDate)>=Date()-30)
GROUP BY t2.SKU
ORDER BY t2.SKU;
Well...I've assumed alot which may be
wrong, but I think you might get the general
gist of this.
Please respond back if I have misunderstood
or was not clear about something.
(I will be "away" for a few days though)
Good luck,
Gary Walter