I have two tables from two different databases that I want to join together but I don't have a column on which this can be easily done by a join.
In table A I have a table with server names
In table B I have a table where one of the columns has a free text field (description).
I want to be able to create a search that searches for the server name within the description column and then add that description column onto the end of table A.
For example:
Table A Table B
name date
server description
customer
Output
name
server
customer
description (join on searching for server name in description)
If you don't have a full text index on description, then you can use like:
SELECT A.name, A.server, A.customer, B.description
FROM A JOIN
B
ON ' ' + B.description + ' ' LIKE '% ' + A.server ' %';
Related
Lets say i have the following sql table named urls:
url
redirect
revenue
realRevenue
clicksGermany
clicksUSA
clicksIndia
gaxktgq
google.com
0.321
69.51
15
28
33
oqjkgf1
example.cn
0.252
1424.3
1202
10
69
gaxktgq
corn.shop
1.242
42525.2
325525
1230
420
Now i want to fetch every column after realRevenue.
In this example you could just fetch by using the names of the columns that come after realRevenue but in my case there are way more fields that come after.
What query do i need?
If the fields you have correspond to the ones presented in your sample input, you should directly select those who are needed by you. If they're more than what we see here, check the next part.
As long as SQLite does not support dynamic queries, you can't create a query in an automatical way.
Although you can retrieve your table interesting fields by accessing the two tables "sqlite_master" and "pragma_table_info", that contains information regarding your table name and table fields respectively. By filtering on the table name and on the field id, you can have a list of all your fields.
SELECT p.name AS column_name
FROM sqlite_master AS m
JOIN pragma_table_info(m.name) AS p ON m.name = 'tab' AND p.cid >= 4
Output:
column_name
clicksGermany
clicksUSA
clicksIndia
But you can also have them prepared to be hardcoded into a SELECT statement, applying a GROUP_CONCAT on the concatenation of the table name and each table field.
SELECT GROUP_CONCAT(m.name || '.' || p.name, ', ') AS fields
FROM sqlite_master AS m
JOIN pragma_table_info(m.name) AS p ON m.name = 'tab' AND p.cid >= 4
fields
tab.clicksGermany, tab.clicksUSA, tab.clicksIndia
Check the demo here.
Note: This solution gets useful if your amount of fields is very big, such that writing all of them by hand becomes a time-consuming task.
I have this customers table (I'm using SQL Server):
About 300 customers were registered in this table. I created another table in another database and inserted these customers into the new database.
Here is the new customers table:
But I have an operation table as well and I didn't change this one. The problem is the foreign key here. Since PhoneNumber is no longer the primary key in Customers table, customerId shouldn't be filled with the phone number anymore. I want to know how can I insert about 1000 operations in the new operation table but use each customer's ID as a foreign key instead of phone number in customerId.
Here is the operations table:
You can use following query to update the old data in operation table:
UPDATE OperationTable AS OT SET CustomerID =
(SELECT ID FROM CustomerTable AS CT WHERE CT.PhoneNumber = OT.CustomerID)
Assuming Counter in the old database is same as CustomerId in new database.
In your previous operations table of the old database, write a query like below
select 'Insert into OperationsTable values(
' + CT.Counter + ',
' + OT.TypeID + ',
' + OT.Amount + ',
' + OT.DateTime + ')' as newquery from OperationTable OT left outer join Customers CT on CT.PhoneNumber=OT.PhoneNumber
This will give you the insert queries which can be copied and run on the new OperationsTable in the new database
Now - if the above assumption is not correct - then -
You will have to write a query on the new customer table to insert the new Id from new customer database into the Counter Column (or a new CID column) of the old customer database, and then repeat the above step.
If we assume that AFDB is the old database and StoreDB is the new one, the following query helped me out:
INSERT INTO StoreDB.dbo.Operations
SELECT StoreDB.dbo.Customers.Id, TypeID, Amount, [DateTime]
FROM AFDB.dbo.Operations JOIN StoreDB.dbo.Customers
ON StoreDb.dbo.Customers.PhoneNumber = AFDB.dbo.Operations.CustomerId;
I'm having an issue copying one table's data to another. I have around 100 or so individual tables that have generally the same field names but not always. I need to be able to copy and map the fields. example: source table is BROWARD and has column names broward_ID, name, dob, address (the list goes on). The temp table I want to copy it to has ID, name, dob, address etc.
I'd like to map the fields like broward_ID = ID, name = name, etc. But many of the other tables are different in column name, so I will have to write a query for each one. Once I figure out the first on, I can do the rest. Also the column in both tables are not in order either..thanks in advance for the TSQL...
With tables:
BROWARD (broward_ID, name, dob, address) /*source*/
TEMP (ID, name, address,dob) /*target*/
If you want to copy information from BROWARD to TEMP then:
INSERT INTO TEMP SELECT broward_ID,NAME,ADDRESS,DOB FROM BROWARD --check that the order of columns in select represents the order in the target table
If you want only copy values of broward_ID and name then:
INSERT INTO TEMP(ID, name) SELECT broward_ID,NAME FROM BROWARD
Your question will resolve using update
Let's consider we have two different table
Table A
Id Name
1 abc
2 cde
Table B
Id Name
1
2
In above case want to insert Table A Name column data into Table B Name column
update B inner join on B.Id = A.Id set B.Name = A.Name where ...
I have a DB Table [List1], 2 columns, Name, Number
SQLFiddle
I have an excel spreadsheet with 2 columns,names and numbers.
I want to match the Names in Excel to the Names column in SQL and If a match is found insert the relevant number in the second column.
Something tells me I will need to build an array / or csv and run some Tsql to achieve this.
I originally used the Task> Import data to build the DB Table.
Will importing the data again just overwrite the existing data?
What is the most efficient way to import the info, but update existing numbers? [EDIT, I have made some progress, read on]
I have managed to Create an conditional insert:
SET #PersonName = 'Andy
insert into People (Name, Number)
select
#PersonName
where not exists (
select * from People where Name = #PersonName
);
How do I pump the name list into the #PersonName variable and loop through the command in SQL?
Update:
I want to update the Datasets based on a dual column First/Last name.
Will this Work?
Update : Yes it worked, final code below.
update p
set p.number = s.numbers
from People p
join dbo.[spreadsheet] s on p.Firstname = s.Firstname AND p.lastname = s.lastname
If I understood you correctly and you want to match relatively small amount of data (up to 2k-5k rows) between excel and database table you may perform the next sequence of actions:
In SSMS execute: create table dbo.[spreadsheet] (firstname nvarchar(100), lastname nvarchar(100), numbers int);
In Excel spreadsheet copy to buffer data from firstname, lastname and numbers columns (without headers)
In SSMS Object Explorer: Tables->Right click->Refresh
Select dbo.spreadsheet table->Right click->Edit top 200 rows
In the designer select last row->Right click the on row header->Paste
And finally execute following update statement (see below)
update p
set p.number = s.numbers
from People p
join dbo.[spreadsheet] s on s.firstname = p.firstname and s.lastname = p.lastname
I have a legacy table with all column named in an old way, the names don't make sense to others, but the table description contains column description, how to can select all data from the table, and combine with the column description?
UPDATED BELOW:
To get the Names and Columns Description
SELECT
COLUMN_NAME AS Name,
COLUMN_TEXT AS Description
FROM
[DB2-LINKED-SERVER].[BD2].QSYS2.SYSCOLUMNS
WHERE
TABLE_NAME = 'ITMHED'
I got:
Name Description
ITMNO Item Number
ITMNM Item Name
.... 800+ rows more
Then I have another query:
SELECT * FROM [DB2-LINKED-SERVER].[BD2].ITMHED
It returned me:
ITMNO ITMNM ...800+ more columns
AB-001 Mountain Bike ....
What I want to get:
Item Number Item Name ...800+ more columns
AB-001 Mountain Bike .....
If I need only 2-3 column, I can manually rename them, but with that many record, I want to make it more readable for users. I need to generate a report from that.
SELECT
COLUMN_NAME AS Name + ' as '+
COLUMN_TEXT AS Description + ','
FROM
[DB2-LINKED-SERVER].[BD2].QSYS2.SYSCOLUMNS
WHERE
TABLE_NAME = 'ITMHED'
Could get the output from that and then insert it into the following:
select (insert the output from above here) from [DB2-LINKED-SERVER].[BD2].ITMHED