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.
Related
How to add them together?
Need to be in vb.net
Two value statement as below:
(SELECT SUM(ChildName) FROM Child SA WHERE SA.Name=A.Name AND SA.Health_Status=1 AND SA.Parrent_ID IS NOT NULL) AS Present_CHILD
(SELECT SUM(LATE_COMING_CHILD) FROM LATE_COME SB WHERE SB.Name=A.Name) AS LATE_CHILD
You can use what is referred to as a "scalar subquery":
select (select Name from table1) + (select Name from table2)
In your example, if Table1 and Table2 have a reference that relate them to each other, you can add their fields. The best way is defining a foreign key for one of them, referring to primary key of other table.
For example you can define a new column in Table2 named Table1Id, and rewrite the query as bellow:
SELECT Table1.Name+Table2.Name
FROM Table2
INNER JOIN Table1
ON Table2.Table1Id=Table1.Id
If there is no relation between Table1 and Table2, so there is no meaning for adding fields of these tables.
In the edited situation, the query may be as follows:
SELECT SA.ChildName+' '+SB.LATE_COMING_CHILD AS AllNames
FROM LATE_COME SB
INNER JOIN Child SA
ON SB.ChildId=SA.Id
WHERE
SA.Health_Status=1
AND
SA.Parrent_ID IS NOT NULL
I don't understand why all names of a person must use in one filed!
Additionally I suggest you learn SQL from scratch...
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.
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
I am using the below query to get the customer detail. But it's not working please help me. I am new for SQL.
select cu.fld_cust_id,ord.* from test1 where fld_order_id ord in (select * from tbl_customer cu where cu.fld_status=1);
You can't select columns from subquery used in WHERE clause, becaused they are not joined to this query. You are just using value range returned from this subquery
Your subquery should return only one column here.
You should try something like this.
SELECT cu.fld_cust_id,ord.*
FROM test1
JOIN tbl_customer cu ON cu.fld_status=1 AND fld_order_id = cu.fld_cust_id
Not sure what's in your tbl_customer, but seems like you are matching the fld_order_id to *. You should match with the order_id in your customer table.
select cu.fld_cust_id,ord.*
from test1
where fld_order_id ord in (
select *ORDERID* from tbl_customer cu where cu.fld_status=1
);
In a query you also need to be aware that you can only see the fields in the current scope. So in the main query you only use FROM TEST1, therefore you can only see the fields from that table. ord.* and the use of cu will give an error. If you need other fields from that table use a JOIN. The TEST1 table should contain a foreign key that links to TBL_CUSTOMER, if not you need either a path using other other tables or redesign your database. If you have that foreign key, that's what you use around the IN operator:
select fld_cust_id from test1 where fld_cust_id in (select id from tbl_customer cu where cu.fld_status=1);
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.