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
...
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)
I am trying to calculate the sum of two calculations multiplied together. Independently, the {Hours} and {Rate} calculations work just fine but when I multiply them together within a sum formula, they normally return a null. The exception is that they will return 0 if the rate is 0.
If anyone has an guidance, it would be appreciated. I would also love to know what I did wrong because I'm teaching myself this.
Select
SFLTX.BF2RDAT as {Date},
SFLTX.BF2DEPT as {Dept},
sum((SFLTX.BF2QTYG / HJOBDR.EDRUNS)) as {HOURS},
(RESRE.ABLABR * ((RESRE.ABBRDP + RESRE.ABBDVP)/100)) as {RATE},
sum((SFLTX.BF2QTYG / HJOBDR.EDRUNS) * (RESRE.ABLABR * ((RESRE.ABBRDP + RESRE.ABBDVP)/100))) as {ABS}
from SFLTX
join ERW.FCALPF on SFLTX.BF2RDAT = ERW.FCALPF.FDATE
join HJOBDR on SFLTX.BF2JOBN = HJOBDR.EDJOB# and SFLTX.BF2SEQN = HJOBDR.EDSEQ#
Join RESRE on SFLTX.BF2DEPT = RESRE.ABDEPT and SFLTX.BF2RESC = RESRE.ABRESC
where SFLTX.BF2RDAT > '1/1/2019' and (right(ERW.FCALPF.FYEAR,2) = ‘19’ and SFLTX.BF2CMODE = 'R')
group by SFLTX.BF2RDAT , SFLTX.BF2DEPT, (RESRE.ABLABR * ((RESRE.ABBRDP + RESRE.ABBDVP)/100))
I attached an image of the data output.
If any of the columns involved in any of the calculations contains NULL values, then the result of the calculation will likewise be NULL. Ensure that either all columns are defined as NOT NULL, or, use a CASE statement to convert any NULL value to 0 or 0.00 (unless you are going to be dividing by that column).
My manager wants to see what is total values of our suppliers shipments, and what is the total values of their invoices recorded. So he can see the differencies and want from suppliers to unsended invoices.
Here is my code.
It is working on accounting table and shipment detail table.
fis_meblag0 is always little from zero because it is 320 account so I mutiply it -1 for get real value.
sth_tutar is value of shipment, sth_vergi is VAT and so the sum of them is equal to sum of total with VAT.
Now the hard part.
Manager wants to diference between them in a other column and also sort the valuez z to a.
I know I can reuse same subselect for the getting totals but I want to know that can I achieve this without using the same subquery again.
I mean in first subselect I have the total, in last column I only need just the total, can I use total without compute it again?
Regards
select
ch.cari_kod as Carikod,
ch.cari_unvan1 as Unvani,
(select (sum(mf.fis_meblag0) * -1)
from dbo.MUHASEBE_FISLERI mf
where (mf.fis_tarih > '20141231' and mf.fis_tarih < '20150201')
and mf.fis_hesap_kod = ch.cari_kod
and mf.fis_meblag0 < 0) as mtoplam,
(Select sum (sth.sth_tutar + sth.sth_vergi)
from dbo.STOK_HAREKETLERI sth
where (sth.sth_tarih > '20141231' and sth.sth_tarih < '20150201')
and sth.sth_cari_kodu = ch.cari_kod
and sth.sth_normal_iade = 0
and sth.sth_tip = 0) as stoplam
from
dbo.CARI_HESAPLAR ch
where
ch.cari_kod like '320%'
try this query:
select Carikod, Unvani, mtoplam, stoplam, mtoplam - stoplam as Grand_total
from
(
-- your full query here
) T
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.
Ok, the title is probably confusing.
I need to use a case statement to calculate the amount of tax to deduct offa value.
This reduced value will be used in a Scalar function to calculate another value in a CTE in a Stored procedure..
How can I re-use this calculated column inside the scalar function?? I have tried the following.
paidminusIPT = case when country = "ireland" then (paid - 1) else paid end,
dbo.fnEarnedPrem(a,b,c,d,paidminusipt,e,f)
please note, this isn't the exact code, just a quick reference..
Is this possible to do?
Regards.
You should use full expression instead of just column name:
paidminusIPT = case when country = "ireland" then (paid - 1) else paid end,
dbo.fnEarnedPrem(a,b,c,d,case when country = "ireland" then (paid - 1) else paid end,e,f)
or use CTE
;WITH C as(
SELECT a,b,c,d,e,f,paidminusIPT = case when country = "ireland" then (paid - 1) else paid end FROM SomeTable)
Select *,dbo.fnEarnedPrem(a,b,c,d,paidminusipt,e,f) FROM C