My database has a table with columns genus, species, and inatcode. inatcode is blank for every row it hasn't been manually added to. I imported a new table that contains all the codes and want to create an Update query that copies them all to the corresponding rows of the first table. However, because the species column of my first table has additional strings, the match is not perfect and many rows were not updated. table.a.species might look like :
x bimundorum
vesicula (sexgen)
sinuata breviloba
And I want it to match these values in table.b.species:
bimundorum
vesicula
sinuata
I know to use table.a.species LIKE '%table.b.species%' when b is a substring of a, but this is the opposite case and just flipping (shown below) doesn't seem to work. Is there another way to accomplish this in SQLite? The differences between a and b are heterogeneous, but there are only a few cases and I could potentially do multiple queries to account for each.
"UPDATE table.a SET inatcode = table.b.inatcode
FROM table.b
WHERE table.b.genus = table.a.genus
AND table.a.species LIKE '%table.b.species%' "
I am working with SQLite through the DBI package in R, and could make all of this happen in R and reinsert instead. But it seems like this I should be able to do in SQLite.
The modified formatting posted here does what I wanted; I just needed to use the || to concatenate the values and the % operator to make the LIKE function behave as I wanted it to.
"UPDATE table.a SET inatcode = table.b.inatcode
FROM table.b
WHERE table.b.genus = table.a.genus
AND table.a.species LIKE '%' || table.b.species || '%'
AND table.b.species != '' "
Related
I'm using SQLite to deal with tons of data (like 100gb of data).
I need to seach the value of one column in other table in the fastest way possible.
For example, I need to find the following values of Table 1
[COD]
C62
K801
And then find them in Table 2:
[COD_2]
C60-C63
K80-K81
My desired result is something like:
[COD_1] [COD_2]
C62 C60-C63
K801 K80-K81
Since I have a lot of data, it is inefficient to do something like:
SELECT *
FROM TABLE_1, TABLE_2
WHERE COD_1 LIKE '%' || COD_2 || '%';
Instead, I was trying to do this:
SELECT *
FROM TABLE_1
WHERE COD_1 IN (SELECT COD_2 FROM TABLE_2);
Of course that this doesn't result because the codes are not exactly the sames. Is there a way to search for similar values of one column (something like the LIKE operator) in other table by using IN? Or other way that doesn't cross TABLE_1 and TABLE_2?
Thank you!!!
useful to me.
Based on the small data set shown, and my presumed answer to #Shawn's question (K801 is a typo and is meant to be K80 or K81) I assume the following problem description:
Find a row in COD_2 such that the value in COD_1 is between {value1}-{value2} in COD_2; the - being significant and dependable.
I cannot speak to speed, but I would approach it this way:
SELECT value1, value2
from COD_1,COD_2
where value1 between substr(value2,1,instr(value2,'-')-1) and substr(value2,instr(value2,'-')+1)
The thought being: split the value from COD-2 into a "start" and an "end" value.
I've the following simplified SQLLite query:
SELECT SPECIALTABLE.DETAILS AS Details
FROM SPECIALTABLE
INNER JOIN CUSTOMERTABLE
ON CUSTOMERTABLE.CUSCODE = SPECIALTABLE.CUSCODE
INNER JOIN CUSTOMERNAMETABLE
ON CUSTOMERNAMETABLE.NAMECODE = CUSTOMERTABLE.NAMECODE
WHERE NAMECODE LIKE '%' LIMIT 10
SPECIALTABLE does not contain the NAMECODE column.
But when I run the query, it gives the "ambiguous column name" error on NAMECODE. I do not want to specify the table however on namecode (e.g. WHERE CUSTOMERTABLE.NAMECODE LIKE ...).
To my understanding, SQLLite should treat the result of these joins like one big table containing all columns that are contained in each of the tables, so why is there ambiguity?
The problem is there is no column directly linking SPECIALTABLE and CUSTOMERNAMETABLE.
When you write your last line, you do not specify what NAMECODE is in condition. So, the SQL can't catch the NAMECODE correctly, because there is two definitions for them (CUSTOMERNAMETABLE.NAMECODE and CUSTOMERTABLE.NAMECODE)
WHERE NAMECODE LIKE '%' LIMIT 10
Try to Specify the Table that you want the NAMECODE condition, like this:
WHERE CUSTOMERNAMETABLE.NAMECODE LIKE '%' LIMIT 10
Or this:
WHERE CUSTOMERTABLE.NAMECODE LIKE '%' LIMIT 10
I am trying to concatenate 2 columns from oracle apex to fill out one of the column. P3_ID contains text data like 09842 and P3_Type also contains data like 04214. I am trying to concatenate P3_ID +'.'+P3_Type (09842.04214) to put it into p3_total. So I add default value into P3_total to
select concat('P3_ID'+'.'+'P3_Type') from dual;
but It doesn't work for me. Does anyone have an idea to make p3_total to work?
Try
Assuming this is a computation assigned to P3, try
:P3_ID || '.' || :P3_Type
Docs here and here.
I have been looking all day for a solution that works for my situation. I have found some things that are very similar but don't work for my situation, I tried them.
Here is the scenario; I have two table base and partdetails. I have an asp website (internal ONLY) that has drop down lists to select the parameters for a SQL query that fills a data grid view.
My problem is this, I need to be able, based on the drop down list boxes on the page, assign the column name that the criteria that is entered to be searched for.
Here is the query that I am trying to define: (This one returns 0 rows)
sqlCmd.CommandText = ("Select ba.referenceid, ba.partnum, pd.width, pd.length, CONVERT(varchar(12), pd.dateentered, 101) As [dateentered], ba.partqty, ba.status, ba.material From tbl_dlbase ba Join tbl_partdetails pd On ba.referenceid = pd.referenceid Where Case #field1 When 'part #' Then 'ba.partnum' When 'Spacing' Then 'pd.spacing' When 'Surface' Then 'pd.surface' When 'Height' Then 'pd.height' When 'Thickness' Then 'pd.thickness' End Like '%' + #criteria1 + '%'")
sqlCmd.Parameters.AddWithValue("#field1", ddlSc1.SelectedItem.Text)
sqlCmd.Parameters.AddWithValue("#criteria1", txbCriteria1.Text)
This is the latest version of the SQL statement that I have tried. I need to be able to set the field/column name based on the selection from the drop down list ddlsc1 on the asp page.
I have also been trying the queries in Studio manager to see if maybe I have fat fingered something but it also returns 0 rows so I know something is wrong with the query.
So how can I set the column name field using a parameter for the name. I know this is a huge security concern with SQL injection but this is an internal only site, and more importantly my boss said he wants it done with variables.
I don't really see a problem with this other than you have single quotes around your THEN values. Does this fix it?
SELECT ba.referenceid
,ba.partnum
,pd.width
,pd.length
,CONVERT(VARCHAR(12), pd.dateentered, 101) AS [dateentered]
,ba.partqty
,ba.STATUS
,ba.material
FROM tbl_dlbase ba
JOIN tbl_partdetails pd ON ba.referenceid = pd.referenceid
WHERE CASE #field1
WHEN 'part #'
THEN ba.partnum
WHEN 'Spacing'
THEN pd.spacing
WHEN 'Surface'
THEN pd.surface
WHEN 'Height'
THEN pd.height
WHEN 'Thickness'
THEN pd.thickness
END LIKE '%' + #criteria1 + '%'
Sorry in advance due to being new to Rstudio...
There are two parts to this question:
1) I have a large database that has almost 6,000 tables in it. Many of these tables have no data in them. Is there a code using R to only pull a list of tables names that have data in them?
I know how to pull a list of all table names and how to pull specific table data using the code below..
test<-odbcDriverConnect('driver={SQL Server};server=(SERVER);database=(DB_Name);trusted_connection=true')
rest<-sqlQuery(test,'select*from information_schema.tables')
Table1<-sqlFetch(test, "PROPERTY")
Above is the code I use to access the database and tables.
"test" is the connection
"rest" shows the list of 5,803 tables names.. one of which is called "PROPERTY"
"Table1" is simply pulling one of the tables named "PROPERTY".
I am looking to make "rest" only show the data tables that have data in them.
2) My ultimate goal, which leads to the second question, is to create a table that shows a list of every table from this database in column#1 and then column 2,3,4,etc... would include every one of the column headers that is contained in each table. Any idea how do to that?
Thanks so much!
The Tables object below returns a data frame giving all of the tables in the database and how many rows are in each table. As a condition, it requires that any table selected have at least one record. This is probably the fastest way to get your list of non-empty tables. I pulled the query to get that information from https://stackoverflow.com/a/14163881/1017276
My only reservation about that query is that it doesn't give the schema name, and it is possible to have tables with the same name in different schemas. So this is likely only going to work well within one schema at a time.
library(RODBCext)
Tables <-
sqlExecute(
channel = test,
query = "SELECT T.name TableName, I.rows Records
FROM sysobjects t, sysindexes i
WHERE T.xtype = ? AND I.id = T.id AND I.indid IN (0,1) AND I.rows > 0
ORDER BY TableName;",
data = list(xtype = "U"),
fetch = TRUE,
stringsAsFactors = FALSE
)
This next part uses the tables you found above and then gets the column information from each of those tables. Lastly, it makes on single data frame with all of the column names.
Columns <-
lapply(Tables$TableName,
function(x) sqlColumns(test, x))
Columns <- do.call("rbind", Columns)
sqlColumns is a function in RODBC.
sqlExecute is a function in RODBCext that allows for parameterized queries. I tend to use that anytime I need to use quoted strings in a query.