MS Access - Extract single value for calculation - sql

This an MS Access related question.
I get the Collateral divided 50 times because I have 50 rows in my ExchangeRates table... however the SELECT statement is supposed to only extract the value associated to CurrencyCode="EUR". How can I change the below statement to have the division being applied once only?
SELECT tbl_A.Security, tbl_A.Typ, Sum(([Collateral]/(SELECT tblExchangeRates.RateToUSD
FROM tblExchangeRates
WHERE (((tblExchangeRates.CurrencyCode)="EUR"))))) AS CollateralUSD
FROM tbl_A, tblExchangeRates
GROUP BY tbl_A.Security, tbl_A.Typ
HAVING (((tbl_A.Typ)="PR"));

It looks like this is what I was willing to get, just an Alias. SQL gurus, you are welcome to review.
SELECT tbl_A.Security, Sum(([Collateral]/[RateToUSD])) AS CollateralUSD
FROM tbl_A, (SELECT RateToUSD
FROM tblExchangeRates
WHERE CurrencyCode = 'EUR') AS MyAliasQ
GROUP BY tbl_A.Security
HAVING (((tbl_A.Typ)="PR"));

Related

SQL aggregate function

I want to run a query in which I have to count no of itemsModel in the database which is in MS Access 2007 and also to multiply that count with the rate with i have kept in the different table.
SELECT
AllocateAsset.Item, AllocateAsset.ItemModel,
COUNT(AllocateAsset.ItemModel) AS CountOfItem,
(COUNT(AllocateAsset.ItemModel) * rateList.Rate) AS Amount
I'm getting an error that:
it is not right expression ...
you need to add a group by statement:
group by AllocateAssettItem, AllocateAsset.ItemModel

Oracle Data Gaps

im looking for a query to fill this condition:
That currently gives us the number of BACs at the entity (which is something we need). The database assigns the BAC IDs consecutively within each accounting entity. So we need to add one more field to the query showing the current highest BAC ID at the entity. And once we have that, just filter the results down to anyplace the number of records doesn't equal the highest ID.
My current query:
select accounting_entity_id, count(bac_id)
from dc.pl_bac_information
group by accounting_entity_id
having count(bac_id) > 1;
Use analytic functions for this:
select bi.*
from (select bi.*, max(bac_id) over (partition by accounting_entity_id) as max_bac_id
from dc.pl_bac_information bi
) bi
where bac_id = max_bac_id;
This assumes you are using Oracle.
SELECT ACCOUNTING_ENTITY_ID
FROM DC.PL_BAC_INFORMATION
HAVING COUNT(BAC_ID) > 1 AND COUNT(BAC_ID) != MAX(BAC_ID)
GROUP BY ACCOUNTING_ENTITY_ID;

SQL postgres query

I have a query that I'm trying to perform below. However, being unable to test my query, I am unsure of my result.
The donors who have donated at least $10,000 since January 1, 2010 (donor ID, name, total
gifts).
SELECT donor_id,
donor_name,
SUM (amount) AS total_gifts_since_2010
FROM donor
JOIN gift
USING (donor_id)
JOIN gift_fund_allocation
USING (gift_id)
JOIN fund
USING (fund_id)
WHERE gift_date >= ‘01/01/2010'
GROUP BY (donor_id, donor_name)
HAVING total_gifts_since_2010 >= 10000;
Either one of two things will happen:
What I want it to do, which is only adds amounts after 2010.
Not what I want it to do, which is add all the amounts, but only
select/display the ones after 2010.
So my question would be something like this: Does the entire query happen instruction-by-instruction, or does the WHERE clause have an immediate effect on the query?
1) WHERE should precede GROUP BY (in the statement)
2) The sequence is following: JOIN - WHERE - GROUP BY - SELECT list

Query cannot be parse SELECT avg_galoon, avg_galoon

I dont know if its the right way to multiply that way, I want to multiply the avg_galoon that is sold weekly by the price of it which is 20
SELECT avg_galoon,
avg_galoon * 20 + NVL(total income , 0)
FROM customer;
total income isn't a column name from the customer table.
If you disagree and can see that column name (via methods mentioned in the comments already) then you will need to enclose that column name in the appropriate syntax for the database engine you are using i.e. [total income] for Microsofts SQL Server, `total income` for MySQL etc.
In future avoid the use of spaces in column names.

Calculate result from multi table in database

I have a question in my VB.NET POS Development and could't find a solution myself. I'm currently using VS2010 and MS Access as my database.
I have two database table as shown below:
SalesReceipt(#Receipt_ID, Sales_Date, Receipt_Number, Customer_ID, Sales_BDiscount, Sales_ADiscount, Sales_Payment)
Customer(#Customer_ID, Customer_Name, Customer_Contact, Customer_Debt)
NOTE : BDiscount = Before Discount / ADiscount = After Discount
In my system, one customer can have many SalesReceipt. Now my problem is how can I update the correct Customer_Debt for customer? My logic is update the respective customer's Customer_Debt by looping every row in SalesReceipt and calculate the debt by doing something like :
totalDebt = totalDebt + (Sales_Payment - Sales_ADiscount)
But I not sure how can I make sure it only loop and calculate the result for the selected customer only. How can I achieve this correctly in .NET?
If you want to calculate totalDebt per customer you can use query
SELECT Customer_ID, sum(Sales_Payment - Sales_ADiscount) as totalDebt FROM SalesReceipt
GROUP BY Customer_ID
Result contains totalDebts aggregated by Customer_ID and can be used to update Customer (all the looping and calculating is done by a database engine).
The query can also update be more complex to do even update for you.
Couldn't you just write a query in your Access db that performs your calculation (Sales_Payment - Sales_ADiscount) on your SalesReceipt table grouped by CustomerID?