I'm working on an access database at the moment, where I have multiple fields.
Product Quantity (Entered by User)
Product Price (Retrieved by Database)
Total Price (Product Quantity * Product Price)
Discount
Discount needs to be calculated, however I don't know how to set it to 30% on the condition that TotalPrice is more than 50. It would be useful if the TotalPrice automatically updated if it was more than 50 with the discount too.
Set up a new query that brings in Product Quantity, ProductPrice and TotalPrice. Then, in the Design View of the query, add this field:
Discount: IIF((ProductQuantity * ProductPrice) > 50, 30, 0)
If you're not familiar with IIF statements, the above reads: "If ProductQuantity times ProductPrice is greater than 50, then set Discount = 30, otherwise set Discount = 0"
This will set your discount = 0 if Total Price is less than or equal to 50, so edit that last part if it needs to be something else. Also, I made assumptions on your field names, so you may need to tweak those too, but you get the idea.
If you need Discount to actually reflect 30% of Total Price, then if would look like this:
Discount: IIF((ProductQuantity * ProductPrice) > 50, (ProductQuantity * ProductPrice) * .30, 0)
If this is a data entry screen You can add an After_Update Event to Sandwich Price And Sandwich Quantity
Private Sub SandwichPrice_AfterUpdate()
update_total
End Sub
Private Sub SandwichQuantity_AfterUpdate()
update_total
End Sub
Private Sub update_total()
Dim total AS Double
If IsNull(Me.SandwichPrice) OR IsNull(Me.SandwichQuantity) Then Exit Sub
total = Me.SandwichPrice * Me.SandwichQuantity
SELECT CASE total
CASE 51 to 1000
Me.Discount = 0.3
CASE Else
Me.Discount = 0
END SELECT
Me.TotalPrice = total - (total * Me.Discount)
End Sub
Doing it this way will allow you to add multiple tiers of discount by adding CASE Statements i.e. You could add CASE 10 TO 20 with a new discount and it will apply when TotalPrice is >=10 And <= 20.
This will allow your lookup query to return the correct values from discount. I don't think this should be handled in the lookup query but rather in the data storage itself.
Related
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
I'm creating a system where I must deduct and add by one if the item is borrowed/returned. In my system I have 2 tables:
tblItems(_itemnumber_, _itemname_, _quantity_)
tblBorrow(_dateborrowed_, _datedue_, _status_)
Where if I borrowed an item the status will be borrowed and the quantity will deducted by 1 and if the status is returned the quantity will added by 1. This is my idea(I already how to add and update) but i don`t know how to code this idea. I'd really appreciate your help.
I`m using imports system.data.oledb
First, the underscores might be eliminated to improve readability.
Next, tblBorrow needs itemnumber, else all Borrowings look the same.
tblBorrow(_itemnumber_,_dateborrowed_, _datedue_, _status_)
Then the Borrow_Clicked button would have code to the effect of
If tblItems for this _itemnumber_ has _quantity_ <= 0 then
msg"There is no Quantity to Borrow"
exit
else
Update tblItems Set _quantity_ = _quantity_ - 1 Where _itemnumber_ = x
Insert tblBorrow Item#, Date, Due, "Borrowed"
End If
There would also be a Returned_Click button, with code to the effect of
If tblBorrow for this Item# exists with a "Borrowed" status then
Update tblItems Set _quantity_ = _quantity_ + 1 Where _itemnumber_ = x
Update tblBorrow Set Status = "Returned"
else
msg"Item status is not Borrowed"
End If
Each of these should also edit the Item# to insure that is it valid.
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.
I have a button at the end of each record on a continuous form and it needs to do this:
Private Sub Update_Click()
Dim SellP As Double
Dim BuyP As Double
Dim Profit As Double
SellP = DLookup(SellPrice, Flips, [Current])
BuyP = DLookup(BuyPrice, Flips, [Current])
Profit = SellP - BuyP
Flips.Profit = Profit
End Sub
Now I know this isn't the correct code but I hope it will give you an idea of what it needs to do, essentially:
Find the SalePrice, find the BuyPrice, minus the BuyPrice from the SalePrice and make the result Profit, then populate the Profit field with profit..
Thanks!
The columns of the current record of the bound table/query are directly available in the code.
You can just write e.g.
Profit = SalePrice - BuyPrice if these fields are all part of the bound data.
You might then move this code in the "AfterUpdate"-Event of both the SalePrice- and the BuyPrice-Textfields, maybe like this:
If IsNull(salePrice) Or IsNull(buyPrice) Then
Profit = 0
Else
Profit = salePrice - buyPrice
End If
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
...