Firebird insert into according to a condition [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I insert a data from one table to another table according to a column condition
For example one table there is a column.if its value is 1 then insert
Database:Firebird 2.5

Based on the example you provided in your comment:
INSERT INTO some_table (CARI_id, anydatacomefromcari )
SELECT EFATURA_KULLAN, someotherdata FROM CARI
WHERE EFATURA_KULLAN = 1
fiddle example
Results:
The Script used for test is:
CREATE TABLE CARI (id INTEGER, EFATURA_KULLAN INTEGER, someotherdata INTEGER);
INSERT INTO CARI VALUES (1, 1, 55);
INSERT INTO CARI VALUES (2, 1, 44);
INSERT INTO CARI VALUES (3, 0, 99);
INSERT INTO CARI VALUES (4, 1, 12);
SELECT * FROM CARI;
CREATE TABLE some_table (CARI_id INTEGER, anydatacomefromcari INTEGER );
INSERT INTO some_table (CARI_id, anydatacomefromcari )
SELECT EFATURA_KULLAN, someotherdata FROM CARI
WHERE EFATURA_KULLAN = 1
SELECT * FROM some_table
Results:
+---------+-------------------------+
| CARI_id | anydatacomefromcari |
+---------+-------------------------+
| 1 | 55 |
+---------+-------------------------+
| 1 | 44 |
+---------+-------------------------+
| 1 | 12 |
+---------+-------------------------+

Related

SQL check if exists and insert [duplicate]

This question already has answers here:
Is SELECT or INSERT in a function prone to race conditions?
(3 answers)
Closed last year.
I'm using a table 'Customer' with the following schema
id INTEGER NOT NULL UNIQUE,
name TEXT NOT NULL,
auth BOOLEAN DEFAULT FALSE
Now, I want to add a record if does not exist, I can do the following
IF NOT EXISTS (SELECT name from Customer where id=220)
BEGIN
INSERT into Customer (name,id) values ('Jon', 220)
END;
But at the same time, I also want to know if the id really did not exist along with the insertion i.e. True/False result of the select query. I can split it into two queries, from the first I can know if it exists and if id did not then I can insert it. But how can I do this in a single query?
You need to use INSERT with the RETURNING clause (PostgreSQL INSERT).
'on conflict' clause used with INSERT can be customized to serve your purpose.
INSERT INTO <table_name>(<column_name_list)) values(<column_values>) ON CONFLICT(<constraint_column>) DO NOTHING;
ref: https://www.postgresqltutorial.com/postgresql-upsert/
Set up
Step 1: Create the table:
create table test
(
id INTEGER NOT NULL UNIQUE,
name TEXT NOT NULL,
auth BOOLEAN DEFAULT FALSE
);
Step 2: Load the table with some sample rows:
insert into test(id,name) values(1,'vincent'),(2,'gabriel'),(3,'sebastian');
Step 3: Test with an INSERT of a row with existing id i.e 1 , the insert does not go through as the ID already exists:
INSERT INTO test(id,name) values(1,'xavier') ON CONFLICT(id) DO NOTHING;
Step 4: Now test with a row with ID that does not exist.i.e 4. It gets through.
INSERT INTO test(id,name) values(4,'xavier') ON CONFLICT(id) DO NOTHING;
Demo:
postgres=# select * from test;
id | name | auth
----+-----------+------
1 | vincent | f
2 | gabriel | f
3 | sebastian | f
(3 rows)
postgres=# INSERT INTO test(id,name) values(1,'xavier') ON CONFLICT(id) DO NOTHING;
INSERT 0 0
postgres=#
postgres=# select * from test;
id | name | auth
----+-----------+------
1 | vincent | f
2 | gabriel | f
3 | sebastian | f
(3 rows)
--- NOTE: no row inserted as ID 1 already exists.
postgres=# INSERT INTO test(id,name) values(4,'xavier') ON CONFLICT(id) DO NOTHING;
INSERT 0 1
postgres=# select * from test;
id | name | auth
----+-----------+------
1 | vincent | f
2 | gabriel | f
3 | sebastian | f
4 | xavier | f -------> new row inserted.
(4 rows)
you can use the following :
INSERT into Customer SELECT 'Jon', 220
Where Not EXISTS (SELECT 1
from Customer
where id=220);
Select Cast(##ROWCOUNT as bit);

How to loop through selected data in SQL Server? [duplicate]

This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 4 years ago.
I want to select multiple rows of data from a table and add it onto another one.
For example:
Select * from Table1
which would return
id | name
1 | Chad
2 | Mary
3 | Denise
I want to add these rows of data to Table 2
Insert(id, name)
values(#id, #name)
Thank you!
INSERT INTO Table2(id,name)
SELECT t.id,t.name
FROM Table1 t
;

HOW to use STUFF with UPDATE in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have table with 3 column and field Data-Types is Decimal(38,0).
so I want to update each row of my table . actually the length of number is not same as each other I mean some of them is 5 digit and some of them 7 digit but I want after 2 first digit insert '.' (Decimals) for example 123456 should be 12.3456
my table is:
+-------------+-------+-------+
| ID | X | Y |
+-------------+-------+-------+
| 1200 | 321121| 345000|
| 1201 | 564777| 4145 |
| 1202 | 4567 | 121444|
| 1203 | 12747 | 789887|
| 1204 | 489899| 124778|
+-------------+-------+-------+
and I want to change this :
+-------------+--------+--------+
| ID | X | Y |
+-------------+--------+--------+
| 1200 | 32.1121| 34.5000|
| 1201 | 56.4777| 41.45 |
| 1202 | 45.67 | 12.1444|
| 1203 | 12.747 | 78.9887|
| 1204 | 48.9899| 12.4778|
+-------------+--------+--------+
my code is here :
Update [dbo].[UTM]
SET [X] = STUFF([X],3,0,'.')
[Y] = STUFF([X],3,0,'.')
and I try this :
BEGIN
DECLARE #COUNT1 int;
DECLARE #COUNT2 int;
DECLARE #TEMP_X VARCHAR(255);
DECLARE #TEMP_Y VARCHAR(255);
DECLARE #TEMP_main VARCHAR(255);
SELECT #COUNT1 = COUNT(*) FROM [UTM1];
SET #COUNT2 = 0;
WHILE(#COUNT2<#COUNT1)
BEGIN
SET #TEMP_main = (SELECT [id] from [UTM1] order by [id] desc offset #COUNT2 rows fetch next 1 rows only);
SET #TEMP_X = (SELECT [X] from [UTM1] order by [id] desc offset #COUNT2 rows fetch next 1 rows only);
SET #TEMP_Y = (SELECT [Y] from [UTM1] order by [id] desc offset #COUNT2 rows fetch next 1 rows only);
UPDATE [dbo].[UTM1]
SET [X] = CONVERT(decimal(38,0),STUFF(#TEMP_X,3,0,'.'))
,[Y] = CONVERT(decimal(38,0),STUFF(#TEMP_Y,3,0,'.'))
WHERE [id] = #TEMP_main;
SET #COUNT2 = #COUNT2 + 1
END
END
I wouldn't use stuff()-- your values don't look like strings they look like numbers. You should be doing arithmetic operations. However, if the columns are integers, you need to first convert them to an appropriate type:
alter table utm alter column x decimal(12, 4);
alter table utm alter column y decimal(12, 4);
Then do an update with division:
Update [dbo].[UTM]
SET x = x / 10000
y = y / 10000;
EDIT:
If the column is decimal(38, 0), then you have no room for digits after the decimal points. The 0 is the number of digits to the right of the decimal point.
So, change the type of the column to allow digits. The (12, 4) is a suggestion. You can also use (38, 4) -- or many other combinations.
EDIT II:
You have a real problem with your data. You can not tell if "1" should be 10.0000, 01.0000, 0.1000, 0.0100, 0.0010, or 0.0001. I would suggest that you re-import the data using the correct type.
You can do the transformation -- after fixing the data types of the column. One method is via strings (as you have attempted):
Update [dbo].[UTM]
SET x = convert(decimal(12, 4), stuff(convert(varchar(255), x), 3, 0, '')),
y = convert(decimal(12, 4), stuff(convert(varchar(255), y), 3, 0, ''))
However, I suspect that this is quite dangerous, because you may be missing leading zeros on your values and converting to the wrong value. Numeric types in SQL do not store leading zeros.

How to remove duplicate rows in postgresql? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I would like to remove duplicate entries in Postgresql.
There is no unique constraint, but I would like to consider all columns together to consider a row as a duplicate.
So we have a table containing following rows :
id | name | age | started_date |Score |
-----|----------|---------------|---------------|------|
1 | tom | 15 | 01/06/2022 |5 |
2 | tom | 15 | 01/06/2022 |5 |
3 | henry | 10 | 01/06/2022 |4 |
4 | john | 11 | 01/06/2022 |6 |
...
I would like to consider all columns together to identify the duplicate rows.
How to achieve this in Postgresql ?
PostgreSQL assigns a ctid pseudo-column to identify the physical location of each row. You could use that to identify different rows with the same values:
-- Create the table
CREATE TABLE my_table (num1 NUMERIC, num2 NUMERIC);
-- Create duplicate data
INSERT INTO my_table VALUES (1, 2);
INSERT INTO my_table VALUES (1, 2);
-- Remove duplicates
DELETE FROM my_table
WHERE ctid IN (SELECT ctid
FROM (SELECT ctid,
ROW_NUMBER() OVER (
PARTITION BY num1, num2) AS rn
FROM my_table) t
WHERE rn > 1);
DB Fiddle
Let say your table has 2 columns, you can identify duplicates using.
Post this :-
1) Insert this result into a temp table
2) Drop data from Main table
3) Insert data from temp table into main table
4) Drop temp table.
select col1, col2, count(*) as cnt
from table1
group by col1, col2
having cnt > 1

Crosstab SQL query for 2 tables? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have 2 tables and I need to make one view of them like if it was 1 single table
Table1 DEVICE
+-----+-------+-----------+
|DevID|DevName|DevIP |
+-----+-------+-----------+
|1 |HH1 |192.168.1.1|
+-----+-------+-----------+
|2 |HH2 |192.168.1.2|
+-----+-------+-----------+
Table2 DEVICECUSTOMDATA
+-----+------------+--------+
|DevID|Name |Value |
+-----+------------+--------+
|1 |Model |CN70 |
+-----+------------+--------+
|1 |BuildVersion|1.2 |
+-----+------------+--------+
|1 |BuildDate |20140113|
+-----+------------+--------+
|2 |Model |MC55 |
+-----+------------+--------+
|2 |BuildVersion|1.2 |
+-----+------------+--------+
|2 |BuildDate |20140110|
+-----+------------+--------+
The resulting table should be:
+-----+-------+-----------+-----+------------+---------+
|DevID|DevName|DevIP |Model|BuildVersion|BuildDate|
+-----+-------+-----------+-----+------------+---------+
|1 |HH1 |192.168.1.1|CN70 |1.2 |20140113 |
+-----+-------+-----------+-----+------------+---------+
|2 |HH2 |192.168.1.2|MC55 |1.2 |20140110 |
+-----+-------+-----------+-----+------------+---------+
I would appreciate any help to do this. Thanks
SQL SERVER:
See SqlFiddle:
SELECT d.DevId, d.DevName, d.DevIp, p.Model, p.BuildVersion, p.BuildDate
FROM DEVICE d
JOIN (
SELECT *
FROM DEVICECUSTOMDATA
PIVOT (MAX(Value) FOR Name IN ([Model], [BuildVersion], [BuildDate])) as Something) p
on d.DevId = p.DevId
Working Online Example (SQL Server Syntax): SQL Fiddle
Result:
SQL Script:
DECLARE #Device TABLE (
DevID int not null,
DevName varchar(max) not null,
DevIP varchar(max) not null
)
insert into #Device values ('1', 'HH1','192.168.1.1')
insert into #Device values ('2', 'HH2','192.168.1.2')
DECLARE #DeviceCustomData TABLE (
CDevID int not null,
Name varchar(max) not null,
Value varchar(max) not null
)
insert into #DeviceCustomData
values ('1','Model','CN70')
insert into #DeviceCustomData
values ('1','BuildVersion','1.2')
insert into #DeviceCustomData
values ('1','BuildDate','20140113')
insert into #DeviceCustomData
values ('2','Model','MC55')
insert into #DeviceCustomData
values ('2','BuildVersion','1.2')
insert into #DeviceCustomData
values ('2','BuildDate','20140110')
SELECT *
FROM
(SELECT d.DevID, d.DevName, d.DevIP, c.Value, c.Name
FROM #Device d
inner join #DeviceCustomData c on d.DevID = c.CDevID) AS SourceTable
PIVOT(
MIN(Value)
FOR Name in ([Model],[BuildVersion],[BuildDate])
) as PivotTable
Reference: http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx