Calculate Average cost Price every new date based on hte Stock - sql

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>

Related

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.

Summing up the price of production and selling prices

I need to write query and calculate the total CostPrice’s and total Sellprice’s related to Material of ID=1. I want to calculate total costs so I have to include the QuantityColumn from the table Materials right?
SELECT SUM((CostPrice) * M.Quantity) AS 'TotalCostExclusive', SUM((SellPrice) * M.Quantity) AS 'SellPriceExclusive'
FROM Materials AS M INNER JOIN Warehouse AS W
ON M.Warehouse_id = W.Id
WHERE M.Material_Id = '1';
I tried this, but I'm not sure the result is equal to the required task.
Now I want to write new query to include the Discount on both cost and selling prices.
Since I’m a beginner and this data model is for student purpose, I would like to know why I must to have a table WarehouseIdentities. What exactly this table doing here and what role it has.

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.

How to calculate new value for field in Access

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

Pivot Table Report

I am trying to create a pivot table with data from a SQL database. The pivot table is basically a process capability report that requires certain information. I have created an Excel file that sort of does what we are trying to accomplish, but I don't think my calculations are quite accurate. I know after doing some research that there are Pivot Tables within SQL but I don't know how to get them to work.
The table that my data is stored in has thousands of records. Each record has the following information: DATE, TIME, PRODUCT_NO, SEQ, DATECODE, DATECODE_IDH, PRODUCT, LINE, SHIFT, SIZE, OPERATOR, SAMPLE_SIZE, WEIGHT (1-12), WEIGHTXR, LSL_WT, TAR_WT, USL_WT, LABEL, MAV, LINE_SIZE
For the report, I need to group the data based on product and by line. Since the product isn't consistent, each product can be described by TAR_WT. So the grouping will be a combination of TAR_WT and LINE_SIZE. I need to count how many instances of that product were measured which will be the number of measurements (each individual weight which is 12 weights per record). I also need to find the minimum, maximum, and average of all of the weights per product (again 12 weights for every record). After those values are obtained, I have to calculate the Standard Deviation, Cp, and Tz of the values (staistical calculations) and report all the information.
Date Time Product No Seq DateCode Internal DateCode&ProductNo Product Description Line Size Weight1 Weight2 Weight3 Weight4 WeightXR LSL_WT TAR_WT USL_WT LABEL MAV
8/3/11 0:37:54 1234567 23 DateCode Internal DateCode&ProductNo Product Description L-1A 50 1575 1566 1569.5 1575.5 1573.4 1550.809 1574.623 1598.437 1564.623 1525.507 L-1A_50