Price Calculating formula in SQL - sql

I have been using the following formula in excel but now I a trying with sql:
the formula:
Price field=(ROUND(Cost field/Constant number,1))-0.01
If the cost is 1.43 the price outcome would be according to this formula would be 2.59.
How can I write this formula is sql?

You can use a simple select and round
assuming your column are name Cost field and Constant number
select ROUND(`Cost field`/`Constant number`,2)-0.01 as price
from my_table
or using as Constant number = 30
select ROUND(`Cost field`/30,2)-0.01 as price
from my_table
and as suggestd by Philipp be sure for the proper rounding take a look at https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round

Related

Value of a table column is not what it seems

I am trying to find all records in a table where the quantity is less than 1.
For this i am using a simple query:
SELECT * FROM Table WHERE Quantity < 1
However, i am getting results where the Quantity is actual 1.
The type of this column is FLOAT for some odd reason (Dont ask why, i haven't created it)
How do i know what the exact value is of this column. I tried:
SELECT
CEILING(quantity) as [Ceiling quantity],
FLOOR(quantity) as [Floor quantity],
CAST(ROUND(Quantity, 2) AS INT) as [Rounded quantity],
Quantity AS [Actual quantity]
FROM Table
But the result is:
Ceiling quantity Floor quantity Rounded quantity Actual quantity
---------------------- ---------------------- ---------------- ----------------------
1 0 1 1
I know that technically the quantity is lower than one. I am trying to find a decent way of obtaining the actual value this column holds.
Thanks to #JeroenMostert for providing the correct answer:
SELECT
FORMAT(quantity, 'G17') as [Format quantity]
FROM Table
results in:
Format quantity
-------------------
0.99999999999999989
The CONVERT method has a style where every distinct float value is guaranteed to convert to a distinct character string. This is style number 3, and it is available in SQL Server 2016 and later. You can read more about the CONVERT method and the different styles here:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
You can try it out like this:
SELECT Convert (VARCHAR(99), Quantity, 3) FROM Table

how to convert date from rows to columns as week numbers and get the price from the highest week number

Problem: I am trying to convert date from rows to columns as week numbers and get the price from the highest week number and call it givenPrice.
expected:
See below. In your case, shopName is the row field, date is your pivot field, and price is your crosstab field. Since it appears that you are not doing anything to the crosstab values, we can just use a Max() function as a dummy because we don't need to ensure that the values in the pivot field are unique. So, I came up with this:
TRANSFORM Max(price)
SELECT shopName
FROM YourTable
GROUP BY shopName
PIVOT date;
NOTE: This query is Access SQL.
https://i.stack.imgur.com/492Lb.png

How to calculate the percentage using date fields?

I want to calculate the percentages using date fields in my SSRS report.
I have two date fields :
eg I have 3 columns in my matrix
monthly target,
monthly completed and
percentage
Due date field count 10
completed date count 5
percentage 50%
for example
=Sum(Fields!Completeddate.Value)/Sum(Fields!duedate.Value)*100
however, this will not work for date fields.
Can anyone help?
Thanks in advance
The most straight-forward way to do this would be to add this field to your stored procedure (since it represents a column in your matrix) instead of trying to calculate it in SSRS.
Your SQL statement may look like this:
SELECT #CompletedDate, #DueDate, CAST(DateDiff(day, GetDate(), #CompletedDate) AS FLOAT)/CAST(DateDiff(day, GetDate(), #DueDate) AS FLOAT) * 100 AS INT) AS [Percentage]
The exact implementation will depend on how exactly you're using this value, but the point is that you can use the DateDiff() function to determine how many days apart two dates are, and with that information, you can find a percent difference. Once you've calculated this, you can assign it to a matrix column like you would any other value.

Calculating a percentage of two "Counts" in SQL in Microsoft Access 2010

I have a Microsoft Access 2010 database of thyroid surgeries. I have a query that counts the number of surgeries in the entire database. I have a second query that counts the number of surgeries performed between certain dates. I created a new query using SQL to calculate the percentage of surgeries in that date range (a percentage of the total surgery number). Here is the code:
SELECT
((select count(ThyroidSurgeryType)
from [Thyroid Surgeries]
HAVING ([Thyroid Surgeries].SurgeryDate) Between #1/1/2011# And #12/31/2012#)/(select count(ThyroidSurgeryType)
from [Thyroid Surgeries])) AS Percentage
FROM [Thyroid Surgeries];
I get .33 (I then set the row format to percentage to get 33%), but I get 6 rows of 33% as opposed to just one. Why is it displaying this way? Thanks
The way you're using inline queries, you're executing the same query per row of the outer query. So if your table has six rows, you'd be executing it six time (and getting the same result for each query, naturally). You can simplify things by removing the outer query and using an iif expression:
SELECT SUM (IIF (SurgeryDate Between #1/1/2011# And #12/31/2012#, 1, 0)) /
COUNT(ThyroidSurgeryType) AS Percentage
FROM [Thyroid Surgeries];

How to search value range inside another range in sql

I have a form where i enter min price and max price when creating product details. In the product search i have also 2 fields called min and max. So how can i get the result of given range considering the range given when inserting products
Products
min max
10 15
15 30
20 30
In the search form i insert min as 5 and max as 16. Which products i will get in the result and whats the best theory for searching considering practical situations.
Compare opposite ends of each range to find products in the overlap:
select * from products
where min < $max and max > $min
This approach works well for date ranges too.
select * from products where min>=5 and max<=16
so based on this you will get
Row -> 10, 15
The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.
SQL BETWEEN Operator tutorial
For example:
SELECT * FROM Product
WHERE price
BETWEEN $min AND $max