Multiply rate & Revenue based on currency - sql

There are two tables one with Product-Name, revenue, Currency AND other with currency & rate
I need to convert the Revenue based on the rate:
Like: If Table1.Currecy = Table2.Currecy_code then revenue * rate else 'NA'
it is a long list of currencies.
could you please tell me which query will work to get this result.
FYI: I am using Athena database-dbeaver, not sure which sql that is.

You can use left join with case when expression
select Product_name,currency,
(case when rate is null then 'NA' else cast(revenue* rate as varchar2) end) revenue
from table1 left join table2 on Table1.Currecy = Table2.Currecy_code

Related

SQL Percentage Queries

I'm a beginner in SQL and my question is about calculating a percentage of the overall disclosed total from a table called merged. I want to calculate the number of 'SUPPORT' from committee_position (a column in the table merged)
How to calculate the percentage in that case.
I start with:
SELECT Sum (amount) *100
from merged
where merged.committee_position == 'SUPPORT';
Help me continue it, Thank you
If I followed you correctly, you can do conditional aggregation:
select
100.0 * sum(case when committee_position = 'SUPPORT' then amount else 0 end) / sum(amount)
from merged
This gives you the percentage of amount that have committee_position = 'SUPPORT' over the total amount in the table.
Here you go.
SELECT a.Support_Amount/b.Total_Amount*100
FROM (SELECT Sum (amount) as Support_Amount
from merged
where merged.committee_position = 'SUPPORT') as a
CROSS JOIN
(SELECT Sum (amount) as Total_Amount
from merged) as b

SQL server Currency Code if Invoice has one more than one currency

Below is my requirement
Table1 - QuotationMaster - QuoteID,CustomerID,Date,InvoiceNo
Table2 - QuoteItems - QuoteID,ItemID,CurrencyID,Amount
Table3 - CurrencyMaster - CurrencyID,CurrencyCode
Example:
If I search for the invoice statement for a particular date, final result must be
Date, CustomerName, CurrencyCode, Amount
For a particular invoice if it has 2 items which has same CurrencyID
then result should be displayed as follow
Item1 - Amount (2.00)
Item2 - Amount (3.00)
Date InvoiceNo CustomerName CurrencyCode Amount
June INV123 TESTING INR 5.00
For a particular invoice if it has 2 items which has different CurrencyID's
then result should be displayed as follow
Date InvoiceNo CustomerName CurrencyCode Amount
June INV123 TESTING 0 0.00
To make it simple if a invoice has two items with different currencies then the currency code and Amount must be 0.
I'm not able to get a clear picture how to solve this as I end up with groupby currencyid and get the same result printed twice
Date InvoiceNo CustomerName CurrencyCode Amount
June INV123 TESTING INR 2.00
June INV123 TESTING GBP 3.00
Can you please help me with the approach.
When I understand this right, you basically want to group the items by quoteid and filter for those where the maximum currencyid is no equal to the minimum currencyid in the HAVING clause. (If all currency IDs are equal the minimum is equal to the maximum.)
Then left join this to your quotes table and left join the currency table to get the code.
Check in a case if the currency ID IS NULL. If it is, the items of the quote had different currency ids (or all were NULL which means they were different, by the tri state logic).
As you didn't describe you customer table the customer name isn't selected. You'll have to add that yourself.
SELECT qm1.date,
qm1.invoiceno,
CASE
WHEN qi2.quoteid IS NULL
THEN cm1.currencycode
ELSE
'0'
END currencycode,
CASE
WHEN qi2.quoteid IS NULL
THEN qi2.amount
ELSE
0
END amount
FROM quotationmaster qm1
LEFT JOIN (SELECT qi1.quoteid,
sum(qi1.amount) amount,
max(qi1.currencyid) currencyid
FROM quoteitems qi1
GROUP BY qi1.quoteid
HAVING max(qi1.currencyid) = min(qi1.currencyid)) qi2
ON qi2.quoteid = qm1.quoteid
LEFT JOIN currencymaster cm1
ON cm1.currencyid = cm1.qurrencyid;
You can try this:
SELECT sub.QuoteID,
sub.CustomerID,
sub.Date,
sub.InvoiceNo,
sub.ItemID,
IIF(sub.ItemCount = sub.CurrencyCount, sub.CurrencyID, 0) AS CurrencyID
sub.Amount,
FROM (
SELECT qm.QuoteID,
qm.CustomerID,
qm.Date,
qm.InvoiceNo,
qi.ItemID,
qi.CurrencyID,
qi.Amount,
COUNT(*) OVER (PARTITION BY qi.QuoteID) AS ItemCount,
COUNT(*) OVER (PARTITION BY qi.QuoteID, qi.CurrencyID) AS CurrencyCount
FROM QuotationMaster AS qm
INNER JOIN QuoteItems AS qi
ON qi.QuoteID = qm.QuoteID
) AS sub
-- You can use it also for a join (a 'n/a'-Currency in CurrencyMaster with ID=0 is required)
-- INNER JOIN CurrencyMaster AS cm
-- ON cm.CurrencyID = IIF(sub.ItemCount = sub.CurrencyCount, sub.CurrencyID, 0)
-- OR Use LEFT OUTER JOIN if you dont have a Fallback-Value in CurrencyMaster.

