Sql to join two foxpro tables - adodb

I have two foxpro files as detailed below
E:\F1\Table1.dbf {Id, Name, Address, City}
E:\F2\Table2.dbf {Id, qualifcn, marks}
How can I join them to get an ADODB record set with details from both tables?
Thanks and regards
Jojy

Like I have asked other people with similar questions - is this a One-time need or is this an On-going need?
For your general SQL syntax you might want to look at:
Inner and Outer SQL Joins
Especially - 4) Full Outer Join SQL Example
But if this is a one-time need, you can merely:
Manually create a new recipient table with ALL fields
Append Table1 into new table
Set relation to ID into Table2
REPLACE the recipient table's 'extra' fields with the Related Table2 values
After which your new recipient table has ALL field values from both tables.
Good Luck

I KNOW the following has worked with OleDB connection and same principal may work for you. Since both your data components are on the same logical drive, just different paths, you might be able to via common root.
Instead of making a connection to your direct folder where the first data location is, make a connection to the common root path. Then in your query, refer to the RELATIVE PATH to the tables
Connect to E:\
Your query could be
select
T1.*,
T2.*
from
F1\Table1 T1
JOIN F2\Table2 T2
on T1.ID = T2.ID
where
...

Related

updating a table in VFP

There are two tables in my database. I am trying to update a column in table2 by setting it equal to one of the columns in table1. I've already looked at this answer visual foxpro - need to update table from another table
And tried to do this on my code, however, I kept having a syntax error on UPDATE table2. Why?
Here is what I have.
ALTER TABLE table2;
ADD COLUMN base2 B(8,2);
UPDATE table2
WHERE table2.itemid=table1.itemid from table1;
SET table2.base2=table1.base;
The simplest syntax is:
update table2 from table1 where table2.itemid = table1.itemid ;
set table2.base2 = table1.base
You could also add more fields to update separated by commas, i.e.
... set table2.base2 = table1.base, table2.this = table1.that
Using 'standard' VFP language syntax and RELATED Tables, you could quite easily do the following:
USE Table1 IN 0 EXCLUSIVE
SELECT Table1
INDEX ON ID TAG ID && Create Index on ID field
USE Table2 IN 0
SELECT Table2
SET RELATION TO ID INTO Table1
REPLACE ALL Table2.ID WITH Table1.ID FOR !EMPTY(Table2.ID)
You might want to spend some time looking over the free, on-line tutorial videos at: Learn Visual Foxpro # garfieldhudson.com
The videos named:
* Building a Simple Application - Pt. 5
and
* Q&A: Using Related Tables In A Report
Both discuss using VFP's language to work with Related Tables
Good Luck
Use join
Update table2 b
Join table1 a on b. Itemid=a.itemid
Set b. Base2=a.base

How to populate columns of a new hive table from multiple existing tables?

I have created a new table in hive (T1) with columns c1,c2,c3,c4. I want to populate data into this table by querying from other existing tables(T2,T3).
E.g c1 and c2 come from a query run on T2 & the other columns c3 and c4 come from a query run on T3.
Is this possible in hive ? I have done immense research but still am unable to find a solution to this
Didn't something like this work?
create table T1 as
select t2.c1, t2.c2, t3.c3, t3.c4 from (some query against T2) t2 JOIN (some query against T3) t3
Obviously replace JOIN with whatever is needed. I assume some join between T2 and T3 is possible or else you wouldn't be putting their columns alongside each other in T1.
According to the hive documentation, you can use the following syntax to insert data:
INSERT INTO TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement;
Be careful that:
Values must be provided for every column in the table. The standard SQL syntax that allows the user to insert values into only some columns is not yet supported. To mimic the standard SQL, nulls can be provided for columns the user does not wish to assign a value to.
So, I would make a JOIN between the two existing table, and then insert only the needed values in the target table playing around with SELECT. Or maybe creating a temporary table would allow you to have more control over the data. Just remember to handle the problem with NULL, as stated in the official documentation. This is just an idea, I guess there are other ways to achieve what you need, but could be a good place to start from.

