numeric columns ignore decimal values in mariadb? - sum

In mariadb or maybe others, decimal values in numeric type columns are not included in calculation. For example,
MariaDB [mydb]> create table test (a decimal(5,0),b double(5,0));
MariaDB [mydb]> insert into test values (1.5,1.5);
MariaDB [mydb]> insert into test values (1.5,1.5);
MariaDB [mydb]> select format(sum(a),3) from test;
+------------------+
| format(sum(a),3) |
+------------------+
| 4.000 |
+------------------+
MariaDB [mydb]> select sum(b) from test;
+--------+
| sum(b) |
+--------+
| 2 |
+--------+
Why don't they return "3" what I want?
And why are their returns different?

The (m,0) says to store 0 decimal places. So, 1.5 is rounded up (or down?) to 2 (or 1?).
The SUM() function works on what is stored, and knows nothing about the original 1.5.
DECIMAL(5.0) says to keep only 5 digits to the left of the decimal place, and none to the right.
DOUBLE(5.0) says even worse things; do not use it. Use plain DOUBLE.

Related

Add a dot notation in Label SQL

I would like to return my label in SQL so they look as dot notation.
Something like this should be return:
|---------------------|------------------|
| product.attribute | product.order |
|---------------------|------------------|
| product A | 34 |
|---------------------|------------------|
However, when I try to insert the dot in my label.
It returns me an error.
I am sure it is something obvious that I missed.
Example of query :
SELECT product as "product.attribute", Count(Order) as "product.order",
from orderTable
Limit 100
Probably you are using MySQL, in this case you should use backticks:
SELECT `product` AS `product.attribute`, COUNT(`order`) AS `product.order`,
FROM `orderTable` LIMIT 100
Otherwise the engine will complain about using reserved keywords (like order).
I depends on the database:
In Oracle, DB2, or PostgreSQL you can do:
SELECT product as "product.attribute"...
In SQL Server, or Sybase you can do:
SELECT product as [product.attribute]...
In MariaDB or MySQL you can do:
SELECT product as `product.attribute`...

My PostgreSQL calculations don't include decimals

I don't dabble in SQL queries much and rely on google when I need something more than the basics, and have come up with a problem.
I am trying to calculate a value and it returns a result rounded down to the nearest integer.
To test this out, I wrote the following query:
select ELAPTIME AS "ELAPSEC", ELAPTIME/60 AS "ELAPMIN" from CMR_RUNINF
The result is:
+-----------+-----------+
|ELAPSEC |ELAPMIN |
+-----------+-----------+
|258 |4 |
+-----------+-----------+
|0 |0 |
+-----------+-----------+
|2128 |35 |
+-----------+-----------+
|59 |0 |
+-----------+-----------+
I'm trying to do a bit more than this, but I've simplified it to make it easier to explain the problem. How do I ensure that this calculation returns the decimal point?
postgres=# SELECT 258/60::float;
?column?
----------
4.3
(1 row)
Your SQL product performs integral division because both operands are integers. ELAPTIME's integer type is determined be the table structure and 60 is automatically assumed to be integer because it has no decimal point.
There are two methods of resolving the issue:
Convert either operand to a non-integer numeric type explicitly:
CAST(ELAPTIME AS float) / 60
Write 60.0 instead of 60 so that the parser can see you are not dividing an integer by an integer:
ELAPTIME / 60.0
Simply try this
SELECT ELAPTIME AS "ELAPSEC", ELAPTIME/60 :: Float AS "ELAPMIN"
FROM CMR_RUNINF
Or:
SELECT ELAPTIME AS "ELAPSEC", ELAPTIME/60 :: Real AS "ELAPMIN"
FROM CMR_RUNINF
Fiddle Demo

SQL Query Update is not working

I am using pawn script for something, and everything works great except for one of my queries. For some reason, it will not work, and I am hoping it is simple enough someone can spot my mistake as I have been banging my head on it for days.
http://ampaste.net/m6a887d30
The two highlighted lines are the queries that are not working. The other one works fine, but the values for 'class1kills' and 'class2kills' remain at 0. Here is a screenshot from phpmyadmin incase I did something silly.
http://brutalservers.net/sql.png
Your SQL-code, copied from where you pasted it:
UPDATE global SET class1kills = class1kills + 1
In addition to what the user Marcus said, even if there is a row in the table, but it's value is NULL, then adding to the value will not work. You will have to set it to an integer value first, such as 0.
E.g.:
mysql> create table mytable(a int);
mysql> insert into mytable(a) values (0),(NULL);
mysql> select * from mytable;
+------+
| a |
+------+
| 0 |
| NULL |
+------+
mysql> update mytable set a = a+1;
mysql> select * from mytable;
+------+
| a |
+------+
| 1 |
| NULL |
+------+
The NULL value was not updated!
By the way, are you sure you want to update the complete table?
Try inserting a row into global, and then updating it.
Note that without a WHERE clause on your UPDATE statement, all rows will be updated.
Is MySQL case sensitive when it comes to comparing strings? Otherwise check encodings etc. That is all I can think of.
Sorry guys, turns out it is a bug with the scripting language. For some reason two queries right after each other causes the second one not to be called properly.
Thank you all for all of the help!

Match a Query to a Regular Expression in SQL?

I'm trying to find a way to match a query to a regular expression in a database. As far as I can tell (although I'm no expert), while most DBMS like MySQL have a regex option for searching, you can only do something like:
Find all rows in Column 1 that match the regex in my query.
What I want to be able to do is the opposite, i.e.:
Find all rows in Column 1 such that the regex in Column 1 matches my query.
Simple example - say I had a database structured like so:
+----------+-----------+
| Column 1 | Column 2 |
+----------+-----------+
| [a-z]+ | whatever |
+----------+-----------+
| [\w]+ | whatever |
+----------+-----------+
| [0-9]+ | whatever |
+----------+-----------+
So if I queried "dog", I would want it to return the rows with [a-z]+ and [\w]+, and if I queried 123, it would return the row with [0-9]+.
If you know of a way to do this in SQL, a short SELECT example or a link with an example would be much appreciated.
For MySQL (and may be other databases too):
SELECT * FROM table WHERE "dog" RLIKE(`Column 1`)
In PostgreSQL it would be:
SELECT * FROM table WHERE 'dog' ~ "Column 1";

format integer to string

I have an integer field in a table and I want to make a query to format the integer value of this field in an char or double field with a especific format.
For example, if my value in the table is 123456 I want to format it as "###.###" what means the result should be like this: 123.456
I've done this using CONCAT function, but the result is not very elegant. I would like to use another funciont spacific for this purpose.
I would suggest doing this in your presentation layer rather than the DB.
This is pretty easy in C#:
// Assuming value is an int
value.ToString("N");
More details on formatting int in various ways see the Microsoft documentation
Maybe you would like to use formatting like '###,###.###' ?
Here is the example.
mysql> select FORMAT( 123446, 4 );
+---------------------+
| FORMAT( 123446, 4 ) |
+---------------------+
| 123,446.0000 |
+---------------------+
1 row in set (0.02 sec)
mysql> select FORMAT( 123446, 0 );
+---------------------+
| FORMAT( 123446, 0 ) |
+---------------------+
| 123,446 |
+---------------------+
1 row in set (0.00 sec)