Combining Count and MIN functions

I have a part of my query as:
SUM(POReceiptQuantity) as Receieved,
MIN(ItemLocalStandardCost) as Low,
MAX(ItemLocalStandardCost) as High,
Received returns the total number of Items we sold this year. The LOW is the lowest price we paid, and High is the highest price we paid.
I'm trying to incorporate a new column showing how many if the item we sold at the Low price. I tried to use Count along with Min function but it returns a "cannot perform an aggregate function on an expression containing an aggregate or a subquery"
Does anyone have any ideas how i could go about this.
Thank you
You need create a subquery with your current GROUP BY query and join with your Original Table. Then you can use a conditional COUNT
SELECT T2.Received,
T2.Low,
COUNT( CASE WHEN T1.ItemLocalStandardCost = T2.Low THEN 1 END) as Total_Low,
T2.High,
COUNT( CASE WHEN T1.ItemLocalStandardCost = T2.High THEN 1 END) as Total_High
FROM YourTable T1
CROSS JOIN ( SELECT SUM(Y.POReceiptQuantity) as Receieved,
MIN(Y.ItemLocalStandardCost) as Low,
MAX(Y.ItemLocalStandardCost) as High
FROM YourTable Y
GROUP BY .... ) as T2

Trying to combine two different attributes into one column

I am trying to write a query that shows 3 different columns. The activity class name, total revenue, and number of customers. My code below shows those columns (along with 2 different prices, student and regular). What I am trying to do is write a query that calculates revenue based on what the customer type is * the appropriate customer type (student or regular). I can't seem to be write the query to distinguish the 2 different customer types and the appropriate price into a 'revenue' column. Any help would be much appreciated!
SELECT ACTIVITY_NAME AS CLASS,
STUDENT_PRICE,
REGULAR_PRICE,
Count(CUSTOMER.CUSTOMER_TYPE) AS NUM_CUST,
Count(CUSTOMER.CUSTOMER_TYPE) * ACTIVITY.STUDENT_PRICE AS REVENUE
FROM ACTIVITY
JOIN ACTIVITY_BOOKING
ON ACTIVITY.ID = ACTIVITY_BOOKING.ACTIVITY_ID
JOIN CUSTOMER
ON ACTIVITY_BOOKING.CUSTOMER_ID = CUSTOMER.ID
GROUP BY ACTIVITY_NAME,
STUDENT_PRICE,
REGULAR_PRICE
ORDER BY ACTIVITY.ACTIVITY_NAME
I think you need SUM not COUNT and you can sum conditional value as:
SELECT ACTIVITY_NAME AS CLASS,
STUDENT_PRICE,
REGULAR_PRICE,
Count(CUSTOMER.CUSTOMER_TYPE) AS NUM_CUST,
Sum (case when CUSTOMER.CUSTOMER_TYPE = 'Student' then STUDENT_PRICE when CUSTOMER.CUSTOMER_TYPE = 'Regular' then REGULAR_PRICE else 0 end) AS REVENUE
FROM ACTIVITY
JOIN ACTIVITY_BOOKING
ON ACTIVITY.ID = ACTIVITY_BOOKING.ACTIVITY_ID
JOIN CUSTOMER
ON ACTIVITY_BOOKING.CUSTOMER_ID = CUSTOMER.ID
GROUP BY ACTIVITY_NAME,
STUDENT_PRICE,
REGULAR_PRICE
ORDER BY ACTIVITY.ACTIVITY_NAME

sql select sum off of two where clauses

SELECT Projects.Projectid, Projects.ProjectNumber, Projects.ProjectName,
Projects.ProjectBudgetedIS, Projects.ProjectSpentIS,
Projects.ProjectBudgetedBusiness, Projects.PorjectSpentBusiness, Project.Status,
ProjectStatus.Status AS Expr1
FROM Projects
INNER JOIN ProjectStatus ON Projects.Status = ProjectStatus.StatusID
WHERE Projects.Status = #Status
So what I want to do is take the sum of a table called invoices which has a field called ISorBusiness and a field called totalspent and store that data into the projects tabel in the appropriate field. So that when I get an invoice that is charged to the IS it takes that amount and rolls it into the Projects.ProjectSpentIS and if I get a invoice that is to the business it rolls it into the Projects.ProjectBudgetedBusiness.
I know this should be easy and sorry for the noobish question. Thanks in advance!
I would do something like:
SELECT SUM(CASE WHEN (IsOrBusiness = 'IS') THEN totalSpent ELSE 0 END) AS IsSpent,
SUM(CASE WHEN (IsOrBusiness = 'Business') THEN totalSpent ELSE 0 END) AS BusinessSpent
FROM Invoices
Obviously the usage depends on whether you are trying to write an insert query or select this data as part of the select query you have posted.