TSQL - How to find out if a Users Currency rate is correct based on another currency rate for the same user - sql

We currently have an issue where some rates for users have not been entered correctly and I have been tasked to find out which entries are incorrect, but unfortunately I am bit stuck.
The requirements are as follows;
If user only has a GBP charge out rate, then do nothing
If user has Charge out rate in GBP + USD then;
GBP must = USD / 1.55 otherwise FALSE
The following is how I started it:
We have a table called TIMERATE where a users charge out rate is entered, with a new line added every time the rate changes. It is possible for the user to have a charge out rate in more than one currency.
Here is a cut of the data in the table for a single user;
tkinit tkeffdate tkrt01 tkrtcur
LAU 01/02/2014 170 GBP
LAU 01/08/2014 260 GBP
LAU 01/12/2014 130 GBP
LAU 01/08/2014 260 USD
LAU 01/12/2014 210 USD
LAU 01/02/2015 260 USD
To find out the latest rate for each currency:
SELECT
TKINIT as Timekeeper,
MAX (tkeffdate) as MaxEffectiveDate,
tkrtcur as Currency,
Cast (NULL as decimal (16,2)) as Rate
INTO
#LatestRate
FROM
TIMERATE
GROUP BY
TKINIT, tkrtcur
ORDER BY
TKINIT
Then I updated the Rate in the temp table
update
#LatestRate
Set
Rate = tkrt01
from
#LatestRate
JOIN
Timerate on TKINIT = Timekeeper
and tkrtcur = Currency
and tkeffdate = MaxEffectiveDate
So i now have the latest rate for each currency for each user, but I do not know how to manipulate the data to fit the requirements
Does anyone have any ideas? Have I gone about this the wrong way?

Rabbit's answer is the easiest way to compare if the conversion was done correctly.
Going off of your initial steps, you can run this query on the #LatestRate table to give you the results. Pls ensure the comparison in the case statement is working correctly. I have not tested this code.
SELECT
GBP.Timekeeper as Timekeeper,
GBP.MaxEffectiveDate as EffectiveDate,
GBP.Currency as GBP,
GBP.Rate as GBPRate,
USD.Currency as USD,
USD.Rate as USDRate,
case
when USD.Rate is null then 'GBP Only'
when round(USD.Rate/1.55,2)=round(GBP.Rate,2) then 'Correct Conversion'
else 'FALSE'
end as IsConversion
FROM #LatestRate GBP
INNER JOIN #LatestRate USD
ON GBP.Timekeeper=USD.Timekeeper
and GBP.MaxEffectiveDate=USD.MaxEffectiveDate
and GBP.Currency='GBP' and USD.Currency='USD'

Join the table to itself on the tkinit and date where one side has the gbp and the other side has usd. This will give you both on one row so you can check to see if the rate is correct.

Related

SUMIF when cells do not contain "text a" OR "text b" (LibreOffice-Calc)

I've got a spreadsheet of purchase information that has the following format:
Account
Category
Withdraw
Deposit
CC
Home
21.22
Checking
Payment
21.22
CC
Payment
21.22
CC
Transit
50.00
Checking
Income
5000
CC
Food
31.49
CC
Hobbies
15.00
Checking
Transfer
3000
Savings
Transfer
3000
I want to sum all values in the Withdraw column that do not have the category "Payment" or "Transfer" in the same row.
I can get one or the other with something like =SUMIF(B:B,"<>Payment",C:C), but I can't figure out how to exclude both.
=SUMIF(B:B,"<>Payment"&"<>Transfer",C:C) doesn't give me the correct value, and =SUMIF(B:B,AND("<>Payment","<>Transfer"),C:C) gives me an error.
I feel like there's an operator combination for this that I just haven't been able to find?
Completely missed that SUMIFS existed, as #xerx593 pointed out. =SUMIFS(C:C,B:B,"<>Payment",B:B,"<>Transfer") gives the results I was looking for.

Creating Dupe records in SQL Server

Background: We are in process of migrating our ETL from an old database to a new database which record data differently. In order to be as close to our old system (which creates 1 record for sale and another if the sale was cancelled and the NEW system creating only the cancelled record), I need to create another record for these sales so that I match 1:1 between the old and new database.
What would be an effective way to achieve this. For example, I can get 10k records that need duplication. These are based on specific conditions in the database.
Here is the sample of data :
AcctNum CancelFlag SaleDate SalePrice
-----------------------------------------------------------------------
1 Y 2022-06-27 100.00
2 Y 2022-05-10 156.00
Desired output:
AcctNum CancelFlag SaleDate SalePrice
---------------------------------------------------------------------
1 Y 2022-06-27 100.00
1 2022-06-27 -100.00
2 Y 2022-05-10 156.00
2 2022-05-10 -156.00
Please note that on the duped row, I won't have the CancelFlag = Y and the SalePrice will be flipped to a negative sign to indicate the cancellation of the sale.
Thanks for taking the time to read.
So if I understand correctly, you basically want to create new rows for each of those rows selected , with the CheckFlag off, and the amount negated - right?
So try something like this:
INSERT INTO dbo.YourTable (AcctNum, CancelFlag, SaleDate, SalePrice)
SELECT
AcctNum, NULL, SaleDate, SalePrice * -1.0
FROM
dbo.YourTable
WHERE
CancelFlag = 'Y'
AND SalePrice > 0.0;
Does that do what you're looking for? You might need to tweak your WHERE clause to get precisely those existing rows from the table that you're really interested in.

