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!
Related
SQL Case statement with formula returns the wrong value in the column. What I am trying to do is to calculate the absolute value based on several conditions. First of all when the risk_class is either "Not enough samples" or "Regular price" the absolute value should be zero. Secondly, when the difference between the mode and the price is less than zero, the absolute value should be zero. If all these conditions are not met, then the risk_abs should be the difference between the mode and price multiplied by the quantity column corresponding with the row.
UPDATE dbo.import_company
SET risk_abs = ROUND(t.risk_abs, 0)
FROM (
SELECT invoice_ref, risk_class, modus, price, quantity,
CASE
WHEN risk_class = 'Onvoldoende observaties' THEN 0
WHEN risk_class = 'Gefactureerd tegen reguliere prijs' THEN 0
WHEN (modus - price) < 0 THEN 0
ELSE (modus - price) * quantity
END AS risk_abs
FROM dbo.import_company
)
t WHERE t.invoice_ref = dbo.import_company.invoice_ref
SELECT invoice_ref, article_code, quantity, price, modus, risk_class, risk_abs
FROM dbo.import_company
WHERE risk_class = 'Gefactureerd tegen reguliere prijs'
I don't understand why me result is different, and the formulas don't work. See below
I don't get how the 140 is calculated and why it ignores all statements?
Any advice and tips on what I am doing wrong are welcome!
There's no aggregation here or any other reason I can see to use a subquery to do the CASE logic. So we can get rid of that and the too-loose attempt at correlation between the subquery and the table:
UPDATE dbo.import_company
SET risk_abs = ROUND(CASE
WHEN risk_class = 'Onvoldoende observaties' THEN 0
WHEN risk_class = 'Gefactureerd tegen reguliere prijs' THEN 0
WHEN (modus - price) < 0 THEN 0
ELSE (modus - price) * quantity
END, 0)
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 €.
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
...