inventory summary Access - sql

Well after much messing about I have finally got a query that gives sales totals of all the products, and another that gives stock absorbed for all of the products, (images below).
I was looking at Allen Brown's stuff and I get it, but I was wondering If i could make a summary report covering all of the available stock for all of the products. there are 31 products in total and they all appear in the stock query and in the sales query.
http://imageshack.us/photo/my-images/810/87887.jpg
http://imageshack.us/photo/my-images/827/8787j.jpg
Any Ideas on the coding to use...
I guess it would be possible to do, but then I don't really know where to start
I really want to make a report which will summarise the stock of each of the products in the queries.
Instead of having a button for working the stock of one specific product, having a button that will work the stock for each of the products in the queries at the same time.
does this make sense?
Thanks
Sam
UPDATE
this is the query for finding stock
SELECT TblStock.ProductID, Sum(TblStock.StockLevel) AS Stock, TblProduct.Item
FROM TblStock INNER JOIN TblProduct ON TblStock.ProductID = TblProduct.ProductID
GROUP BY TblStock.ProductID, TblProduct.Item;
this is the query for finding quantity of sales
SELECT TblProduct.Item, Sum(TblTotalSale.Size) AS Quantity, TblProduct.ProductID
FROM TblProduct INNER JOIN TblTotalSale ON TblProduct.[ProductID] = TblTotalSale.[ProductID]
GROUP BY TblProduct.Item, TblProduct.ProductID;
TblStock looks like
StockID ProductID StockLevel
89 32 200
90 33 72
91 34 72
92 1 528
93 3 528
94 5 528
95 9 528
96 7 528
97 18 80
98 30 72
99 31 204
Product Table looks like
ProductID Item Price StockDelivery PriceSmall Large Small
1 Carling £2.50 528 £1.40 2 1
3 Carlsburg £2.70 528 £1.60 2 1
5 IPA £2.30 528 £1.20 2 1
7 StrongBow £2.80 528 £1.65 2 1
9 RevJames £2.45 528 £1.30 2 1
11 Becks £2.90 72 1
12 WKDBlue £2.80 72 1
13 WKDRed £2.80 72 1
14 SmirnoffIce £2.80 72 1
15 KoppaburgPear £3.10 72 1
16 KoppaburgSum £3.10 72 1
17 Bulmers £2.90 72 1
18 Vodka £1.60 80 1
19 Gin £1.40 80 1
20 Sherry £1.40 80 1
21 Sambuca £1.70 80 1
22 Rum £1.60 80 1
23 Port £1.60 80 1
24 Whiskey £1.60 80 1
25 Baileys £1.60 80 1
26 Jagermeister £1.50 80 1
27 Martini £1.60 80 1
28 CokeCan £0.85 72 1
29 Coke £1.30 204 £0.30 2 1
30 LemonadeCan £0.85 72 1
31 Lemonade £1.30 204 £0.30 2 1
32 Squash £0.25 200 1
33 Tonic £0.85 72 1
34 RedBull £1.90 72 1
35 Nuts £0.60 70 1
36 Crisps £0.60 70 1
tbltotalSale looks like
TotalSalesID ProductID SalePrice Day Time Size
370 1 £2.50 05/02/2012 19:53:14 2
371 1 £1.40 05/02/2012 19:53:14 1
372 1 £2.50 05/02/2012 19:53:14 2
373 1 £1.40 05/02/2012 19:53:14 1
374 1 £2.50 05/02/2012 20:25:12 2
375 1 £1.40 05/02/2012 20:25:12 1
376 1 £2.50 05/02/2012 20:25:12 2
377 1 £1.40 05/02/2012 20:25:12 1
378 1 £2.50 05/02/2012 20:25:12 2
379 1 £2.50 05/02/2012 20:25:12 2
380 1 £1.40 05/02/2012 20:25:12 1
381 5 £2.30 05/02/2012 20:25:12 2
382 5 £2.30 05/02/2012 20:25:12 2
383 5 £1.20 05/02/2012 20:25:12 1
384 7 £2.80 05/02/2012 20:25:12 2
385 7 £1.65 05/02/2012 20:25:12 1
386 7 £1.65 05/02/2012 20:25:12 1
387 9 £1.30 05/02/2012 20:25:12 1
435 11 £2.90 05/02/2012 20:25:12 1
436 11 £2.90 05/02/2012 20:42:49 1
437 11 £2.90 05/02/2012 20:42:49 1
I can upload my database if that would be easyer.
have tried to use the following Query for what i want, but it returns 11 results for each product id and none that are correct....
SELECT QrySaleTot.Item, QrySaleTot.ProductID, [QryStockLevel].[Stock]-[QrySaleTot].[Quantity] AS StockOnHand
FROM QrySaleTot, QryStockLevel
GROUP BY QrySaleTot.Item, QrySaleTot.ProductID, [QryStockLevel].[Stock]-[QrySaleTot].[Quantity];
Thanks

