Replace rows from table that has specific value of multiple columns? - sql

Let's say I have a table like this called MyTable.
| Column A | Column B || Column C | Column D |
| -------- | -------- || -------- | -------- |
| Cell 1 | Cell 2 || Cell 3 | Cell 4 |
| Cell 5 | Cell 6 || Cell 7 | Cell 8 |
And now I am inserting new row into this table that has format like this:
| Cell 1 | Cell 2 || Cell 3 | Cell Something else |.
What I want to do is replace an existing row from MyTable if the row I am inserting has the same value of the first 3 columns of MyTable (column A, column B, column C). As my real table has 250+ columns and
I want to replace rows if they have same value of 5 columns, I don't think INSERT ON CONFLICT UPDATE is good for this. In my opinion, it would be best to DELETE rows that need to be replaced and just INSERT new ones, but I don't know how to write that query.
I was thinking of INSERT ON CONFLICT UPDATE but firstly: I don't think I can specify more columns in ON CONFLICT part, and secondly: I think that I would need to specify 250 columns in UPDATE part, so that also doesn't work for me.

There is no problem specifying multiple columns in the on conflict clause, you just need a unique constraint on those columns. (see demo). As far as you having 250 columns (a highly questionable design, but another question altogether) you have no way around it you must list every column you want updated.

Related

OBIEE Concatenate on null values

I am trying to concatenate two columns in OBIEE prompts using pipes (||" "||) and it works but the problem is that one table has more rows and I want to eliminate the extra rows:
Here is an example:
Col A | Col B
---------+---------
abcdefgh | ijklmn
opqrstuv | ** Remove these ones **
wxyz123 | anything

Sqlite : Loop through rows and match, break when distinct row encountered

I want to compare two tables A and B, row by row on the basis of a column name.As soon as I encounter a distinct row I want to break.
I want to do this using a query, something like this :
select case
when ( compare row 1 of A and B
if same continue with row+1
else break
)
if all same, then 1
else 0
end
as result
I am not sure how to loop through rows and break? Is it even possible in sqlite?
EDIT
Table looks like this
-------------------------- -----------------------------------
id | name id | name
-------------------------- -----------------------------------
1 | A 1 | A (same)
2 | C 2 | C (same)
3 | B 3 | Z (different break)
4 | K
Both tables have same structure. I want to just compare the names row by row, to see whether there is any order difference.

ADO - how to select column from xls file where two or more columns have the same name?

I have an excel file like this:
| | A | B | C | D |
| 1 | Name 1 | Name 2 | Name 3 | Name 2 |
| 2 | Data | Data | Data | Data |
| 3 | Data | Data | Data | Data |
As you can see, headers of two columns have the same name - Name 2.
My question is, is it possible to tell the ADO engine from which column to select data?
Currently my select looks like this:
SELECT [Name 1], [Name 2] FROM [REPORT7_RAW$] WHERE [Name 1] IS NOT NULL
and ADO picks up the data from column which is listed under column B in excel. In other words it takes the first column which have the given name. Unfortunately I have two columns with the same name and I would like to pull out the data from column D. Is it possible?
I could not find any way to select column by its index rather the name.
You will need to change your connection string so that data header names are not used. The normal connection string would look something like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
You need to change the last bit, HDR=YES, to HDR=NO.
With that type of connection, the columns(fields) then become F1, F2, etc., where F1 = column A, F2 = column B, etc.
This is not ideal, since you are now essentially running the query based on the number of the column rather than the name, but with duplicate column names, this is the only way around that.
Per the comment from #barrowc: This format of the connection string will treat your column names as data. So depending on your query, you may need to include code to filter out the row that contains your column names.

Increasing a +1 to the id without changing the content of a column

I have this random table with random contents.
id | name| mission
1 | aaaa | kitr
2 | bbbb | etre
3 | ccccc| qwqw
4 | dddd | qwert
5 | eeee | potentials
6 | ffffffff | toto
What I want is to add in the above table a column with id=3 with different name and different mission BUT the OLD id =3 I want to have an id = 4 with the name and the mission that it had before when it was id=3, and the OLD id =4 become id=5 with the name and mission of id 5 and so on.
its like i want to enter a column inside of the columns and the below column i want to increase there id +1 but the columns rest the same. example below:
id | name| mission
1 | aaaa | kitr
2 | bbbb | etre
3 | zzzzzz| zzzzz
4 | ccccc| qwqw
5 | dddd | qwert
6 | eeee | potentials
7 | ffffffff | toto
why I want to do this ? I have a table that has 2 CLOB. Inside of those CLOBS there are different queries ex: id =1 has clob of creation of a table id=2 inserts for the columns id=3 has creation of another table id=4 has functions
if you add all of this id in one text(or clob) they will have to create then inserts then create then functions. that table it is like a huge script .
Why I am doing this ? The developers are building their application and they want the sql to work in specific order and I have 6 developers and am organizing the data modeling and the performance and how the scripts are running .So the above table is to organize the calling of the scripts that they wany
Simply put, don't do it.
This case highlights why you should never use any business value, i.e. any 'real world values' for a Primary Key.
In your case I would recommend primary keys not be used for any other purposes.
I recommend you add an extra column 'order' and then change THAT column in order to re-order the rows. That way your primary key and all the other records will not need to be touched.
This avoid the issue that your approach would need to change ALL the database records below the current record which seems like a really bad approach. Just imagine trying to undo that update ;)
Some more info here: https://stackoverflow.com/a/8777574/631619
UPDATE random_table r1
SET id =
(SELECT CASE WHEN id > 2 THEN id+1 ELSE id END id FROM random_table r2
WHERE r1.mission=r2.mission
)
Then insert the new value.

Insert a value from one cell into another cell from the same record

How can I insert a value from once cell into another cell from the same record for each record in a table, overwriting the original value from the destination?
It's a one time query. Using Sql server 2008
e.g.:
origin|destination
------|-----------
1 | A
2 | B
3 | C
to
origin|destination
------|-----------
1 | 1
2 | 2
3 | 3
update into myTable(destination)
?
Try:
update yourtable set destination = origin;
Without a where clause, this will apply to every row in the table.