adding a calculated column on QlikView - qlikview

I'm new on QV and trying to add a column to an existing table.
I want to add to the sales table a sales revenue column that is the sum of salesprice from products table * quantity from sales table.
All of this because I wish to have a list box of sales revenue.
Thanks in advance to all the helpers!

You will need to in the unit price onto the Sales table and then do the calculation. You can't do sub-queries so it will have to be something of the following form;
Sales:
Load Product_No,
Quantity
from Sales.xls;
left join
Load Product_No
Unit_Price
from Products.xls;
Final_Sales:
load Product_No,
Quantity,
Unit_Price,
Quantity*Unit_Price as Revenue
resident Sales;
drop table Sales;
The final drop table statement is very important otherwise you will get a massive synkey between the table that you do not want

Related

Aggregate my quantity sum in a way that doesn't lead to the storeID repeating?

I am writing a SQL query that needs to show the total number of orders from each store. The issue I am running into, is that while I can figure out how to sum the orders by product and each product is only sold by one store, I can't figure out how to total the orders by store alone
This is the code I currently have
SELECT storeID AS [STORE], Product_ID
, SUM(quantity) AS [ORDERS BY STORE]
FROM Fulfillment, Store
GROUP BY storeID, Product_ID;
This line of code leads to a repeat of storeID in the results, where ideally, I would only want storeID to be included in the results once with the total quantity of all of Product_ID being included. I tried to remove Product_ID from the GROUP BY statement, but this resulted in the following error
Column 'Fulfillment.Product_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I'm new to SQL and am trying to learn, so any help and advice is greatly appreciated
#ZLK is correct that if your goal is a total number of units ordered ("quantity") of any product, simply remove the [product_id] column from the SELECT and GROUP BY.
However, it appears that you're referencing two tables ("FROM Fulfillment, Store") and not specifying how those tables are joined, creating a cartesian join - all rows in one table will be joined to all rows in the other table. If the [storeID] and [quantity] fields are available in the Fulfillment table, I recommend removing the Store table reference from the FROM clause (so "FROM Fulfillment" alone).
One last note: You mention that you want to count "orders". In some circumstances, an order may have multiple products and a quantity > 1. If your goal is the total number of "orders" regardless of the number of products or quantity of products on an order, you'll want to use "COUNT(DISTINCT orderID) as [Orders]" (where "orderID" is the reference to the unique order number).

linking child tables of two related parent-child tables

I have two parent child tables and a couple of supporting tables. I am struggling to figure out the right relationships between them.
Orders, a table that holds a list of Orders including:Order Id, Supplier, Order Date etc..
OrderDetails, a Table holds a list of products that have been ordered, and is a child of Orders, linked on OrderId. Key fields are the ProductId and the Quantity Ordered. Each order containes one or many OrderDetails that outline the products and quantities being ordered.
Shipments, a table that holds a list of Shipments, including Tracking Number and Shipper. Links to Orders through Order Id. An Order may have multiple shipments. For example, I ordered 100 lightbulbs. They were dispatched in 5 shipments.
ShipmentDetails, holds a list of product and shipped quantities and is linked to the Shipments table by ShippingId. Each Shipment may have multiple ShipmentDetails. i.e. One shipment may have 30 Lightbulbs and 10 Door Knobs.
Products is a table that holds a list of Products that need to be both ordered and shipped.
So the logic is that I enter details about the products that are to be ordered in Products. For example, 100 CREE LED Lightbulbs, and 50 Door Handles.
When I place an order, I create an Order in Orders.
i.e. amazon.com, order #45454.
Then I add child rows to that order in OrderDetails. i.e. 30 CREE LED Lightbulbs.
When the order Ships, I create an entry in the Shipments table. i.e. Tracking #46464646464, linked to OrderId in Orders. And then I enter what is in that shipment in ShipmentDetails. For example, only 20 or the 30 CREE LED Lightbulbs may be associated with this Shipping entry.
I am struggling with figuring out how to relate the Shipping Detail records to the Order Detail Table. Lets say the Shipping Detail Table has 4 fields.
ShippingID - a link to the Shipments table parent.
TrackingNum - a field that holds a tracking number.
Product - this should be a drop down, that says, the shipment I belong to is related to a defined Order (because ShipmentDetail is a child of a Shipment record which in turn holds a key to the Orders table, which in turn has OrderDetails that reference products).
Quantity - this should have a default (overridable) value that is the "Quantity Ordered" number from the OrdersDetail record that matches the Order Id and Product Id. Where OrderId is in the Shipments table (linked to the Orders table) and ProductID comes from #3 above. The reason it must be overridable is that a shipment may be a partial shipment.
I am stuck with the preceding #3 and #4. I hope I have explained this in a vaguely understandable way! The pictures below may help!
The ask:
What is the correct join between the ShipmentDetails and the OrderDetail Table
How do I create a field in the ShipmentDetail table with a default value pulled from the Quantities Field of the OrderDetail table, where
Shipments!OrderId = Orders!Id and ShipmentDetail!ProductID = OrderDetails!Product ID
I am working in MS Access 2016 - but I suspect this is a fairly generic SQL question...rather than MS Access specific.
I think the naming is confusing.
What I would rather have is (each first id is autoincrement, forgot how to say this on access):
// Product doesn't contain any information about quantity/price etc ...
create table Products(product_id int, name text, description text ...);
// Orders:
create table Orders(order_id int, client_name text, description text ...);
create table OrderDetails(order_detail_id int, order_id int, product_id int, quantity double, unit_price double, ...);
// Shipments
create table Shipments(shipment_id int, company text, description text ...);
create table ShipmentDetails(shipment_detail_id int, shipment_id int, product_id int, quantity double, price double default 0, ...);
// inserting shipments per product (defaulting to total quantity per product), assuming we have shipment_id SID
insert into ShipmentDetails(shipment_id, order_id, product_id, quantity)
select SID, order_id, product_id, SUM(quantity)
from OrderDetails
group by order_id, product_id;
Then you can of course have some filters (date, customer etc ...).
For the first question, I am not clear what exaclty you want to return.
Here is a comparison of quantities:
select t.order_id, t.product_id, sum(t.quantity) as product_quantity, sum(u.quantity) as shipment_quantity
from OrderDetails t inner join ShipmentDetails u
on t.order_id = u.order_id and t.product_id = u.product_id;