You can join the two tables by id and just subtract.
SELECT Sales.ID, Stock.Level - Sales.Quantity
FROM Sales
INNER JOIN Stock
ON Sales.ID = Stock.ID
Updating is not so different. Play around with the query design window. You may wish to read:
Fundamental Microsoft Jet SQL for Access 2000
Intermediate Microsoft Jet SQL for Access 2000
Advanced Microsoft Jet SQL for Access 2000

You included this query in the update to your question:
SELECT
QrySaleTot.Item,
QrySaleTot.ProductID,
[QryStockLevel].[Stock]-[QrySaleTot].[Quantity] AS StockOnHand
FROM QrySaleTot, QryStockLevel
GROUP BY
QrySaleTot.Item,
QrySaleTot.ProductID,
[QryStockLevel].[Stock]-[QrySaleTot].[Quantity];
The first problem is you don't have a join condition ... so each row from QrySaleTot will be matched up with every row from QryStockLevel. That will produce what is called a Cartesian product, or cross join. Revise it to use a join on a field the 2 queries have in common.
The GROUP BY doesn't seem useful here because you're not computing any aggregate values.
Finally, Item is a reserved word. If you must keep that field name, bracket it everywhere you reference it in your queries like this: QrySaleTot.[Item]

Related

Preparing SQL Data for a CDF Plot

i have the following SQL that I would like to use to plot cumulative distribution plot but i can't seem to get the data right.
Sample Data:
token_Length
Frequency
1
6436
2
7489
3
3724
4
2440
5
667
6
396
7
264
8
215
9
117
10
90
11
61
12
29
13
69
15
40
18
45
How do i prepare this data to create a CDF plot in looker?
So that it looks like
token_Length
Frequency
cume_dist
1
6436
0.291459107
2
7489
0.630604112
3
3724
0.799248256
4
2440
0.909745494
5
667
0.939951091
6
396
0.95788425
7
264
0.969839688
8
215
0.979576125
9
117
0.984874558
10
90
0.988950276
11
61
0.991712707
12
29
0.993025994
13
69
0.996150711
15
40
0.997962141
18
45
1
I have tried a measure as follows:
measure: cume_dist {
type: number
sql: cume_dist() over (order by ${token_length} ASC);;
}
This generates SQL as:
SELECT
token_length,
COUNT(*) AS "count",
cume_dist() over (order by (token_length) ASC) AS "cume_dist"
FROM string_facts

how to update duplicate rows in a column to a new values

