In the table below I have
Qty Price = DIVIDE(sum(Table A [Inv Value EUR]);SUM(Table A[fN Inv Qty]))
Cost of Material = IF(SUM(Table B[Open quantity]) > SUM(Table A [fN Inv Qty]);
sum(Table A[fN Inv Value EUR]);
[Qty Price]*sum(Table B[Open quantity]))
Total Qty = IF(sum(Table B[Open quantity])>SUM(Table A[fN Inv Qty]);
sum(Table B[Open quantity]);
SUM(Table A[fN Inv Qty]))
So the problem is I'm getting the Total wrong. I know that there is some problem in the formula but I don t know how to resolve it. Moreover, I would like that when I have a blank cell that was considered as 0.
In the Example, I should have as Total Cost of Material = 29767,16 €.
Related
I need to create a calculated measure Internal Quantity for the following equation,
Internal quantity = Quantity where [X].[Category Id] = A or B/ Quantity
The calculated measure Quantity calculates the SUM of a column. So, for example if the values for [X].[Category Id].&[A] = 50 and [X].[Category Id].&[A] = 20 and the Quantity = 100, the Internal Quantity should be 0.5 and 0.2 respectively.
I have written the following MDX expression to achieve this,
Case [X].[Category Id]
When [X].[Category Id].&[A] Then [X].[Category Id].&[A]/[Measures].
[Quantity]
When [X].[Category Id].&[B] Then [X].[Category Id].&[B]/[Measures].
[Quantity]
else NULL
End
However with the above expression both the [X].[Category Id].&[A] and [Measures].[Quantity] return the same value probably as they are inside a case statement.
I would really appreciate any help with this. Thanks in advance!
I have a data like this in Table Receipts, for the plus value is the amount of receipt of goods while the negative value in case of returns.
In the picture above, the Receiver_No 01267A is the receipt of the first item with Qty_Received 1. Then there is the return of goods on the receipt number of goods 01268A. I want this transaction not to show up.
Here's statement where i have tried.
select I2.Receiver_No,
I2.Purchase_order,
I2.PO_Line,
I2.Qty_received
from (Select Purchase_order,
PO_Line
from Receipts
group by Purchase_order,
PO_Line
having sum(qty_received) <>'0') I1 inner join
Receipts I2 On
I1.Purchase_Order = I2.Purchase_Order and
I1.PO_Line = I2.PO_Line
But the result did not meet with i want.
Please Help.
This will work for the very limited data that you have provided. If you care to provide more information about what the data looks like, then it will be easier to come up with a better solution.
If you have data with a lot of lines that have identical quantities, then this solution will not work.
SELECT
r.Receiver_No, r.Purchase_order, r.PO_Line, r.Qty_received
FROM
receipts r
WHERE
NOT EXISTS (SELECT
purchase_order
FROM
receipts s
WHERE
r.qty_received = -s.qty_received
)
If your requirement is to fetch rows with quantities less than one and not the negative ones :
SELECT * FROM Receipts WHERE ABS(QtyRecvd) >0 AND ABS(QtyRecvd) < 1
Try This
SELECT * FROM Receipts where Qty_received>0 AND Qty_received < 1
Please see below explanation of my problem. I have 2 tables.
[TABLE1: LocationsOFproducts - Keep data about location of products and quantity][1]
[TABLE2: Datahistory - keep data about stock movement ( removing/adding product from/to stock )][2]
[QUERY1: SumDataHistoryQuery - Making Sum of Products moved from the same location][3]
QUERY2: TOTAL QTY - Counting how many products left in some location after stock movement
SQL Code:
SELECT LocationsOFproducts.[Bay no]
,LocationsOFproducts.[Product Code]
,LocationsOFproducts.LocationQTY
,SumDatahistoryQuery.SumOfQTY
,([LocationQTY]) + ([SumOfQTY]) AS TOTALQTY
FROM LocationsOFproducts
INNER JOIN SumDatahistoryQuery ON (LocationsOFproducts.[Product Code] = SumDatahistoryQuery.[Product Code])
AND (LocationsOFproducts.[Bay no] = SumDatahistoryQuery.[Bay no])
AND (LocationsOFproducts.[Product Code] = SumDatahistoryQuery.[Product Code])
GROUP BY LocationsOFproducts.[Bay no]
,LocationsOFproducts.[Product Code]
,LocationsOFproducts.LocationQTY
,SumDatahistoryQuery.SumOfQTY
,([LocationQTY]) + ([SumOfQTY])
ORDER BY LocationsOFproducts.[Product Code];
RESULT:
I expect a little bit different result.
My query should check is there any more value with Bay no,product code and QTY in Datahistory table, if not I want to fill that space with data from LocationOfproducts table and in the field SumOfQTY fill those
values with 0 to make a sum in TOTALQTY column.
Please see below what I need to get:
Please see query what I need to get - I maked that in Photoshop by cutting and pasting fields:
Can anyone know how to write the right query?
The error is 'Operation must use an updatable query'.
I am trying to update the 'orders' table with the information below, however only the price of 1 item will be provided through a text box and I am trying to calculate the total order value using the quantity ordered, which will already be in this table.
The code below includes the data taken from the variables. With the 2 in 'VAT = 2' and 'price = 2' being the price of one single unit (£2.00). The total order value will be stored within the 'price' field and the VAT should be calculated using the same code, but times by 0.2 to give the 20% VAT.
UPDATE orders
SET Invoice_number = 'IN9999',
delivery_note_number = 'DN6000',
price =2 *
(SELECT quantity
FROM orders
WHERE purchase_order_number = 'PO7512'
),
VAT = (2 *
(SELECT quantity
FROM orders
WHERE purchase_order_number = 'PO7512'
)/100) * 20,
shipping = 3
WHERE purchase_order_number = 'PO7512'
Maybe I can't use nested query's this way. I'm not sure, but any help would be appreciated :) Thanks!
Instead of subquerying, you can access the whole record directly in the update, like so:
UPDATE Orders
SET Invoice_number = 'IN9999',
Delivery_note_number = 'DN6000',
Price = 2 * quantity,
VAT = (40 * quantity)/100,
Shipping = 3
WHERE purchase_order_number = 'PO7512'
Note that with fractions it's always better to multiply first and divide later.
From r In ReceiptLines
Where
r.RECEIPT.RECEIPTDATE >= _reportStartDate
And r.RECEIPT.RECEIPTDATE <= _reportEndDate
Let amount = r.QUANTITY * r.PRICE
Let discount = r.RECEIPTDISCOUNTs.Sum(Function(d) d.DISCOUNT)
where discount > 0
Group By Department = r.ITEMSTYLE.ITEM.CATEGORY.DEPARTMENT.DEPARTMENTNAME
Into Sales = Sum(amount - discount),
Average = Average(amount - discount),
Count = Count()
I am fetching all departments and their sales, average, count from the ReceiptLine, Receipt, ReceiptDiscount tables. The problem i am facing is, if i remove where discount > 0, I am getting null exception. But if I include that, then I only get sales that has discount.
How would I write query that bring all sales less discount (if it has one). Any help is highly appreciated.
This is a common pitfall with LINQ2SQL.
The function SUM in SQL returns null if there are no items in the collection, but the signature of Enumerable.Sum() returns an int. This gives a runtime exception when the SQL query return null where the LINQ2SQL provider expects an integer.
The solution is to cast the result of the sum to a nullable integer and use GetValueOrDefault to convert the null-case to 0.
Replace
Let discount = r.RECEIPTDISCOUNTs.Sum(Function(d) d.DISCOUNT)
with
Let discount = CType(r.RECEIPTDISCOUNTs.Sum(Function(d) d.DISCOUNT), Integer?).GetValueOrDefault(0)
Have you tried:
...
Let amount = r.QUANTITY * r.PRICE
Let nDiscount = r.RECEIPTDISCOUNTs.Sum(Function(d) d.DISCOUNT)
Let discount = IIf(nDiscount == Nothing, 0, nDiscount)
Group By Department = r.ITEMSTYLE.ITEM.CATEGORY.DEPARTMENT.DEPARTMENTNAME
...