Multiply 2 values from 2 different tables - sql

I'm trying to multiply value X by value Y with SQL. Value X is located in table A and B is located in table B. I couldn't find the answer for this.
Table Transactions
ID Transaction_ID Total_Amount
1 001 1200
2 002 1500
3 003 1600
Table Rates
ID Currency_Name Exchange_Rate
1 AUD 1.5
2 SEK 2.0
3 PLN 3.0
The question I'm trying to answer is:
What is the Total_Amount for transaction 001 in SEK (Swedish Crown). So I need to multiply 1200 * 2.0 and display the result.

Edited based on added info
SELECT Total_Amount * Exchange_Rate AS Value
FROM Transactions, Rates
WHERE Rates.Currency_Name = 'Sek' and Transaction_id = 001

To answer your question:
What is the Total_Amount for
transaction 001 in SEK (Swedish
Crown). So I need to multiply 1200 *
2.0 and display the result.
Use:
SELECT ID, Transaction_ID, Total_Amount, Total_Amount*(SELECT Exchange_Rate from Rates where Currency_Name='SEK')
FROM TRANSACTIONS
WHERE TRANSACTION_ID='001'

This should work, depending on your table structure :
select table1.x * table2.y
from table1, table2;
But I actually doubt that this is really what you want to do, if you provide more information, we could give you a much better answer. Please provide table structure and you're real goal !

how about this:
select a.x*b.y from tableA a, tableB b

Related

Access 2019 Query through 2 tables

Table A
ID Name Pay Colleage
1 aaa 15,000 1
2 bbb 25,000 2
Table B : Deduction Money
ID Min Max Colleage1 Colleage2 Colleage3 Colleage4
1 10,001 20,000 1,000 2,000 3,000 4,000
2 20,001 30,000 2,000 3,000 4,000 5,000
I have to take each employee's deduction money in Table B.
Given Table B says, aaa's deduction money will be 1,000.
Because its range of pay is between 10,001~20,000
and it has 1 colleage.
So if i follow it on Table B. I can find the amount is 1,000.
As same way, bbb will be 3,000.
But I can't get the proper value.
I want query syntax to use for access 2019.
So help me pz.
You can join the tables and use CHOOSE() to select the correct deduction:
SELECT a.*, CHOOSE(a.Colleage, b.Colleage1, b.Colleage2, b.Colleage3, b.Colleage4) AS Deduction
FROM TableA AS a INNER JOIN TableB AS b
ON a.Pay >= b.Min AND a.Pay <= b.Max

SQL update statement to sum column in one table, then add the total to a different column/table

Evening all, hoping for some pointers with an SQL Server query if possible.
I have two tables in a database, example as follows:
PostedTran
PostedTranID AccountID PeriodID Value TransactionDate
1 100 120 100 2019-01-01
2 100 120 200 2020-01-01
3 100 130 300 2021-01-01
4 101 120 400 2020-01-01
5 101 130 500 2021-01-01
PeriodValue
PeriodValueID AccountID PeriodID ActualValue
10 100 120 500
11 101 120 600
I have a mismatch in the two tables, and I'm failing miserably in my attempts. From the PostedTran table, I'm trying to select all transaction lines dated before 2021-01-01, then sum the Value for each AccountID from the results. I then need to add that value to the existing ActualValue in the PeriodValue table.
So, in the above example, the ActualValue on PeriodValueID 10 will update to 800, and 11 to 1000. The PeriodID in this example is constant and will always be 120.
Thanks in advance for any help.
Since RDMS not mentioned, pseudo-sql looks like:
with DataSum as
(
select AccountID, PeriodID, sum(Value) as TotalValue
from PostedTran
where TransactionDate<'1/1/2021'
group by AccountID, PeriodID
)
update PeriodValue set ActualValue = ActualValue + ds.TotalVaue
from PeriodValue pv inner join DataSum ds
on pv.accountid=ds.accountid and pv.periodid=ds.periodid
The following should do what you ask. I haven't included PeriodId in the correlation as you did not specify it in your description, however you can just include it if it's required.
update pv set pv.ActualValue=pv.ActualValue + t.Value
from PeriodValue pv
cross apply (
select Sum(value) value
from PostedTran pt
where pt.AccountId=pv.AccountId and pt.TransactionDate <'20210101'
)t

