I'm updating an iPhone app with a SQLite3 database. The user's have a database on their phone currently, and I need to update three of the columns with new data (stored in a separate database) if the id of the rows match.
I've been able to attach the two tables and copy an entire table, but not only three columns.
database1
table1
id, col1, col2, col3, col4
database2
table1
id, col1, col2, col3, col4
I want to copy col1, col2, & col3 (not col4) from database1, table1 to database2, table1 if the ids match.
You could use a query along the following lines:
//Select the database, table and table values
SELECT INTO 'db2.table2' (field1, field2, field3)
VALUES
//Insert data into second database using nested SQL
(SELECT * FROM 'db1.table1' (field1, field2, field3) WHERE field1 = 1)
Hope this helps (and works for you) :)
Related
I have a Tableau dashboard drawing data from a Vertica Database via a Custom SQL Query.
The database table contains more than 100 million rows, with a column COL1 indicated as primary key. Each COL1 value corresponds to exactly one row of data. Therefore COL1 is unique for all rows.
The Custom SQL Query below refreshes the dashboard whenever the parameter is updated.
SELECT COL1, COL2, COL3, COL4, COL5 FROM TABLE WHERE COL1=<Parameters.Col1Param>
Can the dashboard users input more than one value to get more than 1 row of data?
I have tried using the IN condition as below:
SELECT COL1, COL2, COL3, COL4, COL5 FROM TABLE WHERE COL1 IN (<Parameters.Col1Param>)
However, I can't seem to be able to make this work with Parameter values Param1;Param2;Param3 or Param1,Param2,Param3.
I also tried including all values of COL1 and allowing the user to filter on-the-fly, but the database table is too large (over 100M of rows) for the dashboard to load the data into memory.
As always, minutes after posting a question on StackOverflow, I find a reasonable answer to my question.
The answer to this can be found here: Convert comma separated string to a list
SELECT COL1, COL2, COL3, COL4, COL5 FROM TABLE WHERE COL1 IN (
SELECT SPLIT_PART(<Parameters.Col1Param>, ';', row_num) params
FROM (SELECT ROW_NUMBER() OVER () AS row_num FROM tables) row_nums
WHERE SPLIT_PART(<Parameters.Col1Param>, ';', row_num) <> ''
)
I am looking to update a Table B from Table A (Master Table) when ever a new record is published.
the table data is same and just a dump to table B to maintain more data volume.
I am looking to write a stored proc for this and below is what i have, but it throws an error at the begin statement
ID column is the unique identifier here. and DB is PostgreSQL
create or replace procedure proc 1 ()
as
begin
INSERT into table B (Col1, Col2, Col3, etc)
select (Col1, Col2, Col3, etc) from table A
where not exists (
select (Col1, Col2, Col3, etc)
from table B
where table A.id = TableB.id
end
Any help is much appreciated.
I am using SQL Server where I have designed a view to sum the results of two tables and I want the output to be a single table with the results. My query simplified is something like:
SELECT SUM(col1), col2, col3
FROM Table1
GROUP BY col2, col3
This gives me the data I want, but when updating my EDM the view is excluded because "a primary key cannot be inferred".
With a little research I modified the query to spoof an id column to as follows:
SELECT ROW_NUMBER() OVER (ORDER BY col2) AS 'ID', SUM(col1), col2, col3
FROM Table1
GROUP BY col2, col3
This kind of query gives me a nice increasing set of ids. However, when I attempt to update my model it still excludes my view because it cannot infer a primary key. How can we use views that aggregate records and connect them with Linq-to-Entities?
As already discussed in the comments you can try adding MAX(id) as id to the view. Based on your feedback this would become:
SELECT ISNULL(MAX(id), 0) as ID,
SUM(col1),
col2,
col3
FROM Table1
GROUP BY col2, col3
Another option is to try creating an index on the view:
CREATE UNIQUE CLUSTERED INDEX idx_view1 ON dbo.View1(id)
I use this code alter view
ISNULL(ROW_NUMBER() OVER(ORDER BY ActionDate DESC), -1) AS RowID
I use this clause in multi relations view / table query
ROW_NUMBER never give null value because it never seen -1
This is all I needed to add in order to import my view into EF6.
select ISNULL(1, 1) keyField
What I'm trying to do is select various rows from a certain table and insert them right back into the same table. My problem is that I keep running into the whole "duplicate PK" error - is there a way to skip the PK field when executing an INSERT INTO statement in PostgreSQL?
For example:
INSERT INTO reviews SELECT * FROM reviews WHERE rev_id=14;
the rev_id in the preceding SQL is the PK key, which I somehow need to skip. (To clarify: I am using * in the SELECT statement because the number of table columns can increase dynamically).
So finally, is there any way to skip the PK field?
Thanks in advance.
You can insert only the values you want so your PK will get auto-incremented
insert into reviews (col1, col2, col3) select col1, col2, col3 from reviews where rev_id=14
Please do not retrieve/insert the id-column
insert into reviews (col0, col1, ...) select col0, col1, ... from reviews where rev_id=14;
I am inserting only new records that do not exist in a live table from a "dump" table. My issue is there is an identity column that I don't want to insert into the live, I want the live tables identity column to take care of incrementing the value but I am getting an insert error "Insert Error: Column name or number of supplied values does not match table definition." Is there a way around this or is the only fix to remove the identity column all together?
Thanks,
Sam
You need to list of all the needed columns in your query, excluding the identity column.
One more reason why you should never use SELECT *.
INSERT liveTable
(col1, col2, col3)
SELECT col1, col2, col3
FROM dumpTable dt
WHERE NOT EXISTS
(
SELECT 1
FROM liveTable lt
WHERE lt.Id == dt.Id
)
Pro tip: You can also achieve the above by using an OUTER JOIN between the dump and live tables and using WHERE liveTable.col1 = NULL (you will probably need to qualify the column names selected with the dump table alias).
I figured out the issue.... my live table didn't have the ID field set as an identity, somehow when I created it that field wasn't set up correctly.
you can leave that column in your insert statment like this
insert into destination (col2, col3, col4)
select col2, col3 col4 from source
Don't do just
insert into destination
select * from source