SQL "IF" expression coding - sql

Im using a program that uses SQL in the background and I have run into a snag and I just cant figure it out.
It has asked for a SQL Expression to calculate cost mark up in a data field. I sort of get how it works because Ive used Excel and have done some complex calculations but SQL is different and I am having a hard time grasping it. Anyway ...
I need to have the expression code say:
If column A is between $0-$9.99 add 70%
If column A is between $10-$14.99 add 30%
If column A is between $15-$24.99 add 21%
If column A is between $25-$89.99 add 14.9%
If column A is between $90-$244.99 add 10%
If column A is between $245-$499.99 add 9%
If column A is between $500-1999.99 add 6.9%
If column A is $3000 and up add 7.5 %
It should be all one string / expression
Also it should round to the 2nd decimal place.
If I am missing something or using the wrong wording forgive me -SQL newbie here
Please help me!!
Thank you

Use a case:
select cast(
case
when columnA between 0 and 9.99 then columnA * 1.7
when columnA between 10 and 14.89 then columnA * 1.3
-- etc for other value ranges
else columnA
end as decimal(10, 2)) as columnA_plus_fee

Related

How to query column with letters on SQL?

I'm new to this.
I have a column: (chocolate_weight) On the table : (Chocolate) which has g at the end of every number, so 30x , 2x5g,10g etc.
I want to remove the letter at the end and then query it to show any that weigh greater than 35.
So far I have done
Select *
From Chocolate
Where chocolate_weight IN
(SELECT
REPLACE(chocolote_weight,'x','') From Chocolate) > 35
It is coming back with 0 , even though there are many that weigh more than 35.
Any help is appreciated
Thanks
If 'g' is always the suffix then your current query is along the right lines, but you don't need the IN you can do the replace in the where clause:
SELECT *
FROM Chocolate
WHERE CAST(REPLACE(chocolate_weight,'g','') AS DECIMAL(10, 2)) > 35;
N.B. This works in both the tagged DBMS SQL-Server and MySQL
This will fail (although only silently in MySQL) if you have anything that contains units other than grams though, so what I would strongly suggest is that you fix your design if it is not too late, store the weight as an numeric type and lose the 'g' completely if you only ever store in grams. If you use multiple different units then you may wish to standardise this so all are as grams, or alternatively store the two things in separate columns, one as a decimal/int for the numeric value and a separate column for the weight, e.g.
Weight
Unit
10
g
150
g
1000
lb
The issue you will have here though is that you will have start doing conversions in your queries to ensure you get all results. It is easier to do the conversion once when the data is saved and use a standard measure for all records.

Redshift SQL WHERE statement doesn't seem to filter correctly?

So I have a pretty simple query something like:
Select * from TableX where column1 = '2020-24'
However, in the results we get column 1 but it is not restricted to 2020-24 (week 24 in 2020), I see 2020-20 and 2020-05 etc.
I assume it's something to do with text/number formatting or something. I am using Redshift SQL, how can I fix this so it only shows rows where column 1 is '2020-24'? I tried use the WHERE with 2020-24 and with '2020-24' but both give the same result. I have heard I may need to cast with :: notation but not sure how..

Converting pounds to kilos in SQL

I am trying to pull data from a table with the filter weight < 25 kgs , but my table has weight in pounds, I tried using below sql can some one please tell me is this the right way to do it or is there any other way .
select * from dbo.abc
where (round((WEIGHT * 0.453592 ),0) < 25)
Your solution would work, but it's not sargaeble. A better solution would be to convert your 25kgs to lbs. That way, if you have an index on your WEIGHT column, the query analyzer could make use of it.
One additional note: Why round to 0 decimal places? You'll lose accuracy that way. Unless you have some requirement to do so, I'd drop the rounding. It's unnecessary overhead.
As other people mentioned, you don't want to convert weight as it will cause SQL Server not to use your index. So try this instead:
SELECT *
FROM dbo.acb
WHERE WEIGHT < ROUND(25/.453592,4)

Apply a single case statement to all columns in sql

I need to get the sum of each column of my table. So i used select sum(col1),col2 etc.
If the sum is null, i need to get 0, else the value of the sum. So I used "select case when sum(col1) is null then 0 else sum(col1) end as sum_col1".
I have around 40 such columns in my table. Do i need to write " case when sum(col n) then..." 40 times in my query?
Im working on oracle 9 g.
Thanks
I have around 40 such columns in my table. Do i need to write " case
when sum(col n) then..." 40 times in my query?
Short answer: Yes.
Longer answer: You might be able to use some kind of dynamic SQL to generate the statement automatically from the column metadata. But it might not be worth the trouble, as you can often just as easily copy-paste the statement in your query editor. All things considered, having a table with 40 columns that you need to sum, indicates a bad data model design. When working with a badly designed data model, you pay the price at query time...

SQL - View column that calculates the percentage from other columns

I have a query from Access where I caluclated the percentage score of three seperate numbers Ex:
AFPercentageMajor: [AFNumberOfMajors]/([AFTotalMajor]-[AFMajorNA])
which could have values of 20/(23-2) = 95%
I have imported this table into my SQL database and tried to write a expression in the view (changed the names of the columns a bit)
AF_Major / (AF_Major_Totals - AF_Major_NA)
I tried adding *100 to the end of the statement but it only works if the calculation is at 100%. If it is anything less than that it puts it as a 0.
I have a feeling it just doesn't like the combincation of the three seperate column names. But like I said I'm still learning so I could be going at this completely wrong!
SQL Server does integer division. You need to change one of the values to a floating point representation. The following will work:
cast([AFNumberOfMajors] as float)/([AFTotalMajor]-[AFMajorNA])
You can multiply this by 100 to get the percentage value.