SQL: difference of 2 columns in a new column - sql

I have the following SQL table, with a positive column and a negative column, both ints.
positive negative
----------------------
5 3
3 1
10 7
How can I create a third column, e.g. total that is equal to positive - negative. Also, I would like the total column to be updated every time an element of the positive or negative column changes.
How can I do this in SQL?
Edit: I am using MariaDB

make use of computed column as described below
you can create table as
create table table_name ( positive int, negitive int, difference as positive-negitive)
then after creating, if you enter values as
insert into table_name values(3,2)
--no need to enter third column it is called as computed column.
then after inserting the difference will be present in the third column "difference"

Use a virtual computed column as explained here https://mariadb.com/kb/en/mariadb/virtual-computed-columns/
create table table1
(
positive int not null,
negative int not null,
difference int as (positive - negative) virtual
);

Related

insert range of values in sql

CREATE TABLE RECHARGE(
R_LVL VARCHAR(20) NOT NULL PRIMARY KEY,
AMOUNT NUMBER,
POINTS_1 NUMBER
);
insert into RECHARGE
VALUES ('S1',(2950-4950),250);
i have the code above, and im trying to insert a range for the values in attribute amount, like this :
s1 : 2950-4950
s2: 5000-9950
s3: 10000-30000
so each lvl has it's own range of data, is it possible ?
Number datatype cannot store special characters like -. You have to make it either VARCHAR or store 2 rows with single number like -
insert into RECHARGE
VALUES ('S1',2950,250);
insert into RECHARGE
VALUES ('S1',4950,250);
Or you can use 2 colums also like FROM-TO to show your date range like -
CREATE TABLE RECHARGE(
R_LVL VARCHAR(20) NOT NULL PRIMARY KEY,
AMOUNT_FROM NUMBER,
AMOUNT_TO NUMBER,
POINTS_1 NUMBER
);
insert into RECHARGE
VALUES ('S1',2950,4950,250);
Then in SELECT query you can generate the result like you want.
Range consists of 2 values, store it in 2 columns.
The expression (2950-4950) equals to -2000 (2950 minus 4950).
You should create two columns or have only just one of the boundaries of the ranges stored.
I suggest to store both values, because it is easier to query:
CREATE TABLE RECHARGE(
R_LVL VARCHAR(20) NOT NULL PRIMARY KEY,
LOWER_BOUNDARY NUMBER,
UPPER_BOUNDARY NUMBER,
POINTS_1 NUMBER
);
You need to decide if the boundaries are closed (<=, >=) or open (<, >).
So you can query them like this:
SELECT * FROM Recharge WHERE LOWER_BOUNDARY <= 2000 AND UPPER_BOUNDARY > 2000
Where 2000 is the value you are trying to find the range for.
You can make this thing more bulletproof by adding a few constraints:
CHECK constraint to make sure that LOWER_BOUNDARY is lower than
UPPER_BOUNDARY.
UNIQUE on LOWER_BOUNDARY to eliminate repetition
FOREIGN KEY to reference the UPPER_BOUNDARY from the previous range
for the current ones LOWER_BOUNDARY (boundaries are continuous)
it is possible
use the next query
insert into RECHARGE
VALUES ('S1',(2950-4950),250)
('S2',(5000-9950),250)
('S3',(10000-30000),250)
;

How to insert default value in unknown column

I have a function that INSERT INTO a table some data. I do not know how much columns the table has (let it be N).
My func gets a list of N-1 params and then
INSERT INTO ... VALUES( *N-1 params* )
The last N-th column is a standart ID column, which i want to set DEFAULT (ID column has the default value = "max" and is auto incremented)
The main problem: due to the fact that i do not know names of the columns i am working with, i can't manually enter data into the database.
How to insert a row of data, if I don't know the number of columns, but I know that I have 1 less than it should be and the last column is auto incremented / has the default value?
If you know that the order of the N-1 params is exactly the same as the order of the columns in the definition of the table (the CREATE stamenet), then there is no need to enumerate the names of the columns in the INSERT statement.
Add, 1 more NULL value to the list for the ID column (at the end of the list if, as you say it is defined last):
INSERT INTO tablename VALUES(param1, param2, ..., paramN-1, NULL)
By passing NULL for the ID, it will be incremented since it is defined as AUTOINCREMENT.
See a simplified demo.

How to update rows in one column of a Table with increment number in minus in Sybase ASE

I have to update around 40 rows in a column of a table with increment of numbers in minus i.e -1,-2,-3,-4 etc in sybase ASE. Can any one tell how to do
Raj, identity values in ASE cannot be set to maintain a sequence of negative values as you want. So that eliminates the simplest approach.
Let's assume your table looks like this:
CREATE TABLE cars (
id not null,
name varchar(20),
serial int null
)
where serial is the column that may contain NULL values.
You could create a trigger on the table to update serial when it is null.
Alternatively if you are trying to update existing values in this table you could run the following code 40 times:
UPDATE cars
SET serial = -(SELECT COUNT(*) FROM cars WHERE serial IS NULL)
WHERE id = (SELECT MAX(id) FROM cars WHERE serial IS NULL)
Of course, for 40 rows manually updating the values sounds attractive!

Package which inserts data to a table inserts wrong value (NULL)

I have a package which inserts data to a table. However, there are few columns which the values that were inserted are NULL instead of an existing value based on the source table.
example:
Price.SourceTable
- ColHours float
- ColUnit varchar(10)
- ColVolume float
Price.DestinationTable
- ColHours int
- ColUnit varchar(10)
- ColVolume float
The values that I get in the destination table is NULL for the three columns.
however, there is one column (ColVolHours) which is a calculation from the 2 columns - ColHours * ColVolume - that has the actual value.
How is this possible that the ColVolHours has value whereas the 2 other columns were NULL?
And how can I know why the values inserted for the 3 columns were null?

Adding a new column in a temporary table

I have a temporary table in a PostgreSQL function and I want to insert a new VARCHAR column. It should have a value that depends on another column of the table, named "amount".
When the amount is positive I would like the value of the column row to be credit and when the value is negative the column should be debit.
I have one more request: I want to round the value of amount column in 2 decimal digits
You want ALTER TABLE ... ADD COLUMN followed by an UPDATE.
I initially said ALTER TABLE ... ADD COLUMN ... USING but that was wrong on two counts. ADD COLUMN takes a DEFAULT not USING - and You can't do it in one pass because neither a DEFAULT expression nor a USING expression may not refer to other columns.
So you must do:
ALTER TABLE tablename ADD COLUMN colname varchar;
UPDATE tablename SET colname = ( CASE WHEN othercol < 0 THEN 'Credit' ELSE 'Debit' END );
Think carefully about whether zero should be 'Debit' or 'Credit' and adjust the CASE accordingly.
For rounding, use round(amount,2). There isn't enough detail in your question for me to be sure how; probably by UPDATEing the temp table with UPDATE thetable SET amount = round(amount,2) but without the context it's hard to know if that's right. That statement irreversibly throws information away so it should only be used on a copy of the data.