Sum of two columns as criteria - sql

I am trying to match product entries by their surface.
My thought was that the below query should be valid.
But it doesn't work, I am receiving:
Unknown column 'surface' in 'where clause'
SELECT SUM(width*height) AS surface FROM products WHERE surface>50

The function sum is not for this use case. With sum you get the sum of all rows. What you are looking for is.
SELECT (width*height) AS surface FROM products WHERE surface>50

This works:
SELECT * FROM products WHERE (width*height)>50

Related

Select query which sums two columns and want to use the result as a criteria for the select [duplicate]

This question already has an answer here:
How to use Alias in Where clause? [duplicate]
(1 answer)
Closed 2 years ago.
I'm using Oracle SQL Developer.
I want to multiply the values of two columns, price and quantity, and then use the sum as a criteria. The user needs to be able to determine what the minimum value of the sum field is, and the query should return all results where the sum of those two columns is higher than their specified minimum.
My query right now looks like this:
SELECT PROD_PRICE, PROD_QUANTITY, PROD_PRICE * PROD_QUANTITY AS "PROD_AMOUNT"
FROM PRODUCT
WHERE PROD_AMOUNT >= &PROD_AMOUNT
The Error message I get is that "PROD_AMOUNT" is an invalid identifier.
Thank you so much!
You can't use alias of computed columns in where clause you need to repeat the expression in order to perform filter using computed value
SELECT PROD_PRICE,
PROD_QUANTITY,
PROD_PRICE * PROD_QUANTITY AS "PROD_AMOUNT"
FROM PRODUCT
WHERE (PROD_PRICE * PROD_QUANTITY)>= &PROD_AMOUNT
any sql ex. oracle, MySQL , SQLite, SQLServer ... etc never use an alias in filer conditions (where or having)
SELECT PROD_PRICE,
PROD_QUANTITY,
PROD_PRICE * PROD_QUANTITY AS "PROD_AMOUNT"
FROM PRODUCT
WHERE (PROD_PRICE * PROD_QUANTITY)>= &PROD_AMOUNT

How to exclude items from search using NOT LIKE and % in Presto SQL?

I have the following query that returned an error: mismatched input 'LIKE'. Expecting: expression
select * from sales
where item_name NOT LIKE '%Tortoise%'
The image below illustrates what I want to get from my source data. I want records that do not contain the keyword "Tortoise".
Appreciate any advice on how to correct this syntax, thank you.
This may get you the expected result.
select *
from sales
where item_name NOT IN (
select *
from sales
where item_name LIKE '%Tortoise%'
)

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 calculate prevalence using sql code

I am trying to calculate prevalence in sql.
kind of stuck in writing the code.
I want to make automative code.
I have check that I have 1453477 of sample size and number of people who has disease is 851451 using count.
The formula of calculating prevalence is no.of person who has disease/no.sample size.
select (COUNT(condition_id)/COUNT(person_id)) as prevalence
from disease
where condition_id=12345;
when I run above code, I get 1 as a output where I am suppose to get 0.5858.
Can some one please help me out?
Thanks!
In your current query you count the number of rows in the disease table, once using the column condition_id, once using the column person_id. But the number of rows is the same - this is why you get 1 as a result.
I think you need to find the number of different values for these columns. This can be done using count distinct:
select (COUNT(DISTINCT condition_id)/COUNT(DISTINCT person_id)) as prevalence
from disease
where condition_id=12345;
You can cast by
count(...)/count(...)::numeric(6,4) or
count(...)/count(...)::decimal
as two options.
Important point is apply cast to denominator or numerator part(in this case denominator), Do not apply to division as
(count(...)/count(...))::numeric(6,4) which again results an integer.
I am pretty sure that the logic that you want is something like this:
select avg( (condition_id = 12345)::int )
from disease;
Your version doesn't have the sample size, because you are filtering out people without the condition.
If you have duplicate people in the data, then this is a little more complicated. One method is:
select (count(distinct person_id) filter (where condition_id = 12345)::numeric /
count(distinct person_id
)
from disease;

Add a Percent of Total Column in Access Query

I have the following query:
SELECT [3-2017].[Dealer#], Sum([3-2017].Balance) AS SumOfBalance
FROM [3-2017]
GROUP BY [3-2017].[Dealer#];
I want to add a column that returns the percent of grand total for each dealer#. I think I need another single-row query with the totals and join or union somehow, but I'm completely lost.
I've looked through a few different posts and this is the top result, but it doesn't solve my problem, unfortunately.
Really best and easiest done in a report. However, try:
SELECT [3-2017].[Dealer#], Sum([3-2017].Balance) AS SumDealerBal,
(SELECT Sum(Balance) FROM [3-2017]) AS SumBalance, SumDealerBal/SumBalance AS DealerPct
FROM [3-2017]
GROUP BY [3-2017].[Dealer#];
Advise no spaces or special character/punctuation (underscore is only exception) in any names. Better would be DealerNum or Dealer_Num.