I have a work table "sheet1" with columns "Article, article_1(1,2,3,4), database_id1". I have ten (or more) tables with database_id (table1, table2, table3...) with four columns :
table1: article_1, database_id1, Information, another_information
Table2: article_2, database_id2, information, another information
I need to make a query like select article, information, another information WHERE article1=database_id1 FROM database_id(NUMBER) some kind of dynamic table.
Related
I have database (couple hundred tables) that all contains a specific column called LastReplicationDate. This column name is always the same in each table, and is always the same value within each table.
Is it possible to write a query that gets the distinct value of this column assigned to each table in my database without having to select each table via a union query?
I have a Destination table with 3 columns, ID, Name, Source.
I have 10+ Source tables, each with multiple columns, but I only require the ID, Name, and the table name itself to be appended into Destination table.
Do note the naming of the column names are different in each table, but the required ID and Name are of the same data type and are of sufficient length for the ID and Name fields.
I already have the query to add the data (see below), and I have no issues doing the first run to add the required data into the destination table, as I just need one query for each table, Here is one my code below
INSERT INTO dest_table
SELECT ID, Name, 'source_table' as Source
FROM source_table
The issue now is that I need to schedule this to run on a daily basis.
I would like the source tables to append their new data into the destination table, and not add all records from each source table into the destination table.
Another condition to consider is that I will still need the data from the destination table to be intact. This means what ever records that were removed from the source tables , will not be removed from the destination table.
Thanks people!
You can exclude the old data by using a WHERE clause as shown below. I am assuming that the ID is unique in this table among all tables else you need to add another column in the destination table to identify where this ID is coming from
INSERT INTO dest_table
SELECT ID, Name, 'source_table' as Source FROM source_table
WHERE NOT EXISTS (SELECT 1 FROM dest_table dt WHERE dt.id = source_table.id)
Another non-optimal approach would be to create a trigger on insert in the source tables and push the data to the destination table.
I have this newly created table in SQL Server with 3 columns ID, Name, Source.
Basically this table will be populated with data from other different tables, each specifically taking in their record IDs and record Names. I believe this can be easily achieved with an INSERT INTO SELECT statement.
I would like to find out on how to populate the Source column. This column is supposed to indicate which table the data came from. For example, Source in table A has 3 records, which I then copied the ID and Name columns from this table, and put it into my destination table.
At the same time, the 3 new records will have their Source column set, indicating it came from Table A. Then I will proceed to do the same for other tables.
You can use the constant string as follows:
INSERT INTO your_table
SELECT id, name, 'TableA' as source
FROM tableA
I have two database tables: TABLE 1 and TABLE 2, both have the same number of columns and have the same columns, the problem is that TABLE 2 has a different order of it columns, So I want to arrange the columns of TABLE 2 like they are arranged in TABL1, but i Dont know how to do that.
I'll be very grateful if you can help me.
Re-create Table2 as create table Table2_Temp as select <sequence of fields like Table1> from Table2. After that re-name tables: Table2 -> Table2_Old (or just drop this one), Table2_Temp -> Table2. This is the simplest way and can be realized in any version of DB.
Change order of columns using non-standard SQL language facilities but if it's possible in using a type of DB.
I want only the unique records in same or new table. And I want to do this with different tables (having duplicate records) in access database through same code.
The flow should be like:
input table ------VBA MODULE------> table with unique records
I am able to do this group by function but for that i have to use field names in query. But field names will differ from table to table.
Please help!
Just use query
SELECT DISTINCT * FROM MyAnyTable