Hey all so I have created a view using some tables in my DB.
My view looks like..
Listing all Order information.
Next, I need to make another view based on the one shown above. For this one I need to display only the Product Names, Total number of times product has been ordered, and the Total price for that product.
I am having trouble combining the ProductNames whilst also combining the Quantity and ItemTotal.
I have tried using Distinct(ProductName)
CREATE VIEW ProductSummary AS
SELECT DISTINCT(ProductName), Quantity AS OrderCount, ItemTotal
FROM OrderItemProducts
however that just results in..
Which is not correct because it displays duplicate ProductNames
(because they have different OrderCounts).
I would like to Combine the Duplicate rows and total the OrderCount and ItemTotals. What is the best and/or most correct way to do this?
Use group by and sum:
CREATE VIEW ProductSummary AS
SELECT ProductName, sum(Quantity) AS OrderCount, sum(ItemTotal) as ItemTotal
FROM OrderItemProducts
Group by ProductName
Related
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).
Table Inventory
|Variety|Weight|Quantity|
|-------|--------|------|
|Native Chicken|1.6kg|10|
|Native Chicken|1.3|20|
|Chicken Broiler|2.1|30|
|Chicken Broiler|2.3|10|
|Duck|2.1|30|
|Duck|2.3|15|
|Turkey|2.1|30|
|Turkey|2.3|15|
How to get this desired Output for table Reports using the SELECT WHERE, Join and Sum statement in SQL. I've been trying to solve this for a few days now but I haven't seen any codes to answer for this one. Can you help your girl out? Thanks!
Table Reports
|Variety|Qty|
|-------|---|
|Native Chicken|30|
|Chicken Broiler|40|
|Duck|45|
|Turkey|45|
this is the code that I've tried but it's not working.
INSERT INTO reports(qty)
SELECT SUM(qty) FROM (SELECT Inventory.Quantity = "Native Chicken" FROM InventoryTable);
You just need to sum the quantity and group by the column that represents each quantity.
The group by "groups" all like values together where you then sum the quantites for each into a single value
insert into Reports (Variety, Qty)
select Variety, sum(Quantity)
from Inventory
group by Variety
Do a GROUP BY:
insert into Reports (Variety, Qty)
select Variety, sum(Quantity) as Qty
from Inventory
group by Variety
However, I'd consider a view instead of a Reports table, because a view will always have up-to-date data. A separate table requires being emptied and inserted into over and over again.
INSERT INTO reports
SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety;
See https://www.w3schools.com/sql/sql_insert_into_select.asp
I am a complete newbie to sql,
I have an access database table that contains a list of car part items including fields - partnumber, price, description, barcode etc- this table is used to temporarily store the details of a sale. It is a legacy system that does not include a qty ie for multiple items they are duplicate records in this temporary table.
I am trying to produce an "invoice" output in which I need to have The quantity of items (ie counts of duplicate items) list their description and part number and hopefully total each group of duplicate entries prices.
I have tried statements along the lines of
select partnumber, count(partnumber) as qty,
description, price, sum(price) as subtotal
from tblregister
group by partnumber
I know my syntax is way out just after some pointers as to how I achieve what I am after
Try adding the other non-aggregated fields in your select clause to the group by clause, like so:
select partnumber, count(partnumber) as qty, description, price, sum(price) as subtotal FROM tblregister group by partnumber, description, price
That is what I am trying to achieve, I have everything except for the Total column. The total column be the result of the Number * Product Price. I have tried several sum() queries, but none are coming out the way I want and finding it difficult making this new column in the process. Any help would be appreciated, thanks.
You should be able to simply append it to your query like so:
SELECT TransactionID,
CustomerID,
StoreID,
TransactionDate,
Number,
ProductName,
ProductPrice,
Number*ProductPrice AS Total
FROM dbo.table
Question: Select the item and per unit price for each item in the items_ordered table. Hint: Divide the price by the quantity.
1.
select item, sum(price)/sum(quantity)
from items_ordered
group by item;
2.
select item, price/quantity
from items_ordered
group by item;
Have a look at the resultis for flashlights. First one shows average price correctly but 2nd one only takes 28/4 and shows 7, ignoring the 4.5 few rows down. Someone please explain why this is the case.
The used table data from an external website.
SUM() is a group function - so that essentially says go get me all the price and quantities by item, and add them all up to return them in one row.
MySQL is quite forgiving when grouping things and will try to retrieve a rowset (which is why your second example returns something - albeit wrong).
Generally, if you are GROUPing columns (items in your exmaple), you need to return one row per column (item).
Try running the SQL below to see what that looks like.
SELECT item
, SUM(price) AS sum_price
, SUM(quantity) AS sum_quantity
, COUNT(*) AS item_count
, SUM(price) / SUM(quantity) AS avg_price_per_quant
FROM items_ordered
GROUP BY item
ORDER BY item ASC
The first query returns the average price for that item, the second query returns the price for the first item it encounters. This only works in MySQL, the second query would error in SQL Server as no aggegrate function is used. See this post for more details Why does MySQL allow "group by" queries WITHOUT aggregate functions?.