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

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?

Related

Inserting st_length function doesn't match row numbers

I am new to sql/postgres and am trying to run st_length on a database geometry and insert the length as a cost column but the result from running the following commands inserts the data seemingly randomly and not associated with the id column value like needed.
Command:
alter table planet_osm_roads add cost float;
insert into planet_osm_roads (cost)
select st_length(st_transform(way, 4326)::geography) from planet_osm_roads;
example result:
source
target
cost
30,749
30,750
30,751
30,752
7,552
30,385
7.6144929361
41.7331770846
85.3575622508
50.0921684238
3
4
111.5246694513
43.8658606368
I've ignored the other columns as they aren't needed. The columns with 'source' and 'target' values are associated with a specific 'osm_id' value and the cost column is null for those but the commands don't associated the cost value with the linestring row value.
I would expect the cost value to be inserted in the same row as to which the linestring data comes from. That is not what happens.
Insert adds new rows. You want to Update the table and Set cost where some row matches.
ALTER TABLE planet_osm_roads ADD cost FLOAT;
UPDATE planet_osm_roads
SET cost = 7.6144929361
WHERE source = 30,749
or target = something
or both
It's possible to do this from a select but how depends on your schema and data. Depending on your version of PostGres, you may even be able to do it with a generated column - Add generated column to an existing table Postgres

insert and update from table a to table b by combining 2 column from table a in postgres

I have 2 tables name Table A and Table B
"Table A has 10 columns named ta1, ta2, ta3...ta10"
"Table b has 2 columns name id (PK,AI) and tb1"
My requirement is to combine "ta3" and "ta4" from table A initially,
or it will check for the "tb1" column whether any same data is available, if available do nothing else insert the new data.(if a new row is added in the ta3 or ta4, it should be added to the 2nd table column named tb1)
I have checked other functions in the for combining the data from ta3 and ta4 by using the "unnest" function, but if I use that, I'm getting stuck in the insert, as it will insert on each query rather than checking whether any data is there or not.
any solution on this legends.
simply I just want to combine 2 column from 1st table and store it in a column on 2nd table, but in 2nd table's column it should be only distinct values taken from both of those 1st table columns. this should automatically check for the values in the column of 2nd table and compare that with the new one and if there is change the insert else neglect. I hope my question is clear.
Table1
Table 2
new table-1
Expected table-2

String or binary data would be truncated - when inserting to #temp table

My #temp table column is defined as VARCHAR(10), input eg: value is 'Azure'. The data is coming from a table where the column is defined as VARCHAR(100), but the TOP 1 query that inserts into the temp table is bringing back only values as big as 6 but I am still getting a error:
String or binary data would be truncated
Even though the specific values being inserted is of max size 6, if there are bigger values in the source table, say eg: 'Slightly bigger text' (which is 20), the #temp insert fails if it is a TOP query.
Safe bet, size your temp table columns as per the original size of the base data column definition, and you wouldn't waste time like me :) There are so many bigger VARCHAR columns in the #temp table being inserted along with, and one wouldn't think that a VARCHAR(10) vs VARCHAR(20) would make a difference for a value with LEN()=5.

Insert null in one column on two columns same value in Oracle sql

i have inserted value from one table to another and i want to check if two columns values are same in a row insert null in one column of another table(table in which i am inserting values. In my example it is table1).
My existing query is given as follows how should i convert it according to my requirement.
INSERT INTO table1 (date1,date2)
SELECT substr(numtodsinterval(MAX(date1)-MIN(date2),'day'),
12,8)
FROM table2 where ....;

SQL: difference of 2 columns in a new column

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
);