sql Query to find different record in a column

I have table with 5 OR 6 COLUMNS and I need to use the below 2 columns to get the result
col1 col2
Acc1 USD
ACC1 GBP
ACC1 EUR
ACC2 USD
Result:
I need to find out if a acc has more than 2 currency, but the base currency is USD. I need to find out those records which has USD plus other currency if I have only USD accounts then it should not come in my result.
With the information provided this could be an answer:
SELECT col1
FROM tab
GROUP BY col1
HAVING count(*) > 1
Not the best solution but should do the job.
with cte as
(
SELECT t1.[col1],t1.[col2],(select count(t2.col1) from accounts t2 where t2.col1=t1.col1) as AllCurrency
from accounts t1
)
SELECT distinct cte.col1 from cte where cte.AllCurrency>1

select rows with non distinct values in column 1 scoped to column 2

So i have a people table and bank_accounts table:
People
id | name
1 John
2 Mark
3 Mary
BankAccount
id | person_id | currency
1 1 'USD'
2 1 'EUR'
3 2 'USD'
4 2 'USD'
5 3 'EUR'
I want to get back all accounts with their owners if that owner has only accounts with max one kind of currency. I don't want to get back any account which is owned by user which has another account in another currency. erm :P
so the table i want to get back looks like that:
account_id | person_id | currency
3 2(Mark) 'USD'
4 2(Mark) 'USD'
5 3(Mary) 'EUR'
Hope it's understandable. This is simplified example of course. I will use this on a lot bigger tables with a lot of data. So some efficiency would be also good.
Thanks a lot for your time and help!
select *
from bankAccount
where person_id in
(
select person_id
from bankAccount
group by person_id
having count(distinct currency) = 1
)

oracle sql query to get data from two tables of similar type

I have two tables ACTUAL AND ESTIMATE having unique column(sal_id, gal_id, amount, tax).
In ACTUAL table I have
actual_id, sal_id, gal_id, process_flag, amount, tax
1 111 222 N 100 1
2 110 223 N 200 2
In ESTIMATE table I have
estimate_id, sal_id, gal_id, process_flag, amount, tax
3 111 222 N 50 1
4 123 250 N 150 2
5 212 312 Y 10 1
Now I want a final table, which should have record from ACTUAL table and if no record exist for sal_id+gal_id mapping in ACTUAL but exist in ESTIMATE, then populate estimate record (along with addition of amount and tax).
In FINAL table
id sal_id, gal_id, actual_id, estimate_id, total
1 111 222 1 null 101 (since record exist in actual table for 111 222)
2 110 223 2 null 202 (since record exist in actual table for 110 223)
3 123 250 null 4 51 (since record not exist in actual table but estimate exist for 123 250)
(for 212 312 combination in estimate, since record already processed, no need to process again).
I am using Oracle 11g. Please help me on writing a logic in a single sql query?
Thanks.
There are several ways to write this query. One way is to use join and coalesce:
select coalesce(a.sal_id, e.sal_id) as sal_id,
coalesce(a.gal_id, e.gal_id) as gal_id,
coalesce(a.actual_value, e.estimate_value) as actual_value
from actual a full outer join
estimate e
on a.sal_id = e.sal_id and
a.gal_id = e.gal_id
This assumes that sal_id/gal_id provides a unique match between the tables.
Since you are using Oracle, here is perhaps a clearer way of doing it:
select sal_id, gal_id, actual_value
from (select *,
max(isactual) over (partition by sal_id, gal_id) as hasactual
from ((select 1 as isactual, *
from actual
) union all
(select 0 as isactual, *
from estimate
)
) t
) t
where isactual = 1 or hasactual = 0
This query uses a window function to determine whether there is an actual record with the matching sal_id/gal_id. The logic is to take all actuals and then all records that have no match in the actuals.