SQL Query CREATE TABLE on multiple conditions - sql

I am trying to deduplicate a large table where values are present but broken into several rows.
For example:
Table 1: Client_Code,Account#, First and last names, address.
Table 2: Client_Code,Account#, First and last names, address, TAX_ID.
Now what I want to do may seem pretty obvious at this point.
I want my results to pull from Table 1 into a new table and the query to be "Select From Table 1 where client code and account# from table 1 match client code and account# from table 2." TAble 2 has all values populated, Table 1 has everyone except TAX ID.
The code i tried looked like this.
CREATE TABLE Dedupe_1 AS SELECT * FROM `TABLE 1`
WHERE `TABLE 1`.`Client_Code`=`TABLE 2`.`Client_Code`
AND
WHERE `TABLE 1`.`account#`=`TABLE 2`.`account#`
ORDER BY `TABLE 2`.`account#`
I keep getting a syntax error. I am very new to this programming language so I apologize if this question is hard to understand.
I was just under the impression that I could call to a field from another table by simply using the 'WHERE' statement.

I think you want to use an exists clause:
CREATE TABLE Dedupe_1 AS
SELECT *
FROM `TABLE 1` t1
WHERE EXISTS (select 1
from table2 t2
where t2.Client_Code = t1.Client_Code and t2.`account#` = t1.`account#`
);

You may want to use Join to connect two tables. You can make use of common column among two tables for Join statement. Common syntax goes like
SELECT table1.column1, table2.column 2 and as many you want in common table
FROM table1 name
INNER JOIN table2 name
ON table1.commoncolumn=table2.Common column;
You may learn more about joins here.

Related

Search table based on infromation from another table

I have created a temporary that has been populated correctly but now I want to search another table based on two fields that are contained within my temporary table. These fields are Forename and Surname. But I want to search for multiple student names and quantities and return specified data! I think the problem will be better explained in the images below:
My Temporary Table
The Table I would like to search (Table2)
Once I have searched each student name I want to be returned with the students Forename, Surname Address, Pin and Score!
Below shows how I have been trying to achieve this without any luck!
Select TempTable.Forname, TempTable.Surmname, Table2.Address, Table2.Pin
from TempTable
Where Exists ( Select * from Table2
where Table2.Forname=TempTable.Forname and
Table2.Surname=TempTable.Surname
)
But it is returning me no results and I don't know why!
If i understand correctly your question, the way to do it is just a simple join:
select TempTable.Forename, TempTable.Surname, Table2.Address, Table2.Pin
from TempTable
inner join Table2 on Table2.Forename = TempTable.Forename and Table2.Surname = TempTable.Surname
Though i recommend you to have a primary key on the "Persons" table (Table2) and use this primary key to reference the records on TepTable
The EXISTS is only used to "filter" result, it's columns aren't available outside the EXISTS.
You need a JOIN!
Select TempTable.Forname, TempTable.Surmname, Table2.Address, Table2.Pin
from TempTable JOIN Table2 ON Table2.Forname=TempTable.Forname and Table2.Surname=TempTable.Surname;
Assuming you really have called the columns "Forname" you just need a simple join. This does it explicitly to keep close to your original:
SELECT TempTable.Forname, TempTable.Surmname, Table2.Address, Table2.Pin
FROM TempTable tt, Table2 t2
WHERE tt.Forename = t2.Forename
AND tt.Surname = t2.Surname;
You could do the same with INNER JOIN.
This is all assuming that student names are unique.

Replacing Not In clause in sql query

