my first table has about 18K records
so when i
select * from table2 i get about 18k
i'm trying to do a join on it as follows, but i'm getting like 26K back.. what am i doing wrong? i though it's supposed to return all of the "right" aka table2 records plus show me whatever value matches from the first in a separate column...
Select t1.fID , t2.*
FROM table1 t1 right join table2 t2 on t1.fName = t2.f
here is an exmaple of my tables:
table 1:
fID, fName
table 2: id, f, address, etc
i need to get all records from table 2, with an fID column, whenever f=fName
table1 has many rows with a value of fname that matches the same in table2.
Example, say 5k rows table2 have no matching rows in table1, you have a average of 2 rows in table 1 for each of the remaining 13k table2 rows
Because you have also asked for a column for table1, this will happen. You'll note multiple t1.fId values for a given t2.fname. Or NULLs
If t1.fName and t2.f aren't unique identifiers for their tables, you will find that rows from table1 are being joined with multiple rows from table2.
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).See Right Join
So it looks like you do not have your matching criteria set correctly or you have no matches.
This is possible when some fName values are repeated in Table2 and/or Table 1.
Run these Queries and See:
SELECT fName, COUNT(1) FROM Table2 GROUP BY fName HAVING COUNT(1) > 1
SELECT fName, COUNT(1) FROM Table1 GROUP BY fName HAVING COUNT(1) > 1
Related
I have a table that looks like below. It is created using a query -
NPI Other_Columns
123 Several_Other_Columns
456 Several_Other_Columns
How do I take every NPI from this table and get a count of the number of times they appeared in another table? The structure of the other table is like so -
Claim_id NPI1 NPI2 NPI3 NPI4 NPI5 NPI6 NPI7 NPI8
If NPIs in the first table, show in any field in the second table, we want to count that claim.s
The first task is the join
SELECT
t1.npi,
t1.other_columns,
t2.claim_id
FROM table1 as t1
JOIN table2 as t2 ON t1.npi in (t2.np1,t2.np2,t2.np3,t2.np4,t2.np5,t2.np6,t2.np7,t2.np8)
that gets you all the things joined.
Now count those..
SELECT
count(t2.claim_id)
FROM table1 as t1
JOIN table2 as t2 ON t1.npi in (t2.np1,t2.np2,t2.np3,t2.np4,t2.np5,t2.np6,t2.np7,t2.np8)
I have two tables and both contains columns Names and ID_Number.
table1 contains columns Names, ID_Number, Price_date
table2 contains columns Names, ID_Number, historical_date, comments
I am trying to do a loop such that it will start from the first value in ID_Number column in table1 and see if it matches with any value in ID_number column in table2.
If there is a match, then compare the 'Names' for the two tables for that particular ID_number. If the names does not matched, then in the comments column, enter the Name from table1 and enter the Price_date from table1 to historical_date in table2.
Don't use loops in SQL, as long as you can avoid them. SQL is a set-based language, that is not optimized for iterative processes.
From your explanation, it seems like you want an update statement with a join. This should do what you want:
update t2
set t2.comments = t1.names, t2.historical_date = t1.price_date
from table2 t2
inner join table1 t1
on t1.id_number = t2.id_number
and t1.names <> t2.names
I have two tables joined up together
I need to see if Product Code from table 1 (On the left) matches with the on on the right, based on their barcodes.. I have managed to inner join the barcodes and show only barcodes that match up. Just the prdouct code is the problem now...
Is this what you want?
select t1.*
from table1 t1
where not exists (select 1
from table2 t2
where t2.code = t1.product_code and t2.barcode = t1.barcode
);
This selects rows from table1 with no matches in table2.
I have two tables table1 and table2. Both tables have a common column named city.
How do I find all values under city which are in both the tables ?
You can do an inner join on the city column, to find values that exist in both tables.
select
-- Output the city from either table (since it will be the same)
t1.city
from
-- Join table1 and table2 together, on a matching city column
table1 t1 join table2 t2 on (t1.city=t2.city)
group by
-- Only return a single row per city
t1.city
SELECT tbone.desired_column1
tbone.desired_column2,
--other columns from table one
tbtwo.desired_column1,
tbtwo.desired_column2
--other columns from table two
-- Bellow we're stating what this table could be identified as (tbone and tbtwo), so that you don't have to keep typing table name above and bellow. Can be anything, such as A or B or HORSECORRECTINGBATTERY
FROM table1 tbone,
table2 tbtwo
WHERE tbone.city = tbtwo.city
If you don't want to specify which columns to take, just go with
SELECT * FROM ...
SO I have 2 tables and would like to SUM the total of a column in one table where 2 other columns match in another table.
In table1 I have acc_ref and bill_no.
acc_ref is different but bill_no could be 1-10 (so 2 or more acc_ref could have the same bill_no)
In table2 I have acc_ref, bill_no and tran_amnt.
Εach acc_ref has multiple rows and I want to SUM the tran_amnt but only if acc_ref and bill_no both match in table1.
I tried this but I get an error
'The columns in the SELECT clause must be contained in the GROUP BY
clause'
select a.acc_ref, a.bill_no
from table1 a
where exists (select acc_ref, bill_no, SUM (tran_amount)
from table2 b
where a.acc_ref = b.acc_ref
and a.bill_no = b.bill_no
group by acc_ref)
Apologies if this is very basic and obvious but I'm struggling!!
In you description it seems like table1 does not contain any useful information. Because both columns you give also exist in table2. So if nothing else from table1 is needed, you could just remove table1 from the query). Still with your problem you should do a simple join
SELECT a.acc_ref, a.bill_no, SUM(b.tran_amount)
FROM table1 a
JOIN table2 b ON b.acc_ref = a.acc_ref AND b.bill_no=a.bill_no
GROUP BY a.acc_ref, a.bill_no
I believe you should use case:
Sample:
SELECT
TABEL1.Id,
CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
THEN 'TRUE'
ELSE 'FALSE'
END AS NewFiled
FROM TABLE1