Value of a table column is not what it seems - sql

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

Related

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

A simple SUM on all records of a column doesn't work in Big Query

Hi I am trying to find total revenue earned by cabbies from this data set on google big query here but sum(total_amount) doesn't seem to work. It says
Error: Field total_amount is of type STRING which is not supported for
SUM
even though it is defined as numeric data type by default. I did some casting things but then the sum says 0.
I don't seem to find why.
Things I tried:
Specifying the column as INTEGER in SUM like SUM(INTEGER(total_amount)) returned 0.
CASTED the column name to INTEGER using CAST type returned 0.
Checked if there are any NULL columns but there is none.
Here is my query:
Select sum(total_amount) from
[bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
What am I missing here?
NUMERIC data type in Legacy SQL has limited support
So, instead try running your query in BigQuery Standard SQL as in below example
#standardSQL
SELECT SUM(total_amount)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
with result
Row f0_
1 1837861124.95

Spotfire Running Balance (Cumulative Sum)

I am trying to create a running balance column in Spotfire that's supposed to look like the picture attached below. In essence,I want to calculate the cumulative total of the "amount" column row by row, and that I want it to start from 0 as the date changes.
I have tried several OVER functions:
Sum([AMOUNT]) OVER AllPrevious([Date])
Sum([AMOUNT]) OVER Intersect([CURRENCY],AllPrevious([SETTLEDATE]))
Sum([AMOUNT]) OVER Intersect([Calculation Date],AllPrevious([SETTLEDATE]))
Any help is greatly appreciated.
You were very close with your first over statement. The problem is, when you use over (AllPrevious([Date])) and you don't have 1 row for each date, then you will skip rows. So, the last row of your data would only sum it over the rows where 6/1/2017 is in the Date column. Instead, we need to apply a RowID to your data set and then sum over it. This will ensure we sum over all previous rows.
Assuming your data set is in the order you want it to be in when you bring it into SpotFire, do the following:
Insert a calculated column RowID() and name it RowID
Use this calculation: Sum([amount]) over (Intersect([Date],AllPrevious([RowID])))
This will give you the running sum you are looking for.
#scsimon- I modified your custom expression slightly to include date as requested in the question.
Modified expression:
Sum([Amt]) over (intersect(Allprevious([rowID]),[Date]))
Final output table:
#LeoL - Hope this answers your question.

Price Calculating formula in 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

SQL Server : aggregate function doesn't work?

I have imported a price list from a csv to my SQL Server database. That has worked fine. But now some weird stuff. Table is named PRICE which includes a column (and some more) Endprice and a total of 761 rows. All datatypes are varchar(50).
SELECT MAX(Endprice)
FROM PRICE
When I want this simple SQL statement to show the highest price in the column, I get a wrong result. I don't know why.
I get 98,39 as a result, but that's definitively wrong, it must be 100,73.
Here you can see a part of the data:
And now the wrong MAX() result:
BUT when I'm using the MIN function I get the highest one!? The min is somewhere at ~50 (not shown in the screenshot part).
`
The resultset of SELECT Endprice FROM PRICE is correct. I am at my wit's end.
This is because your column is a varchar, so it is determining the min or max based on characters. The column should be a decimal or money type, so it sorts by the value of your number (instead of an alphabetic sort like you are getting now).
Alphabetic sort: 9 is more than 1, thus 98.39 is the max.
The reason is because price is a varchar().
Here are two solutions:
order by len(price), price
This works assuming that all the price values have the same structure.
Or:
order by cast(price as float)
If you could have non-numeric values (always a danger when storing numbers in the wrong data type):
order by (case when isnumeric(price) = 1 then cast(price as float) end)
Or better yet:
alter table alter column price money
Then you don't have to worry about having the wrong type for the column.
Your problem is Endprice columns is varchar(50), therefore it is comparing strings not numbers, which means that a 9>1 no matter what cames next of the first digit. You have to convert it to a number before the max!
Also you really should consider in doing what #a_horse_with_no_name suggested change your column into a number like column type.
This is a example on how you solve your actual problem
select max(cast(endprice as money)) from sample
See it here: http://sqlfiddle.com/#!3/767f6/1
Note that I used . as a decimal separator it will depend on your database language setup.