Dear Experts,
Need your help. I having date in 3 tables
+----------+-------------+-------------+
| SalesABC | | |
+----------+-------------+-------------+
| Code | Cost Amount | Sale Amount |
| ABC | 70 | 100 |
+----------+-------------+-------------+
+-------------+-------------+-----------+----------+--------+
| PurchaseABC | | | | |
+-------------+-------------+-----------+----------+--------+
| RawCode | Description | Pur Price | Currency | Vendor |
| A | Kit | 0.495 | Euro | S |
| B | Barrel | 0.282 | $ | C |
| C | Rod | 15 | INR | L |
+-------------+-------------+-----------+----------+--------+
+----------+------------+-----------+------+------+
| BOMABC | | | | |
+----------+------------+-----------+------+------+
| Line No. | ParentCode | ChildCode | Qty. | Unit |
| 1000 | ABC | A | 1 | Pcs |
| 2000 | ABC | B | 1 | Pcs |
| 3000 | ABC | C | 1 | Pcs |
+----------+------------+-----------+------+------+
Following are the commands to create and populate above tables.
CREATE TABLE SalesABC(
Code VARCHAR(3) NOT NULL PRIMARY KEY
,Cost_Amount INTEGER NOT NULL
,Sale_Amount INTEGER NOT NULL
);
INSERT INTO SalesABC(Code,Cost_Amount,Sale_Amount) VALUES ('ABC',70,100);
CREATE TABLE PurchaseABC(
RawCode VARCHAR(1) NOT NULL PRIMARY KEY
,Description VARCHAR(6) NOT NULL
,Pur_Price NUMERIC(5,3) NOT NULL
,Currency VARCHAR(4) NOT NULL
,Vendor VARCHAR(1) NOT NULL
);
INSERT INTO PurchaseABC(RawCode,Description,Pur_Price,Currency,Vendor) VALUES ('A','Kit',0.495,'Euro','S');
INSERT INTO PurchaseABC(RawCode,Description,Pur_Price,Currency,Vendor) VALUES ('B','Barrel',0.282,'$','C');
INSERT INTO PurchaseABC(RawCode,Description,Pur_Price,Currency,Vendor) VALUES ('C','Rod',15,'INR','L');
CREATE TABLE BOMABC(
Line_No INTEGER NOT NULL
,ParentCode VARCHAR(3) NOT NULL
,ChildCode VARCHAR(1) NOT NULL
,Qty INTEGER NOT NULL
,Unit VARCHAR(3) NOT NULL
,PRIMARY KEY(Line_No,ParentCode)
);
INSERT INTO BOMABC(Line_No,ParentCode,ChildCode,Qty,Unit) VALUES (1000,'ABC','A',1,'Pcs');
INSERT INTO BOMABC(Line_No,ParentCode,ChildCode,Qty,Unit) VALUES (2000,'ABC','B',1,'Pcs');
INSERT INTO BOMABC(Line_No,ParentCode,ChildCode,Qty,Unit) VALUES (3000,'ABC','C',1,'Pcs');
I am using following query to get the output data
Declare @Euro as int, @Dollar as Int
set @Euro=81
set @Dollar=71;
with sale (Item,Cost,Sale)
as
(select * from SalesABC),
BOM (LineNum, ParentCode,ChildCode,Qty, Unit)
as
(select * from BOMABC),
Purchase (RawMaterial, Descrip, PurPrice,Currency,VendorName)
as
(select * from PurchaseABC)
Select sale.,BOM.ChildCode
, (CASE WHEN Purchase.Currency = 'Euro' THEN ROUND(Purchase.PurPrice@Euro,0)
WHEN Purchase.Currency = '$' THEN ROUND(Purchase.PurPrice*@Dollar,0)
WHEN Purchase.Currency = 'INR' THEN ROUND(Purchase.PurPrice,0)
eLSE 0 END) AS MasterCost
--, Purchase.PurPrice
, Purchase.VendorName
, sale.Sale-sale.Cost as 'Margin(Rs.)'
from sale
left join BOM on sale.Item = BOM.ParentCode
left join Purchase on BOM.ChildCode = Purchase.RawMaterial
I am getting following output using the above query
+-------+------+------+--------------+-------------+--------+--------------+
| Code | Cost | Sale | Raw Material | Master Cost | Vendor | Margin (Rs.) |
+-------+------+------+--------------+-------------+--------+--------------+
| ABC | 70 | 100 | A | 40 | S | 30 |
| ABC | 70 | 100 | B | 20 | C | 30 |
| ABC | 70 | 100 | C | 15 | L | 30 |
+-------+------+------+--------------+-------------+--------+--------------+
Actually the output needed is as follows:
+------+------+------+-----+-------------+--------+--------------+-----------------+-------------------+
| Code | Cost | Sale | R M | Master Cost | Vendor | Margin (Rs.) | Prop Margin (%) | Prop Margin (Rs.) |
+------+------+------+-----+-------------+--------+--------------+-----------------+-------------------+
| ABC | 70 | 100 | A | 40 | S | 30 | 53% | 16 |
| ABC | 70 | 100 | B | 20 | C | 30 | 27% | 8 |
| ABC | 70 | 100 | C | 15 | L | 30 | 20% | 6 |
| | | | | 75 | | | 100% | |
+------+------+------+-----+-------------+--------+--------------+-----------------+-------------------+
Proportionate Margin (%) = (Master Cost / Total Master Cost)
For eg. In first record, Proportionate Margin (%) = (40/75)*100
Proportionate Margin (Rs.) = Margin (Rs.) * Proportionate Margin (%)
For eg. In first record, Proportionate Margin (Rs.) = 30 * 53% = 15.9 Rs.
The biggest problem I am facing is how to do totalling of Master Cost itemwise i.e. there are 1000’s of items (codes) for which totaling is needed itemwise. The no. of rows in bom is also not fixed.
Multiple items data will look like a follows:-
+------+------+------+--------------+-------------+--------+--------------+--------------------------+----------------------------+
| Code | Cost | Sale | Raw Material | Master Cost | Vendor | Margin (Rs.) | Proportionate Margin (%) | Proportionate Margin (Rs.) |
+------+------+------+--------------+-------------+--------+--------------+--------------------------+----------------------------+
| ABC | 70 | 100 | A | 40 | S | 30 | 53% | 16 |
| ABC | 70 | 100 | B | 20 | C | 30 | 27% | 8 |
| ABC | 70 | 100 | C | 15 | L | 30 | 20% | 6 |
| XYZ | 140 | 200 | X | 80 | S | 60 | 53% | 32 |
| XYZ | 140 | 200 | Y | 40 | C | 60 | 27% | 16 |
| XYZ | 140 | 200 | Z | 30 | L | 60 | 20% | 12 |
+------+------+------+--------------+-------------+--------+--------------+--------------------------+----------------------------+
Can anybody extended your help to resolve the above issue.