i get wrong data with WebI report Filter

I Created webi report result in crosstabe table,
when I try to get only the customer who made trans more than 50 in total like the customer 222 all the data less than 50 not show up,
example:
customer (222)
did two actions 1- 3/25/2018 with amount 209 gb,
2- 3/29/2018 with amount 14 gb,
the sum of both is (223),
I need to get all customer who made more than 50 actions in all days, when I try that by adding a filter the action in day 3/29/2018 is not shown & only the action in 3/25/2018 is what I get.

SSRS - Suppression and column total on duplicates

I have a report where I need to suppress columns of text when they are dupicate (done) but also total two columns of figures (net debit & credit) leaving the other two columns (ccy & ccy amount) itemised as they are.
When dealing with data like:
DESCRIPTION DEBIT CREDIT CCY CCY AMOUNT
332300 Creditors 0.00 0.50 EUR -0.50
339003 Sundry Creditors 0.00 4,423.39 EUR -4,423.39
339003 Sundry Creditors 4,241.50 0.00 GBP 3,587.50
340101 Ordinary Shares 0.00 1.41 GBP -1.00
395000 Revenue Reserves 0.00 196,850.46 EUR -196,850.46
395000 Revenue Reserves 0.00 967,816.68 GBP -797,995.90
I have managed to surpress the description when duplicated but would like to only display the appropriate net debit or credit amount just leaving the ccy and amount itemised, like this:
DESCRIPTION DEBIT CREDIT CCY CCY AMOUNT
332300 Creditors 0.50 EUR -0.50
339003 Sundry Creditors 181.89 EUR -4,423.39
GBP 3,587.50
340101 Ordinary Shares 1.41 GBP -1.00
395000 Revenue Reserves 1,164,667.14 EUR - 196,850.46
GBP -797,995.90
Any help solving or assisting with this problem would be very much appreciated.
Set up a tablix with a group based on the DESCRIPTION field (no header row for the group):
Since you're using SSRS 2008R2 the left most column will be created by default when you create the group, so nothing extra to do there.
We're displaying CCY and CCYAMOUNT for each row so they are just added as normal fields.
The expressions for DEBIT and CREDIT are a bit more complicated.
CREDIT:
=IIf(RowNumber("DESCRIPTION") = 1
and Sum(Fields!DEBIT.Value, "DESCRIPTION") - Sum(Fields!CREDIT.Value, "DESCRIPTION") > 0
, Sum(Fields!DEBIT.Value, "DESCRIPTION") - Sum(Fields!CREDIT.Value, "DESCRIPTION")
, Nothing)
And basically just switch the column logic for DEBIT:
=IIf(RowNumber("DESCRIPTION") = 1
and Sum(Fields!CREDIT.Value, "DESCRIPTION") - Sum(Fields!DEBIT.Value, "DESCRIPTION") > 0
, Sum(Fields!CREDIT.Value, "DESCRIPTION") - Sum(Fields!DEBIT.Value, "DESCRIPTION")
, Nothing)
End result looks like your requirements:
There are two things to note in the above expressions - we only display a result if it is the first row in the Group scope (RowNumber("DESCRIPTION") = 1) and also only if the particular column, i.e. DEBIT or CREDIT is greater than its counterpart in the same group scope.
You can try the following solution.
Add one derived column in the dataset query as shown below. I am assuming your dataset is sorted on account description if not you need to sort it.
SUM(-credit+debit) OVER(PARTITION BY AccountDescription) as Net
Use the below expression for Debit column in the SSRS tablix
=Iif(Previous(Fields!AccountDescription.Value)=Fields!AccountDescription.Value,"",Iif(Fields!Net.Value>0,Abs(Fields!Net.Value),""))
Use the below expression for Credit column in the SSRS tablix
=Iif(Previous(Fields!AccountDescription.Value)=Fields!AccountDescription.Value,"",Iif(Fields!Net.Value<0,Abs(Fields!Net.Value),""))

OpenCart - how to calculate VAT correctly?