Speed Improving Index

I have a fairly basic query such as below that I am needing to execute very often and as fast as possible:
Select
B.ID, B.FirstName, B.LastName
From
TableA as A
Join
TableB as B on A.ID = B.ID
Where
A.OtherID = #Input
So my thought was to create a stored procedure with parameters of #Input. I figure that since the execution plan was saved on the server side this would increase the speed.
I however want to increase it further and thought that maybe an Index might help. But I have not dealt with indexes much just read a little.
What all information would you need to help me build an index that can help?
Would an Index help?
Also this stored procedure is going to be called from Excel 2013 if that makes a difference on something else we can do to speed it up.
We are using SQL Server 2012.
SQL Server Management Studio is remarkably good at recommending indexes. Run your query and see what it says.
Without knowing more about your schema or the OLAP patterns, I can only make a suggestion...
Is "ID" the key field in TableA and/or TableB? If so, they're already indexed.
I'd say you're looking at two indexes:
An index on TableA for OtherID that includes ID. This will help SQL find values of OtherID and return the ID's associated with them.
An index on TableB on ID that includes FirstName and LastName. This will help SQL with the join and save a trip back to the rows for FirstName and LastName.

how to join two tables in oracle on blob column?

how to join two tables in oracle on blob column
when this query is executed "SQL command not properly ended" error message is appearing
select name,photo
from tbl1 join tbl2 on tbl1.photo = tbl2.photo
First, it seems very very odd to have a design where you are storing the same blob in two different tables and very odd that you would want to join on an image. That doesn't seem like a sensible design.
You've tagged this for Oracle 8i. That is an ancient version of Oracle that didn't support the SQL 99 join syntax. You would need to do the join in the where clause instead. You can't directly test for equality between two blob values. But you can use dbms_lob.compare
select name,photo
from tbl1,
tbl2
where dbms_lob.compare(tbl1.photo, tbl2.photo) = 0
This will be rather hideous from a performance perspective. You'll have to compare every photo from tbl1 against every photo from tbl2 and comparing two lobs isn't particularly quick. If you are really intent on comparing images, you are probably better off computing a hash, storing that in a separate column that is indexed, and then comparing the hashes rather than comparing the images directly.
The code:
SELECT
name, photo
FROM
tbl1 T1
INNER JOIN
tbl2 T2
ON
T1.photo = T2.photo
If not running fine, you would have to make few changes in your TABLE structure:
1. ...Add a new TABLE named as IMAGES having columns (image_id, image_blob)
2. ...And then you you would have to change the:
tbl1's blob and tbl2's blob to image_id
3. ...Then perform the JOIN on the basis of COLUMN named as image_id
NOTE: You can not perform GROUP BY, JOIN(any JOIN), CONCAT operations on BLOB datatype
SUGGESTION: save the Paths to images in the DATABASE and save the IMAGES somewhere on that SERVER's Directory (As saving images in BLOB in the DATABASE is not a good practice..... To ensure what i said VISIT HERE)

How to select row data as column in Oracle

I have two tables like bellow shows figures
I need to select records as bellow shown figure. with AH_ID need to join in second table and ATT_ID will be the column header and ATT_DTL_STR_VALUE need to get as that column relevant value
Required output
Sounds like you have an Entity-Attribute-Value data model which relational DBs aren't the best at modeling. You may want to look into a key-value store.
However, as Justin suggested, if you're using 11g you can use th pivot clause as follows:
SELECT *
FROM (
SELECT T1.AH_ID, T1.AH_DESCRIPTION, T2.ATT_ID, T2.ATT_DTL_STR_VALUE
FROM T1
LEFT OUTER JOIN T2 ON T1.AH_ID = T2.AH_ID
)
PIVOT (MAX(ATT_DTL_STR_VALUE) FOR (ATT_ID) IN (1));
This statement requires you to hard-code in ATT_ID however there are ways to do it dynamically. More info can be found here.

Resources