I'm a SQL newbie and need some help with linking 3 tables in Access. Each order has differnt size type from SIZE_TABLE. I need a SQL statement that produces below query result:
Item
Color
Size
Size*Qty
UPCCode
Thank you!
ORDER_TABLE:
ID
ProductID
Color
SizeType
Size1Qty
Size2Qty
Size3Qty
Size4Qty
Size5Qty
1
Item1
Black
A
0
20
20
10
0
2
Item2
Red
B
15
25
25
15
5
3
Item3
White
C
10
15
0
0
0
4
Item4
Yellow
D
20
0
0
0
0
...
...
...
...
...
...
...
...
...
SIZE_TABLE:
ID
SizeType
Size
Size_Num
1
A
XS
Size1
2
A
S
Size2
3
A
M
Size3
4
A
X
Size4
5
A
XL
Size5
6
B
30
Size1
7
B
32
Size2
8
B
34
Size3
9
B
36
Size4
10
B
38
Size5
11
C
SM
Size1
12
C
ML
Size2
13
D
OS
Size1
BARCODE_TABLE:
ID
ProductID
Color
Price
Size
UPCCode
1
Item1
Black
$99
XS
7229473512
2
Item2
Red
$59
30
7229475516
3
Item3
White
$69
OS
7229474902
4
Item4
Yellow
$49
XL
7229474902
...
...
...
...
...
...
Use UNION query to rearrange Order_Table to normalized structure then use that query in subsequent query to join with other tables
SELECT ID, ProductID, Color, SizeType, Size1Qty AS Qty, "Size1" AS Size FROM Order_Table
UNION SELECT ID, ProductID, Color, SizeType, Size2Qty, "Size2" FROM Order_Table
UNION SELECT ID, ProductID, Color, SizeType, Size3Qty, "Size3" FROM Order_Table
UNION SELECT ID, ProductID, Color, SizeType, Size4Qty, "Size4" FROM Order_Table
UNION SELECT ID, ProductID, Color, SizeType, Size5Qty, "Size5" FROM Order_Table;
To avoid UNION query, normalize data structure. Create a dependent table that relates to Order_Table. Call it OrderDetails_Table.
ID | OrderID_FK | ProductID | Qty
Order_Table should have fields for OrderID, CustID, OrderDate.
Related
I have been looking for the solution to this in PIVOT, UNPIVOT, and others but still don't see my scenario. I have items in a table. For simplicity we'll just say PartNum, Desc. These things can be customized. The attributes like color, height, width, depth are stored in a separate table with a code to indicate which attribute.
OrderId - PartNum - Desc (join from inv)
1 12345 - Block A
2 12345 - Block A
3 23456 - Block B
4 23456 - Block B
Two customers get 12345, and two get 23456 and they have width, height, and depth...
AttrId - OrderId - CCode - Value
1 1 WIDTH 10
2 1 HEIGHT 10
3 1 DEPTH 1
4 2 WIDTH 20
5 2 HEIGHT 10
6 2 DEPTH 1
7 3 WIDTH 10
8 3 HEIGHT 20
9 3 DEPTH 2
10 4 WIDTH 10
11 4 HEIGHT 20
12 4 DEPTH 2
I can't use pivot with an aggregate on the value because I need to group each combination of part, width, height, and depth like this
PartNum - Width - Height - Depth - Count - Area (w x h x count)
12345 10 10 1 1 100
12345 20 10 1 1 200
23456 10 20 2 2 400
I tried case statements with the CCode but I get null values in some rows so the grouping didn't work. This is in SQL Server 2019 if that makes a difference. Can someone help out with this?
Is this what you want?
select t1.partnum, t2.width, t2.height, t2.depth, count(*) as cnt
from t1 join
(select t2.orderid,
sum(case when ccode = 'width' then value end) as width,
sum(case when ccode = 'height' then value end) as height,
sum(case when ccode = 'depth' then value end) as depth
from t2
group by t2.orderid
) t2
on t2.orderid = t1.orderid
group by t1.partnum, t2.width, t2.height, t2.depth;
I might speculate that you want:
sum(t2.width * t2.height * t2.depth) as area
but the numbers disagree with the values in your question.
Here is a db<>fiddle.
I have a table as below in sqlite database. I want to create a line chart showing usage by product groups.
Table: ProductUsageData
UserID ProductName ProductGroup Qty RecordID
1 A1 A 12 1
2 A1 A 12 1
1 A2 A 15 1
3 A1 A 12 2
2 B1 B 12 2
5 B2 B 5 2
1 A1 A 12 3
1 A2 A 15 3
4 A1 A 12 3
3 C1 C 12 3
2 C2 C 15 3
Since I want separate line for each ProductGroup I am using below Query
SELECT
SUM(Qty) as UsedQty,
ProductGroup,
RecordID
FROM ProductUsageData
GROUP BY ProductGroup, RecordID
ORDER BY RecordID ASC;
While I get three records for A (for each RecordID) I get only 1 record each for B & C as they are not used during each RecordID.
Problem is when I am putting one line for each ProductGroup in the chart, the points for B & C are shown as per Qty in the first
My output is like this
A 39 1
A 12 2
B 17 2
A 39 3
C 27 3
So the graph looks like this
instead of
To fix this I changed the query using COALESCE to get 0 Qty if the ProductGroup is not used during each recording.
SELECT
COALESCE(SUM(Qty), 0) as UsedQty,
ProductGroup,
RecordID
FROM ProductUsageData
GROUP BY ProductGroup, RecordID
ORDER BY RecordID ASC;
I was expecting output as below
A 39 1
B 0 1
C 0 1
A 12 2
B 17 2
C 0 2
A 39 3
B 0 3
C 27 3
But I am getting same output as first
Please let me know how can I correct the query to get desired output
A typical solution is to first cross join two queries that select the distinct product groups and record ids from the table; this gives you all possible combinations of productGroup and recordID.
Then, you can bring in the original table with a left join, and aggregate:
select
g.productGroup,
coalesce(sum(p.qty), 0) qty,
r.recordID
from (select distinct productGroup from productUsageData) g
cross join (select distinct recordID from productUsageData) r
left join productUsageData p
on p.productGroup = g.productGroup
and p.recordID = r.recordID
group by r.recordID, g.productGroup
order by r.recordID, g.productGroup
In the real world, you might have separate referential tables for product groups and records ids, which would make the query simpler and more efficient (since it would avoid the need to select distinct in subqueries).
Demo on DB Fiddle:
productGroup | qty | recordID
:----------- | :-- | :-------
A | 39 | 1
B | 0 | 1
C | 0 | 1
A | 12 | 2
B | 17 | 2
C | 0 | 2
A | 39 | 3
B | 0 | 3
C | 27 | 3
I have 3 tables which maintains stock entries for each products/items. These three tables like below :
Table : ItemStock (to maintain remaining stock of each item)
Id ItemId OpgQty BranchID CurrentStock
1 7 0 1 8
2 7 0 2 3
3 6 0 1 2
4 6 0 2 0
Table : ItemPurchase (StockIn)
Id ItemId Qty BranchID
1 7 5 1
2 7 4 2
3 7 6 1
4 7 2 2
5 6 4 1
6 6 2 2
7 6 2 1
Table : ItemSale (StockOut)
Id ItemId Qty BranchID
1 7 2 1
2 7 3 2
3 7 1 1
4 6 4 1
5 6 2 2
Desired Output (based on sql queries)
I want to have result like below : (part of report)
Id ItemId OpgQty BranchId StockIn StockOut CurrentStock
1 7 0 1 11 3 8
2 7 0 2 6 3 3
3 6 0 1 6 4 2
4 6 0 2 2 2 0
I was trying to get the desired result but was not able to do so. Please help!!!
try this;
select
m.Id,
m.ItemId,
m.OpgQty,
m.BranchID,
si.StockIn,
m.CurrentStock-si.StockIn StockOut,
m.CurrentStock
from
ItemStock m
inner join
(
select
ItemId,BranchId,sum(Qty) as StockIn
from
ItemPurchase
group by ItemId,BranchId
) si on si.ItemId=m.ItemId and si.BranchId=m.BranchId
A very simple query that gives the desired result is :
select *,
(select sum(Qty)
from ItemPurchase
where ItemPurchase.ItemId = ItemStock.ItemId and
ItemPurchase.BranchId = ItemStock.BranchId) as StockIn,
(select sum(Qty)
from ItemSale
where ItemSale.ItemId = ItemStock.ItemId and
ItemSale.BranchId = ItemStock.BranchId) as StockOut
from ItemStock
Two subqueries with group by and aggregation will get what you want.
select
s.*,
coalesce([ip].StockIn, 0) as StockIn, -- In case of no records in ItemPurchase or ItemSale, coalesce is neccessary.
coalesce([is].StockOut, 0) as StockOut
from ItemStock s
left join (
select sum(Qty) as StockIn, ItemId, BranchId
from ItemPurchase
group by ItemId, BranchId
) [ip] on s.ItemId = [ip].ItemId and s.BranchId = [ip].BranchId
left join (
select sum(Qty) as StockOut, ItemId, BranchId
from ItemSale
group by ItemId, BranchId
) [is] on s.ItemId = [is].ItemId and s.BranchId = [is].BranchId
See demo in sqlfiddle.
Please
Try This ... I hope you consider this too.
Table 1
prodUID (*) plantID ItemName size qty
1 1 car med 5
2 1 car small 2
3 1 car large 8
4 1 truck small 7
5 1 truck med 0
6 1 truck large 4
7 1 van small 0
8 2 truck large 10
table2
UID(*) plantID table2_plan_tid table2_prodUID itemname size num wk
------------------------------------------------------------------------------
1 1 1 car med 3 41
2 1 2 car small 0 42
3 1 3 car large 6 41
4 1 4 truck small 1 44
5 1 5 truck med 10 45
6 1 6 truck large 1 43
7 1 7 van small 7 42
8 2 8 car med 10 41
table3
UID(*) plantid table3_wk table3_prodUID itemName size qty
------------------------------------------------------------------
1 1 41 2 car med 5
2 1 41 3 car large 7
3 1 43 7 van small 8
Result I'm trying to get is
for plantid = ? and wk between ? and ?
plantid = 1 and wk between 41 and 45
sum qty for table1 on plantID
sum wk for table2 on plantID and for weeks
sum qty on table with on plantID and for weeks
and some how link this with joins or subqueries to get
plantid Itemname qty num order
------------------------------------
1 car 15 9 12
1 truck 11 12 0
1 van 0 7 8
I can't seem to get the right outcome
I also considered putting in an itemNameID field in all tables
if that would make it easier
You can achieve what you want using this query:
SELECT table1.plantID, table1.ItemName, SUM(table1.qty), COALESCE(t2.num_sum, 0), COALESCE(t3.qty_sum, 0)
FROM table1
LEFT JOIN (SELECT plantid, itemname, SUM(num) AS num_sum
FROM table2
WHERE wk >= 41 AND wk <= 45
GROUP BY plantID, itemname) t2 ON table1.plantID = t2.plantID AND table1.ItemName = t2.ItemName
LEFT JOIN (SELECT plantid, itemname, SUM(qty) AS qty_sum
FROM table3
WHERE table3_wk >= 41 AND table3_wk <= 45
GROUP BY plantID, itemname) t3 ON table1.plantID = t3.plantID AND table1.ItemName = t3.ItemName
WHERE table1.plantid = 1
GROUP BY plantID, ItemName
See also live!
Basically, you join 3 queries in order to get what you want.
That would be a bit cleaner if you respect a certain harmony with you column names.
cat_id prod_name parent_cat_id
------ ---------- ------------
1 prod_1 2
2 prod_2 5
3 prod_3 1
4 prod_4 3
5 prod_5 7
6 prod_6 5
In a recursive function, make a table and by using these, if cat_id = 1 and parent_cat_id = 1 take that product name and if that product category id and parent category id is same then take that record also..
ANS IS LIKE :::
1 prod_1 2
2 prod_2 5
5 prod_5 7
WITH rows AS
(
SELECT cat_id, prod_name, parent_cat_id
FROM mytable
WHERE cat_id = 1
UNION ALL
SELECT m.cat_id, m.prod_name, m.parent_cat_id
FROM mytable m
JOIN rows r
ON r.parent_cat_id = m.cat_id
)
SELECT *
FROM rows