How to calculate new value for field in Access - sql

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

Related

Need SQL query in Access to Show Total Sales by Catalog#

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])

Calculate Average cost Price every new date based on hte Stock

i'm trying to create a query in Access to calculates for all records the Average Cost price of another query that contains multiple stock buy and sell on a range of dates .
The Average Cost Price formula in T0 is: ((Accumulated Position T-1 * Av Cost Px T-1) + Qtty T0* Price T0)/ (Accumulated Position T-1+ Qtty T0)
If the record is a Sell, I just subtract the quantity of the Accumulated Position and maintain the Average Cost
I cant use sub query for it because i would have to get a record on the same column for the same query, so its impossible. I'm actually lost on how i would do that, bcse I have around 30.000 records on stock trading that I need the average cost price for each date. What is the best path to follow on that case?
SELECT qry_sample_data_excel_summ.Date, qry_sample_data_excel_summ.Ticker, qry_sample_data_excel_summ.buy_sell, qry_sample_data_excel_summ.SumOfQuantity AS Qtty, qry_sample_data_excel_summ.AvPrice AS Price, (SELECT Sum(Dupe.SumOfQuantity)
FROM qry_sample_data_excel_summ AS Dupe
WHERE ((Dupe.Date)<qry_sample_data_excel_summ.Date) AND ((Dupe.Ticker)=qry_sample_data_excel_summ.Ticker)) AS [Acumm_Position T-1], "?" AS Av_Cost_Px
FROM qry_sample_data_excel_summ
GROUP BY qry_sample_data_excel_summ.Date, qry_sample_data_excel_summ.Ticker, qry_sample_data_excel_summ.buy_sell, qry_sample_data_excel_summ.SumOfQuantity, qry_sample_data_excel_summ.AvPrice, "?";
<table><tbody><tr><th>Date</th><th>Ticker</th><th>buy_sell</th><th>Qtty</th><th>Price</th><th>Acumm_Position T-1</th><th>Av_Cost_Px</th></tr><tr><td>2-Jan-20</td><td>BA</td><td>B</td><td>17,000</td><td>333.00</td><td> </td><td>333.00</td></tr><tr><td>2-Jan-20</td><td>BA</td><td>S</td><td>(5,000)</td><td>332.36</td><td> </td><td>333.00</td></tr><tr><td>2-Jan-20</td><td>KHC</td><td>B</td><td>4,000</td><td>31.64</td><td> </td><td>31.64</td></tr><tr><td>3-Jan-20</td><td>BA</td><td>B</td><td>1,000</td><td>330.53</td><td>12,000</td><td>332.81</td></tr><tr><td>3-Jan-20</td><td>BA</td><td>S</td><td>(2,000)</td><td>333.74</td><td>12,000</td><td>332.81</td></tr><tr><td>6-Jan-20</td><td>BA</td><td>B</td><td>1,000</td><td>328.32</td><td>11,000</td><td>332.44</td></tr><tr><td>6-Jan-20</td><td>BA</td><td>S</td><td>(7,000)</td><td>334.45</td><td>11,000</td><td>332.44</td></tr><tr><td>6-Jan-20</td><td>KHC</td><td>B</td><td>4,000</td><td>31.09</td><td>4,000</td><td>31.37</td></tr><tr><td>7-Jan-20</td><td>BA</td><td>S</td><td>(5,000)</td><td>341.56</td><td>5,000</td><td>324.83</td></tr></tbody></table>

SSRS Group Subtotal and Total

I'm trying to get the subtotal for WO Total Cost but where that WO Number appears multiple time in the Division group I don't want to add them together. I only want the cost for WO Total to appear once in the report for each division. As it is now the work order number 40321 has a WO Total Cost of $362.24 and because it appears for each laborer it is added in the total represented in the green total line. Can anyone tell me how to prevent summing WO Total Cost where the WO Number appears more than once in a Division group?
Thanks for the reply Alan. Here is a screenshot of the design view in Report Builder. So, what I'd like to see is a summing of only the WO Total cost once for each report for each WO Number. The WO Total cost represents all costs (Equipment, Labor, and Material) for a given work order. So, at it stands now, the report is summing work order 40321 three times and giving us an incorrect total.
I've grouped the report by Division so we can see costs for a particular division for a given time period. I've also grouped the report by Person Labor so we can see how much a particular laborer is costing us for a given period of time.
What I don't know is how to do is prevent the report from summing the WO Total cost where the WO Number appears multiple times in the results for a given division.
I'm not sure you will be able to do this directly in the report design without some convoluted custom code. Other people maybe will have more inspiration but I can't see how to do it. You cannot even look for unique values to sum as it's possible that 2 WO numbers could have the same cost.
Without knowing what your dataset/database tables look like I can't offer much more but here are some approaches I might consider if I was doing this, both mean doing the work in SQL.
**Option 1 **
I would consider calculating this in your query in a new column which should be fairly simple. Each row would end up with the total amount for the AssetGroup. So, if you had 20 records for AssetGroup A and 10 records for AssetGroup B than all the 'A' records would have the same 'AssetGroupTotalWOAmount' value and all the 'B' records would have the same 'AssetGroupTotalWOAmount' value.
Then in your report you can simply use =FIRST(Fields!AssetGroupTotalWOAmount.Value) to get the correct number. This will get the first value within the scope of the expression, so your case within the ASSETGROUP row group.
**Option 2 **
Create a separate dataset with just the amounts you need, with a single record per AssetGroup . So probably just two columns, AssetGroup and AssetGroupTotalWOAmount
In the report you could then use a LOOKUP to get the correct value.

Adding shipping total is making my data inflate

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

How do I perform math in SQL query on certain conditions?

I have a few tables I am querying, Item movement, and price. There are three price levels, regular price, reduced price, and sale price. I am attempting to get a markdown (price that item sold at when on sale minus either the regular or reduced price). My item movement table contains only the price the unit sold at and the price type of that sale. I am currently selecting only the items that sold on the sale price type. Next I have to find out whether the item was on a regular or reduced price, which I determine by looking at my price table. The price table has my regular price and reduced price and if the reduced price is null then it is not currently reduced.
How do I tell SQL that I want a column named "markdown" that is essentially (IF price.tprprice not null AND price.tprenddate > #StartDate give me column (price.baseprice - price.tprprice) * itemmovement.qtysold AS markdown ELSE give me column (price.baseprice - itemmovement.price) * itemmovement.qtysold AS markdown)
I need to return the result of each calculation performed on each row into the same column titled Markdown.
Can anyone tell me how this query should be structured?
case when price.tprprice is not null AND price.tprenddate > #StartDate
then (price.baseprice - price.tprprice) * itemmovement.qtysold
else (price.baseprice - itemmovement.price) * itemmovement.qtysold
end as markdown
You would do it with a case statement which works in most databases.