I have a temporary table #allocations
which has the following fields
DAllocationId DAllocationName FundCode DSplitTotal DDisabled DistAlloc AAllocationId AAllocationName ASplitTotal ADisabled
I have another table TRAN_POST_PTN which also has these columns along with other columns.
So for asplit id and dsplit id we just have the same column name which is "posting number" in TRAN_POST_PTN.
What i need to do is insert into my allocations table all the rows from TRAN_POST_PTN where the
posting_number is not in (select DAllocationId from #allocations)
and posting_number not in (select AAllocationId from #allocations)
I do not want to use Not in here.
Can some one please suggest me a better way of writing this query.
I tried writing it using union,but that did not work.
There are a couple alternatives to NOT IN. Here's one using NOT EXISTS:
SELECT fields
FROM TRAN_POST_PTN tpp
WHERE NOT EXISTS (
SELECT 1
FROM #allocations a
WHERE tpp.posting_number IN (a.DAllocationId, a.AAllocationId))
Another common way is to use LEFT JOIN with NULL checks, but I believe you'll see a better performance with NOT EXISTS.
SELECT fields
FROM TRAN_POST_PTN tpp
LEFT JOIN #allocations a ON tpp.posting_number = a.DAllocationId
OR tpp.posting_number = a.AAllocationId
WHERE a.DAllocationId IS NULL

SQL Join for cell content, not column name

I read up on SQL Join but as far as I understand it, you can only join tables which have a column name in common.
I have information in two different tables, but the column name is different in each. I need to pull information on something which is only in one of the tables, but also need information from the other. So was looking to join/merge them.
Here is what I mean..
TABLE1:
http://postimg.org/image/hnd63c2f5/
The cell content 18599 in column from_pin_id also pertains to content in another table:
TABLE2:
http://postimg.org/image/apmu26l5z/
My question is how do I merge the two table details so that it recognizes 18599 is referring to the same thing, so that I can pull content on it from other columns in TABLE2?
I've looked through the codes on W3 but cannot find anything to what I need, as mentioned above, it seems to be just for joining tables with a common column:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
You can write as :
select * from table1
where from_pin_id in
(
select from_pin_id
from table1
intersect
select id
from table2
)
Intersect operator selects all elements that belong to both of the sets.
Change the table names and the columns that you select as needed.
SELECT table1.id, table1.owner_user_id, table1.from_pin_id, table2.board_id
FROM table1
JOIN table2 ON table1.from_pin_id = table2.id
GROUP BY id, owner_user_id, from_pin_id, board_id

Selecting data from 2 different databases and comparing them

Forgive me if this question has already been asked, but... I'm trying to select data from 2 different tables in a database and count all the data in one table that is equal to the data in the second table if that makes sense? Below is the code I am trying to use
$select = "SELECT * FROM client_id, clientid, COUNT(client_id) FROM enquiry, check_s WHERE client_id = clientid";
Your query appears to be syntactically wrong.
What you can try is to join the two tables on a primary key(id?) and any other fields you're trying to match. The basic syntax would be like this:
SELECT * FROM
DB1.Table1
JOIN DB2.Table2
ON DB1.Table1.PrimaryKey = DB2.Table2.PrimaryKey;
If you're looking for an exact data match, you may have to join the tables based on all fields(in the ON clause in the above query).
Edit:
Now that you've explained it, you can try this:
SELECT table1.*, count(*) as `n` FROM table1
JOIN table2
ON table1.field = table2.field;
Again, if you need to compare more fields, just include them in the ON clause, and set conditions in the WHERE clause.
I think you might be looking for something like this:
Lets say your main table name is Client and your secondary table (from the form) is Enquiry and the column which you want to compare in client is called client_id and the same column in Enquiry is called clientid.
Then you have
Select Count(Client.*)
From Client, Enquiry
Where Client.client_id = Enquiry.clientid
Group by Client.client_id

What would be the best way to write this query

I have a table in my database that has 1.1MM records. I have another table in my database that has about 2000 records under the field name, "NAME". What I want to do is do a search from Table 1 using the smaller table and pull the records where they match the smaller tables record. For example Table 1 has First Name, Last Name. Table 2 has Name, I want to find every record in Table 1 that contains any of Table 2 Names in either the first name field or the second name field. I tried just making an access query but my computer just froze. Any thoughts would be appreaciated.
have you considered the following:
Select Table1.FirstName, Table1.LastName
from Table1
where EXISTS(Select * from Table2 WHERE Name = Table1.FirstName)
or EXISTS(Select * from Table2 WHERE Name = Table1.LastName)
I have found before that on large tables this might work better than an inner join.
Be sure to create indexes on Table1.first_name, Table1.last_name, and Table2.name. They will dramatically speed up your query.
Edit: For Microsoft Access 2007, see CREATE INDEX.
See above previous notes about indexes, but I believe from your description, you want something like:
select table1.* from table1
inner join
table2 on (table1.first_name = table2.name OR table1.last_name = table2.name);
It should go something like this,
Select Table1.FirstName, Table1.LastName
from Table1
where Table1.FirstName IN (Select Distinct Name from Table2)
or Table1.LastName IN (Select Distinct Name from Table2)
And there are various other ways to run this same query, i would suggest you see execution plan for each of these queries to find out which one is the fastest. In addition creating indexes on the column which is used in a "where" condition will also speed up the query.
i agree with astander. based on my experience, using EXIST instead of IN is a lot faster.