I am trying to add everything in te data (dollar_value_us, QUANTITY) but not add shipping total since each transaction number has multiple items but the customer only paid shipping once. I am using the below data:
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=01693db7ce05b062804cedeb3b3a7e73
below is the query which I am using on my actual databse:
select QUARTER_DATE ,COUNTRY,sum(DOLLAR_VALUE_US), sum(QUANTITY), max(SHIPPING_TOTAL)
from transaction_detail_mv
group by QUARTER_DATE,COUNTRY
the final output for usa should have shipping total amount of 35
Try using Max(ShippingTotal) instead of Sum so that you only get the shipping total once per order.
See this modification to your dbfiddle: https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=96827f2a82976bdf5f07d8d1831f391c
Related
I'm trying to pull together a table which shows the summary of invoice totals by invoice number and customer code. I think I have it mostly down but when I try to summarize the line item prices which would give an invoice total, I get the parameter value input box for the line_price field.
When you go into the table, the first layer of data has invoice number and customer code. From there, you can drill in and you get line item numbers per invoice and the total value per line. I need to sum that total value per line and show it at the customer code and invoice number level.
Below is what I have so far:
SELECT CUS_CODE, INV_NUMBER, SUM(LINE_PRICE) AS InvoiceTotal
FROM INVOICE
GROUP BY CUS_CODE, INV_NUMBER;
You must include the subdata, the invoice lines' table, like this:
SELECT
CUS_CODE, INV_NUMBER, SUM(LINE_PRICE) AS InvoiceTotal
FROM
INVOICE
INNER JOIN
INVOICE_LINE
ON INVOICE.ID = INVOICE_LINE.INVOICE_ID
GROUP BY
CUS_CODE, INV_NUMBER;
I have a table named Sales_Line_Items. In it I have the following fields; order#, Catalog#, Whole_Sale_Price, Qty_Ordered. I have created a query using this table that sums up the Qty_Ordered field and returns a Revenue field with a total per order line. What I need is a query that shows me each the quantity of each item sold and the total sales by item. This is the query I wrote for the total revenue:
SELECT
Sales_Line_Items.[Order#],
Sales_Line_Items.[Catalog#],
Sales_Line_Items.Wholesale_Price,
Sum(Sales_Line_Items.Qty_Ordered) AS SumOfQty_Ordered,
Sum(Sales_Line_Items.[Qty_Ordered]*Sales_Line_Items.[Wholesale_Price]) AS Revenue
FROM Sales_Line_Items
GROUP BY
Sales_Line_Items.[Order#],
Sales_Line_Items.[Catalog#],
Sales_Line_Items.Wholesale_Price;
The result was a new field with header ' Revenue'. The other fieds listed are Order#, Catalog#, Wholesale_Price, and SumofQty_Ordered fields. This is fine, if I want to know what every sales line totals up to within each order. But I want to show the total sold by Catalog# so that I can view each product sales total.
I was able to figure this out by adding the Totals line in Design View, and grouping by Expression in the query field and leaving the Catalog# field alone. The query I used looks like this;
Total: Sum([Sales_Line_Items]![Wholesale_Price]*[Sales_Line_Items]![Qty_Ordered])
I don't understand why my sql is not running,
it pop out a window say
"Your query does not include the specified expression ' SUM(SaleRecord.Number)*(product.Price' as part of an aggregate function"
SELECT SUM(SaleRecord.Number)*(Product.Price) AS TotalIncome
FROM Product, SaleRecord
WHERE Product.ProductID=SaleRecord.SaleProduct;
Product.Price is not part of the aggregate. Presumably, you intend:
SELECT SUM(SaleRecord.Number * Product.Price) AS TotalIncome
FROM Product INNER JOIN
SaleRecord
ON Product.ProductID=SaleRecord.SaleProduct;
Note that I also fixed the archaic join syntax.
You asked in my previous answer:
"thank you, I just make some mistake, now it is working. And sorry to
bother you more, I want to select the product who sell the most out,
how can I do it, I try to add MAX(xxx) on it, and it don't work"
Now, I am by no means an expert, but there are two processes going on. Your language is confusing so I'm going to assume you want to know which product sells the most in $$ terms (rather than count. For example, you might sell 1,000 $0.50 products, equallying $500 total sales, or 10 $500 products, totallying $5000. If you want the count or the dollar value, then the method changes slightly).
So the first process is to get the total sales of each product, which I outlined above. Then you want to nest that inside a second query, where you then select the max. I'll give you the code and then explain it:
SELECT ProductID, MAX(TotalSale)
FROM (
SELECT P.ProductID, SUM(S.Number)*P.Price AS TotalSale
FROM Products as P, SaleRecords as S
WHERE product.Productid = SaleRecord.SaleProduct
GROUP BY Product.ProductID
)
It's easiest to imagine this as querying a query. Your first query is in the FROM() statement. That will run and give you the output of total sale per product. Then the second query is ran (the top most SELECT line) that selects the productID and the sale amount that is the largest among all the products.
Your teacher may not like this since nesting queries is a little advanced (though completely intuitive IMO). Hopefully this helps!
You brackets are wrong - for each row you want to multiply the price by the number, and only then sum them:
SELECT SUM(SaleRecord.Number * Product.Price) AS TotalIncome
FROM Product, SaleRecord
WHERE Product.ProductID = SaleRecord.SaleProduct;
You have a bracket error:
SELECT SUM(SaleRecord.Number * Product.Price) AS TotalIncome
FROM Product INNER JOIN
SaleRecord ON Product.ProductID = SaleRecord.SaleProduct;
This is because you're not indicating which column to group by. The line you wrote is:
SUM(SaleRecord.Number) * Product.Price
Which sums all of the sale quantities (regardless of differences in product ID) and multiplies it by the price right? Well what if you have multiple products with different prices? Basically, you are doing a one to many match, where you have a total that is the sum of all the sales, multiplied by multiple prices. What you need is a group by command. I would modify your code to say:
SELECT Product.ProductID, SUM(SaleRecord.Number)*Product.Price AS TotalSales
FROM Product, SaleRecord
WHERE product.Productid = SaleRecord.SaleProduct
GROUP BY Product.ProductID
That should take care of it, telling the dbms to group each product together, sum the number of sales and then multiply by the price of that product.
You can nest that inside another query to get total Income:
SELECT SUM(TotalIncome)
FROM ( **the above code here)
EDIT: Or you can do it like the ways listed above where your query creates a TotalIncome for each ORDER, and then sums them all together. my way creates a total sale for each PRODUCT and then sums all the products
I've got a few problems with a database I have created.
I want to calculate a Total Price (Sandwich Quantity multiplied by Sandwich Price). I had it working before, but I had to delete Sandwich Price from the OrderDetailsT table of which it was originally in. I'm now having issues with this calculation, as I cannot make a calculation in the OrderDetailsT table (Sandwich Price isn't there).
How can I apply the Discount to the Total Price if the Total Price is more than $50 for instance? After the Discount has been applied to the Total Price field, I would also like to store it in the NewPriceAfterDiscount field.
Here is an image detailing my situation:
You have multiple questions in one:
But, first of all. As the image shows, why do you have a left join between OrderDetails an Sandwich? In a order calculation you don't need not ordered sandwiches.
To total price calculation:
Add a new column to the query grid (assuming discount is a percentaje stored has a number between 0 and 1):
[SandwichT].[SandwichPrice] * [OrderDetailT].[SandwichQuantity] * [OrderDetailT].[Discount]
To store total price: you can use the above formula, but using a update query.
If you plan to show the prices in a form or in a report:
you can do de calculations on the fly (and don't store the total
price)
or you should update the total price un one query and then build another
query as datasource of the form/report.
another posibility (my recomendation) is to store the total in the input form
I am running a query to retrieve a list of invoices that have not been approved yet. This query is generating daily alerts via email. The email contains an HTML table of the client name, inv #, sub-total, adjustments, final total. I want to be able to list a summary before the table of the total sub-total, total adjustments, and total final total of the entire table.
ex:
Total Sub-total: 10
Total Adj: -8
Total Billed: 2
Sub-Total Adj Total
7 -6 1
3 -2 1
I tried SUM(sub-total), but it only retrieves the first sub total ("7" in this example) instead of adding them all up. Help?
The query I am using is:
SELECT dbo.CurInv.subtotal AS "subtotal", dbo.CurInv.CIAdj AS "Adj",
dbo.CurInv.CIAdj+dbo.CurInv.subtotal+dbo.CurInv.CISTax AS "TotalInvoice",
SUM(dbo.CurInv.CIAdj) AS "Total Write Up(Down)"
FROM dbo.Clients, dbo.CurInv
I ended up just creating a new table that calculated the totals for each row and then added that table to the query.
You can used the UNION operator. You need to define a column to designate the individual vs total data.
SELECT
dbo.CurInv.subtotal AS "subtotal",
dbo.CurInv.CIAdj AS "Adj",
dbo.CurInv.CIAdj+dbo.CurInv.subtotal+dbo.CurInv.CISTax AS "TotalInvoice",
SUM(dbo.CurInv.CIAdj) AS "Total Write Up(Down)"
FROM
dbo.Clients, dbo.CurInv
UNION
SELECT
SUM(dbo.CurInv.subtotal) AS "subtotal",
SUM(dbo.CurInv.CIAdj)AS "Adj",
SUM(dbo.CurInv.CIAdj+dbo.CurInv.subtotal+dbo.CurInv.CISTax) AS "TotalInvoice",
SUM(dbo.CurInv.CIAdj) AS "Total Write Up(Down)"
FROM
dbo.Clients, dbo.CurInv