Query cannot be parse SELECT avg_galoon, avg_galoon - sql

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.

Related

How to fix this column doesn't exist error in SQL?

In the sales table, three columns are btl_price, bottle_qty, and total. The total for a transaction should be the product of btl_price and bottle_qty. How many transactions have a value of total that is not equal to btl_price times bottle_qty?
Here is the table:
Here are my codes:
sql = """
Select (btl_price*bottle_qty) As total_sale, CAST(total AS money)
From sales
Where total != total_sale
"""
It keeps telling me "column "total_sale" does not exist".
Please help me to identify my mistakes.
PS: I code this in Jupyter Notebook. This is a practice of mine not in any DBMS.
You cannot use columns computed in the SELECT clause in the WHERE clause (in SQL, the matter is evaluated before the former).
Also, you need proper type casting to compare money and numbers.
Finally, you need to turn on aggregation to compute the number of sales that satisfy the condition.
Assuming that you are using Postgres, that would be:
select count(*)
from sales
where total::numeric <> btl_price::numeric * btl_quantity
Try this:
SELECT *
FROM sales
WHERE total !=(btl_price * bottle_qty)
Good luck

How to separate target numbers from text using sql

EX/
CLIENTS INCOME DETAILS ARE AS FOLLOWS,
- NET INCOME FROM WOOD SUPPLYING BUSINESS- RS 60,000/-
- INCOME FROM HIRING THE VEHICLES- RS 20,000/-
- TOTAL INCOME- RS 80,000/-
I only need to separate Total income only. final output should be 80000
how can i get this number using sql in sql server 2016?
Please try this.
SELECT CustomerName, (NetIncome + VehicleIncome) AS 'TotalIncome'
FROM Client_Income
GROUP BY CustomerName
ORDER BY CustomerName
Assuming that you have a table named ClientIncome with three rows named Customer, NetIncome, and VehicleIncome, the following query should do what you want:
SELECT Customer, NetIncome + VehicleIncome
FROM ClientIncome
It'll return a table with two rows, the first being the Customer and the second being the sum of the two incomes.
EDIT: Breaking number out of string
If you're using SQL Server 2016, and the column with the information string is called 'info', you can try this (I don't have access to a sql server so I can't test it myself):
SELECT Cast( Substring(info,
Charindex('RS', info, 3) + 3
Len(info) - Charindex('RS', info, 3) - 2)
As Int )
FROM ClientInfo
DISCLAIMER
Forgive any weirdness or wrong information as I did all of the research for this from my phone, so it may not be as thorough as I would like it to be. Take this answer with a pinch of salt.
SELECT REPLACE(REPLACE('80,000/-','/-',''),',','')

MS Access - Extract single value for calculation

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"));

SQL: Calculate Percentage in new column using another column

I found it hard to describe what I wanted to do in the title, but I will be more specific here.
I have a reasonably long query:
SELECT
/*Amount earned with validation to remove outlying figures*/
Case When SUM(t2.[ActualSalesValue])>=0.01 OR SUM(t2.[ActualSalesValue])<0 Then SUM(t2.[ActualSalesValue]) ELSE 0 END AS 'Amount',
/*Profit earned (is already calculated then input into db, this just pulls that figure*/
SUM(t2.[Profit]) AS 'Profit',
/*Product Type - pulls the product type so that we can sort by product*/
t1.[ucIIProductType] AS 'Product Type',
/*Profit Percentage - This is to calculate the percentage of profit based on the sales price which uses 2 different columns - Case ensures that there are no wild values appearing in the reports as previously experienced*/
Case When SUM(t2.[ActualSalesValue])>=0.01 OR SUM(t2.[ActualSalesValue])<0 THEN (SUM(t2.[Profit])/SUM(t2.[ActualSalesValue])) ELSE 0 END AS 'Profit Percentage',
/*Percentage of Turnover*/
*SUM(t2.[ActualSalesValue])/(Select SUM(t2.[ActualSalesValue]) OVER() FROM [_bvSTTransactionsFull]) AS 'PoT'
/*The join is connect the product type with the profit and the amount*/
FROM [dbo].[StkItem] AS t1
INNER JOIN [dbo].[_bvSTTransactionsFull] AS t2
/*There attirbutes are the links between the tables*/
ON t1.[StockLink]=t2.[AccountLink]
WHERE t2.[TxDate] BETWEEN '1/Aug/2014' AND '31/Aug/2014' AND ISNUMERIC(t2.[Account]) = 1
Group By t1.[ucIIProductType]
The 'Percentage of Turnover' part I am having trouble with - I am trying to calculate the percentage of the Amount based on the total amount - using the same column. So eg: I want to take the Amount value in row 1, then divide it by the total amount of the entire column and then have that value listed in a new column. But I keep getting errors or I Keep getting 1 (because it wants to divide the value by the same value. CAN anyone please advise me on proper syntax for solving this:
/*Percentage of Turnover*/
*SUM(t2.[ActualSalesValue])/(Select SUM(t2.[ActualSalesValue]) OVER() FROM [_bvSTTransactionsFull]) AS 'PoT'
I think you want one of the following:
SUM(t2.[ActualSalesValue])/(Select SUM(t.[ActualSalesValue]) FROM [_bvSTTransactionsFull] t) AS PoT
or:
SUM(t2.[ActualSalesValue])/(SUM(SUM(t2.[ActualSalesValue])) OVER() ) AS PoT
Note: you should use single quotes only for string and date constants, not for column and table names. If you need to escape names, use square braces.

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?