Here's an example of the issue at hand:
A product costs 81.25 (that is price including 20% VAT, in other words, 81.25 is equal to 67.71+13.54). I've set the price calculations to the best of my belief (from Extensions->Order Totals) and within Shopping Cart, we'd see the following break-down:
A table of the product with thumbnail, description, quantity, price: and last cell says 81.25. Below that:
Sub-total: 67.71 (that is price without VAT)
+20% VAT: 13.54 (that is the VAT, achieved through Taxes)
-10% Discount: -8.13 (all products over $50 receive 10% discount, that is, 10% off of 81.25)
TOTAL: 73.12
Unfortunately, our customers are very sensitive to VAT, so the calculation should break-down to:
-10% discount: 8.12 (that is 10% off the full price of 81.25)
Sub-total: 60.94 (that would be the full price,81.25, minus the 10% discount or 8.12 first and excluding the 20% VAT second)
+20% VAT: 12.19
TOTAL: 73.12
As you can see, the Total is the same, but the VAT is different. I've tried a different Total Order from the OpenCart admin, but if I start with Discount, it does not have a previous value to discount in the first place and returns 0.
A possible theoretical solution in my mind, perhaps if Total could be duplicated somehow, renamed and inserted hidden in the very first position (so that it would equal 81.25 at the beginning, then discount etc.) that might work. I would really appreciate your ideas.
I see what's Your point. But If I buy something from Your store for the end-price of 73.125 (that is 81.25 - 10% discount) while there is 20% VAT, the price without VAT is 60.9375 and the VAT is indeed 12.1875. This is simply because the discount is applied to the sub-total price (end-price of the product including VAT) and not to the price without VAT.
If You would like to do it Your way, thus subtracting 10% discount from the netto price of the product (excluding VAT), You would have to apply 10% discount on the VAT as well:
price excl. VAT -10% discount: 81.25/1.2 * 0.9 = 67.708333 * 0.9 = 60.9375
VAT = 13.541666 (BUT SEE point 3)
VAT - 10% discount = 13.541666 * 0.9 = 12.1875
Your counting is incorrect simply because if we count Your results (that were very wrongly rounded), we would end up with the final price of 60.9375 (price excl. VAT - 10% discount) + 13.541666 (VAT w/o 10% discount) = 74.4791666 ~ 74.48
AND
74.48 !== 73.12
BUT counting the discounted price + discounted VAT
60.9375 + 12.1875 = 73.125 which is correct result!
I hope now is it clear that OC counts the result correctly while Your idea is wrong...
It should really go
Subtotal (67.71)
Discount (-6.77)
VAT (13.54)
Total (73.12)
The subtotal is actually the "total" you mentioned you wanted to have and rename. It's not possible to have the total show before the taxes etc are applied for the simple fact that the order total is calculated in order of those totals
Uncle Fester, I think you are right.
We have to have in mind two groups of buyers, retail and wholesale.
If you tell a guy in retail that he has 10% discount, it should mean that if the final price (w/ VAT) was, lets say USD 100, he would expect that the discounted price should be USD 90.
On the other hand, for a wholesale customer, it is a bit different. Wholesale customers are interested in prices w/o VAT only, so, a price for him is USD 90 + VAT and when you tell him he has 10% discount, he counts on USD 81 w/o VAT.
Back to the VAT calculation, lets look at the same price of a product in amount of USD 90 w/o VAT and USD 100 w/ VAT. Lets say we are giving 10% discount to both customer groups.
Retail customer is counting on a price of USD 90 (USD 75 + VAT), while
Wholesale customer counts on a price of USD 97.2 (USD 81 + VAT).
So, that is the initial approach to the issue.
Next, something that I do not understand is the following:
basically, any tax is to be paid in a certain percentage of the final sum, dont you agree?
So, if the final price of the product (w/o VAT) is USD 100, then the VAT is USD 20 (if rate is 20%), and final amount is USD 120. If however, one receives 10% discount, his bill is now USD 90 + VAT, or USD 99 is the final sum. In other words, VAT amount in Invoice should always be the exact percentage of the goods that is precised in the law.
If it stays written that it is USD 13,54 - it is NOT 20% of the net, but the gross amount of the goods. In this case, VAT grows to 22,2%!?! And all of you are saying that is how it should be???
I really do not understand this...
There are 2 ways of calculating VAT-
Method 1) Applying VAT on each item
Method 2) Applying VAT on the subtotal
For my country in Malaysia, the VAT has to be calculated using Method 1. Consequently, the accounting (XERO) and billing (TradeGecko) software I use also uses Method 1. Unfortunately, OpenCart uses Method 2, and I am still searching for the solution for OpenCart that allows me to use Method 1.
Method 1 and Method 2 will give you different VAT amount, simply because of where the rounding of the VAT amount is taking place. For Method 1, the rounding of VAT takes place at the item level, while in Method 2, the rounding takes place at the sub-total level.
If only OpenCart has a simple radio button that allows us to choose either one, that should please almost 99% of the users.