Sorting Sql Table by combining multiple column as a column

Please help me. I have a problem in sorting result. I have a product table name with the columns
ProductID, name, price,...
and the Promotion table with columns
PromoID, startDate, EndDate, SalePrice, ProductID,.....
I want to retrieve the products by price.
I want to sort the price by asc and desc. I want to sort the price column in Product table and SalePrice column in Promotion table as a one column because I want to show promotion price to users if the product is on promotion.
Please how can I solve that problem? Is it possible to combine multiple columns as one in sorting?
As you seem to want a single price column try along
SELECT
Prod.productID,
ISNULL(Prom.salePrice, Prod.price)
FROM Product Prod
JOIN Promotion Prom
ON Prod.productID = Prom.productID
AND Prom.startDate <= CONVERT (date, GETDATE())
AND Prom.endDate >= CONVERT (date, GETDATE());
Use the query
select (what you want)
from product
inner join promotion on product.productid = promotion.promoid
order by price, salesprice asc
(what you want) should not be ambiguous, i.e. use like product.productprice, promo.promoprice etc.

SQL to find the average amount of money spent by country?

I need to find the average amount of money spent by country, using the two tables below in oracle. Sale_total is the money spent in each sale and Cust_country is the customer's country. Any help would be greatly appreciated.
Tables
Sale
Sale_Id
Payment_ID
Ship_Id
Customer_ID
Sale_total ==> Money Spent
Sale_date
Sale_time
Customer
Cust_name
Cust_address
Cust_city
Cust_country
Cust_phone
Cust_age
Cust_sex
You need to have a link between the Sale and Customer tables. Presumably place Customer_ID in the Customer table as a primary key, and have Customer_ID in the Sale table be a foreign key. Assuming you do that, you can then run the below query.
SELECT
AVG(S.Sale_Total) Spent,
c.Cust_country
FROM
Sale S
INNER JOIN
Customer C on S.Customer_ID=C.Customer_ID
GROUP BY C.Cust_Country
This tells you the average amount spent, and the country where it was spent.
The key to understanding this answer is that AVG (the average function) is an aggregate function, as it combines things. Often when you have an aggregate function, you have to group the other columns, hence why we included the GROUP BY clause.
The reason you need Customer Id in the Customer table (apart from being the logical place for it) is so that you can establish a relationship between the Customer and Sale tables.
Another way to establish the relation is you can create a new table to link Customer and Sale together. Call it Customer Sales. For example:
Customer_Sales
Customer_Id
Sale_Id
Then you'd adjust the query to join based on that.
Try this:
SELECT C.Cust_country as Country, AVG(S.Sale_total) as Spent
FROM Sale S INNER JOIN
Customer C on S.Customer_ID=C.Customer_ID
GROUP BY C.Cust_country
AVG is an aggregate function that evaluates the average of an expression over a set of rows.
Read more about here.

grouping common results in sql

I have a products table, with the fields product, category and cost, of type varchar, varchar and decimal.
I then have a sales table, with the fields client, productname, quantity, cost, and saledate, of type varchar, varchar, int, decimal and date.
I want to show all of the products sold for a month, say the current month.
However, I donĀ“t want to show every sale individually. I want to automatically add all of one product together and show it as one row.
In the sales table, the cost for each sales record is already multiplied by the quantity.
So for example, to if 5 beers were purchased, it would returned as one row showing name as beers, quanity as 5, and cost as however much.
I need something like say:
Select product, cost from sales, WHERE sales.product=products.name
AND category='food' AND WHERE month(date_field) = month(getdate())
This should show all the sales for a certain category of product for the current month, but is there an easy way to "group" products together?
I would have to take into account the quantity field in the sales table, because one sale is not necessarily only one product
A hopefully clearer example, one sale record maybe for 2 beers for one client with a cost of 10, and another sales record may be to a different client with a quantity of 3 and cost of 15. I want just one record that would say for beer, 5 were sold and the total cost is 25.
I have no idea where to go from as far as I have gotten...
You are looking for something like
Select product, cost from sales,products WHERE sales.product=products.name
AND category='food' AND month(date_field) = month(getdate())
To get a listing of the items linked as you suggest according to the category in the products tables.
To get the summary by category you need something like:
Select category,SUM(Sales.Quantity),SUM(Sales.cost) from
sales,products WHERE sales.product=products.name
AND category='food' AND month(date_field) = month(getdate()) group by category
This will work but there is a lot to criticise in your database structure, specifically to link products by name as you do is not very reliable.
This question looks awfully familiar...
The answer is a SQL GROUP BY statement.
SELECT product, SUM(quantity) FROM sales s, products p WHERE product=p.name AND category='food' AND month(date_field)=month(getdate()) GROUP BY product
The SUM(quantity) above will tally up all the units sold of a particular product in the given month.
Use GROUP BY and COUNT
SELECT productname, cost, COUNT(*) AS cnt
FROM sales LEFT JOIN products ON sales.product = products.productname
WHERE category='food' AND month(date_field) = month(getdate())
GROUP BY productname