I will explain my problem briefly
have duplicate rino like below (actually this rino is the serial number in front end)
chqid rino branchid
----- ---- --------
876 6 2
14 6 2
18 10 2
828 10 2
829 11 2
19 11 2
830 12 2
20 12 2
78 40 2
1092 40 2
1094 41 2
79 41 2
413 43 2
1103 43 2
82 44 2
1104 44 2
1105 45 2
83 45 2
91 46 2
1106 46 2
here in my case I don't want to delete these duplicate rino instead of that I planned to update the rino having max date(column not specified in the above sample actually a date column is there) to the next rino number
what exactly I meant is :
if I sort out the above result according to the max(date) I will get
chqid rino branchid
----- ---- --------
876 6 2
828 10 2
19 11 2
830 12 2
1092 40 2
79 41 2
413 43 2
82 44 2
83 45 2
1106 46 2
(NOTE : total number of duplicate rows are 10 in branchid=2)
the last entered rino in the table for branchid=2 is 245
So I just want to update the 10 rows(Of column rino) with numbers starting from 246 to 255( just added 245+10 like this select lastno+ generate_series(1,10) nos from tab where cola=4 and branchid = 2 and vrid=20;)
Expected Output:
chqid rino branchid
----- ---- --------
876 246 2
828 247 2
19 248 2
830 249 2
1092 250 2
79 251 2
413 252 2
82 253 2
83 254 2
1106 255 2
using postgresql
Finally I found a solution, am using dynamic-sql to solve my issue
do
$$
declare
arow record;
begin
for arow in
select chqid,rino,branchid from (
select chqid,rino::int ,vrid,branchid ,row_number()over (partition by rino::int ) rn
from tab
where vrid =20
and branchid = 2)t
where rn >1
loop
execute format('
update tab
set rino=(select max(rino::int)+1 from gtab19 where acyrid=4 and branchid = 2 and vrid=20)
where chqid=%s
',arow.chqid);
end loop;
end;
$$;

Getting more data while converting data int to float and doing division and Multiplying with int?

I have three columns as shown in below tableA
Student Day Shifts
129 11 4
91 9 6
166 19 8
164 26 12
146 11 6
147 16 8
201 8 3
164 4 2
186 8 6
165 7 4
171 10 4
104 5 4
1834 134 67
I am writing a tvf to calculate Value of Points generated for Students as below
ALTER function Statagic(
#StartDate date
)
RETURNS TABLE
AS
RETURN
(
with src as
( select
Division=case when Shifts=0 then 0 else cast(Day as float)/cast(Shifts as float) end,*
from TableA
)
,tgt as
(select *,Points=Student*Division from src
)
select * from tgt)
When i execute above tvf(select * from Statagic('3/16/2014'))
My output is below
129 11 4 2.75 354.75
91 9 6 1.5 136.5
166 19 8 2.375 394.25
164 26 12 2.16666666666667 355.333333333333
146 11 6 1.83333333333333 267.666666666667
147 16 8 2 294
201 8 3 2.66666666666667 536
164 4 2 2 328
186 8 6 1.33333333333333 248
165 7 4 1.75 288.75
171 10 4 2.5 427.5
104 5 4 1.25 130
1834 134 67 2 3668
Note :
If you see the last row for three columns in the table is the total of rest column.So when you see the last row in the Output of TVF for last two columns when i am adding i am not getting same data i am getting more.
Guys please help me i am struggling to fix this bug i tried in all ways but i am unable to fix it.
select 354.75+136.5+394.25+355.333333333333+267.666666666667+294+536+328+248+288.75+427.5+130=3760.750000000000
3668 is not euql to 3760.75(I am getting more 100 value)

Stock on the fly missing values with no sales

I have the following queries,
QryStockOnHand
SELECT QrySaleTot.Item, QrySaleTot.ProductID, [QryStockLevel].[Stock]-[QrySaleTot].[Quantity] AS StockOnHand
FROM QryStockLevel INNER JOIN QrySaleTot ON QryStockLevel.ProductID = QrySaleTot.ProductID;
QrySaleTot
SELECT TblProduct.Item, Sum(TblTotalSale.Size) AS Quantity, TblProduct.ProductID
FROM TblProduct INNER JOIN TblTotalSale ON TblProduct.[ProductID] = TblTotalSale.[ProductID]
GROUP BY TblProduct.Item, TblProduct.ProductID;
QryStockLevel
SELECT TblStock.ProductID, Sum(TblStock.StockLevel) AS Stock, TblProduct.Item
FROM TblStock INNER JOIN TblProduct ON TblStock.ProductID = TblProduct.ProductID
GROUP BY TblStock.ProductID, TblProduct.Item;
When I run the QryStockonHand and no sales of a product have been made then the porduct does not appear in the result of the query...
Sample Data
TblStock
StockID ProductID StockLevel
138 1 528
139 3 528
140 5 528
141 9 528
142 7 528
143 18 80
144 30 72
145 34 72
146 33 72
147 32 200
148 22 80
149 19 80
150 23 80
151 20 80
TblProduct
ProductID Item Price StockDelivery PriceSmall Large Small
1 Carling £2.50 528 £1.40 2 1
3 Carlsburg £2.70 528 £1.60 2 1
5 IPA £2.30 528 £1.20 2 1
7 StrongBow £2.80 528 £1.65 2 1
9 RevJames £2.45 528 £1.30 2 1
11 Becks £2.90 72 1
12 WKDBlue £2.80 72 1
13 WKDRed £2.80 72 1
14 SmirnoffIce £2.80 72 1
TblTotalSale
TotalSalesID ProductID SalePrice Day Time Size
576 1 £1.40 19/02/2012 15:34:24 1
528 1 £2.50 09/02/2012 14:44:44 2
530 1 £1.40 09/02/2012 14:44:44 1
565 1 £2.50 19/02/2012 15:34:24 2
567 1 £1.40 19/02/2012 15:34:24 1
570 3 £2.70 19/02/2012 15:34:24 2
571 3 £1.60 19/02/2012 15:34:24 1
577 3 £2.70 19/02/2012 15:34:24 2
578 3 £1.60 19/02/2012 15:34:24 1
533 3 £2.70 09/02/2012 14:44:44 2
534 3 £1.60 09/02/2012 14:44:44 1
Any Idea why... I guess it is a null thing, where it is seeing the no sales as a non existent thing, instead of a zero sales.... any idea how i could fix it?
Thanks
Sam
Instead of an inner join, use a left outer join, which will tell it to grab all of the rows from the left hand table on the join, instead of an inner join, which returns only rows which have values in both tables.
I don't know the QryStockLevel fields, but your query should look something like this:
SELECT QryStockLevel.Item, QryStockLevel.ProductID, [QryStockLevel].[Stock]-NZ([QrySaleTot].[Quantity],0) AS StockOnHand
FROM QryStockLevel LEFT OUTER JOIN QrySaleTot ON QryStockLevel.ProductID = QrySaleTot.ProductID;
Note the NZ function to handle a null on the Quantity when qrysaletot does not have a row.

SQL query self join

I am working on a query for a report in Oracle 10g.
I need to generate a short list of each course along with the number of times they were offered in the past year (including ones that weren't actually offered).
I created one query
SELECT coursenumber, count(datestart) AS Offered
FROM class
WHERE datestart BETWEEN (sysdate-365) AND sysdate
GROUP BY coursenumber;
Which produces
COURSENUMBER OFFERED
---- ----------
ST03 2
PD01 1
AY03 2
TB01 4
This query is all correct. However ideally I want it to list those along with COURSENUMBER HY and CS in the left column as well with 0 or null as the OFFERED value. I have a feeling this involves a join of sorts, but so far what I have tried doesn't produce the classes with nothing offered.
The table normally looks like
REFERENCE_NO DATESTART TIME TIME EID ROOMID COURSENUMBER
------------ --------- ---- ---- ---------- ---------- ----
256 03-MAR-11 0930 1100 2 2 PD01
257 03-MAY-11 0930 1100 12 7 PD01
258 18-MAY-11 1230 0100 12 7 PD01
259 24-OCT-11 1930 2015 6 2 CS01
260 17-JUN-11 1130 1300 6 4 CS01
261 25-MAY-11 1900 2000 13 6 HY01
262 25-MAY-11 1900 2000 13 6 HY01
263 04-APR-11 0930 1100 13 5 ST03
264 13-SEP-11 1930 2100 6 4 ST03
265 05-NOV-11 1930 2100 6 5 ST03
266 04-FEB-11 1430 1600 6 5 ST03
267 02-JAN-11 0630 0700 13 1 TB01
268 01-FEB-11 0630 0700 13 1 TB01
269 01-MAR-11 0630 0700 13 1 TB01
270 01-APR-11 0630 0700 13 1 TB01
271 01-MAY-11 0630 0700 13 1 TB01
272 14-MAR-11 0830 0915 4 3 AY03
273 19-APR-11 0930 1015 4 3 AY03
274 17-JUN-11 0830 0915 14 3 AY03
275 14-AUG-09 0930 1015 14 3 AY03
276 03-MAY-09 0830 0915 14 3 AY03
SELECT
coursenumber,
COUNT(CASE WHEN datestart BETWEEN (sysdate-365) AND sysdate THEN 1 END) AS Offered
FROM class
GROUP BY coursenumber;
So, as you can see, this particular problem doesn't need a join.
I think something like this should work for you, by just doing it as a subquery.
SELECT distinct c.coursenumber,
(SELECT COUNT(*)
FROM class
WHERE class.coursenumber = c.coursenumber
AND datestart BETWEEN (sysdate-365) AND sysdate
) AS Offered
FROM class c
I like jschoen's answer better for this particular case (when you want one and only one row and column out of the subquery for each row of the main query), but just to demonstrate another way to do it:
select t1.coursenumber, nvl(t2.cnt,0)
from class t1 left outer join (
select coursenumber, count(*) cnt
from class
where datestart between (sysdate-365) AND sysdate
group by coursenumber
) t2 on t1.coursenumber = t2.coursenumber