ORACLE - Calculate two values and show result in a view - oracle

I received fantastic help from Dave Costa and Justin Cave here (thanks again) in how to calculate a subtotal value from two other attributes, Quantity and Price (so Quantity * Price = Subtotal). In one of the answers it was stated that from a normalisation point of view it's not good to do this as the subtotal value can be derived from the other two attributes and that I should probably look at using a View. I've read up on Views and am getting the concept (I'll be using them elsewhere regardless) but I'm still not sure how to go about actually calculating the two values and show the result in a custom view. If anyone could point me in the right direction I'd appreciate it.
The current trigger (credit to Dave and Justin):
CREATE VIEW show_subtotal
AS SELECT price
FROM products
WHERE product_no =:new.product_no;
:new.subtotal := currentPrice * :new.quantity;

Something like this, for example, will compute the subtotal by joining the order_line and product tables together just as the previous trigger was doing. Presumably, you'd want to include some additional attributes (i.e. the order number, the order line number, etc.)
CREATE OR REPLACE VIEW order_line_with_subtotal( product_no,
quantity,
price,
subtotal )
AS
SELECT ol.product_no,
ol.quantity,
p.price,
(ol.quantity * p.price) subtotal
FROM order_line ol
JOIN product p ON (ol.product_no = p.product_no)
This has many of the same data consistency issues that the trigger-based solution had since the current price is being referenced from the product table rather than the then-current price being stored in the order_line table. If you changed the data model so that the order_line table stored the quantity and the current price, the view would get simpler because it would no longer need to join to the product table
CREATE OR REPLACE VIEW order_line_with_subtotal( product_no,
quantity,
price,
subtotal )
AS
SELECT ol.product_no,
ol.quantity,
ol.price,
(ol.quantity * ol.price) subtotal
FROM order_line ol
If you are on 11g, you can also create a virtual column in your table definition,
CREATE TABLE order_line (
order_line_no NUMBER PRIMARY KEY,
order_no NUMBER REFERENCES order( order_no ),
price NUMBER,
quantity NUMBER,
subtotal NUMBER GENERATED ALWAYS AS (price*quantity) VIRTUAL
);

Related

Keep track of quantity and some columns in multiple tables

I'm trying to build a farm management app with Laravel and I need help with my database schema. Here are some of my tables
users (id, name)
products (id, trade_name, state, category_id)
expenses (id, category_id)
activities (id, equipment_id)
equipment (id, name, type)
Equipment can either be a tractor or implement. For tractors I want to have the following columns total_distance_traveled and a fuel_quantity
Product can be fertilizers, Insecticide, Herbicides and their state can either be liquid or solid and I want to to keep track of quantity of my products as well (quantity is in liters or kilograms)
I'm not sure if it's a good idea to include the total_distance_traveled and fuel_quantity in the equipment table and a quantity column in the products table
I've thought of creating a new stocks table and have a stockable_id, and stockable_type but that would only work for quantity since it's kind of a common column between products and equipment tables
I'ld like to keep track of total_distance_traveled, fuel_quantity for the equipment table and quantity for products table. e.g. when the total_distance_traveled is changed I'd like to know when that happened so I can have logs of my activities over time
You can create a stock table if you know that this table will only use for product and equipements:
id
stockable_id
stockable_type
quantity
total_distance_traveled // nullable as they have value only when item will be tractor,
fuel_quantity // nullable as they have value only when item will be tractor,
but for same table or if you think there can come other data too then a you can have a json column in which you can save extra attributes for specific item
id
stockable_id
stockable_type
quantity
sepecific_attributes //json where you can save any additional values based on your product
"{'total_distance_traveled':'100','fuel_quantity':20,'some_other_info':'value'}

Create a generic DB table

I am having multiple products and each of them are having there own Product table and Value table. Now I have to create a generic screen to validate those product and I don't want to create validated table for each Product. I want to create a generic table which will have all the Products details and one extra column called ProductIdentifier. but the problem is that here in this generic table I may end up putting millions of records and while fetching the data it will take time.
Is there any other better solution???
"Millions of records" sounds like a VLDB problem. I'd put the data into a partitioned table:
CREATE TABLE myproducts (
productIdentifier NUMBER,
value1 VARCHAR2(30),
value2 DATE
) PARTITION BY LIST (productIdentifier)
( PARTITION p1 VALUES (1),
PARTITION p2 VALUES (2),
PARTITION p5to9 VALUES (5,6,7,8,9)
);
For queries that are dealing with only one product, specify the partition:
SELECT * FROM myproducts PARTITION FOR (9);
For your general report, just omit the partition and you get all numbers:
SELECT * FROM myproducts;
Documentation is here:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/vldbg/toc.htm

Get list of joined rows with DAX

I have Supplier dimension table has 1:n relationship with InvoiceDetail fact table. I would like to get the list of active suppliers like below SQL, but in DAX language:
SELECT [Id]
,[Name]
,[Code]
,[CountryIso]
FROM [Supplier] s
WHERE EXISTS (SELECT 1 FROM [InvoiceDetail] id WHERE s.id = id.SupplierId)
I am not sure how I can do on Measure with DAX
Assuming that an active supplier means that the supplier has an invoice against them and that your data looks something like this..
Invoice Table
Supplier Table
Creating a relationship between the two tables will in effect, 'join' the two tables.
You can then use invoice number field from the invoice table and the name/code/countryiso from the supplier table.
Example being:
The value are only being drawn from the invoice table, so you'll only see active Invoices.
If being an active supplier means having a true bool value, join the tables and add a report/page wide filter on that bool value.

Magento Inherit EAV collection

I have created a custom module and tables for that.
1) How can I use EAV Collection functions on my custom table collection - addExpressionAttributeToSelect, I have to use Count, SUM functions .
for example say I have tableA and tableB
table A has some SKU data, tableB has sales data for SKUs(Multilines for a SKU)
`$collection = Mage::getModel('Module/tableA')->getCollection();`,
Now I need one more column(sum of total sale) in this collection from tableB IF sold date is between two dates.
2) How to write inner queries While preparing collection.

Triggers in Oracle PL/SQL Problems

I have just written a stored procedure and stored function that serve to insert a new row into my Orders table. The row update inserts: Ordernum, OrderDate, Customer, Rep, Manufacturer, Product, Qty, and SaleAmount.
I now have to write a trigger that updates my Salesreps table by adding the amount of the order just added. I am unsure of how to reference the rows. I have tried this:
CREATE OR REPLACE TRIGGER UpdateSalesrep
AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
UPDATE Salesreps
SET Sales = Sales + :NEW.Amount
WHERE Rep = Salesrep;
End;
/
Sales is the name of the column on the Salesrep table. Amount if the name used in the stored proc. I am getting an error returned on 'Where Rep = Salesrep'. If I don't include this line, the trigger does not return any errors. However, I am assuming that if I can't figure out how to tie the sales amount into the one salesrep that made the sale, I will update every salesrep (which I'm sure they would be quite happy with). Any help would be greatly appreciated, as always.
CREATE OR REPLACE TRIGGER UpdateSalesrep
AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
UPDATE Salesreps
SET Sales = Sales + :NEW.Amount
WHERE Salesrep = :NEW.Rep;
End;
/
Who have said that you have to write a trigger? Is this stated explicitly as a requirement in your homework task? You can also update table UpdateSalesRep in the same stored procedure that you have already written for inserting in table Orders.
Read: http://www.oracle.com/technetwork/issue-archive/2008/08-sep/o58asktom-101055.html

Resources