Identifying identical pairs across two tables - sql

I have two tables that has the same data. It is collected over two weeks. For example
In table 1 you have
IP|Zip|state
x.x.x.x|abcde|NJ
y.y.y.y|qwert|NY
z.z.z.z|werty|NH
In table 2 you have
IP|Zip|state
x.x.x.x|abcde|NJ
y.y.y.y|qwert|NY
m.m.m.m|werty|NH
z.z.z.z|merty|CA
I am looking to overlap these tables and compare of IP:Zip pairs and then count number of IPs changed zip by state of table 1. Thanks for the help!

You seem to want a join and aggregation:
select t1.state, count(t2.ip)
from table1 t1 left join
table2 t2
on t1.ip = t2.ip and t1.state = t2.state and t1.zip <> t2.zip
group by t1.state;

Related

sql for data validation between two tables

I have two tables (Table1 and Table2) I am comparing. Each table has the same four columns: customerid, channeltreeid, producttreeid and consentstatusid. The customerid is what links each record in the tables, but there are cases where customerid is the same but there are different values in the other column. How can I find all such records (those where customerid is the same but at least one other column differs)?
I am enhancing the answer given by #TheImpaler to output ONLY the rows where there is a difference in one or more of the 3 last columns, as it is so I understand the requirements.
select
t1.customerid,
t1.channeltreeid, t1.producttreeid, t1.consentstatusid,
t2.channeltreeid, t2.producttreeid, t2.consentstatusid,
from table1 t1
join table2 t2 on t1.customerid = t2.customerid
where t1.channeltreeid <> t2.channeltreeid
or t1.producttreeid <> t2.producttreeid
or t1.consentstatusid <> t2.consentstatusid
A simple join will do:
select
t1.customerid,
t1.channeltreeid, t1.producttreeid, t1.consentstatusid,
t2.channeltreeid, t2.producttreeid, t2.consentstatusid,
from table1 t1
join table2 t2 on t1.customerid = t2.customerid
In any case, I really think this is a bad database modelling. You should revise and fix the model.

SQL Multiple INNER JOINS In One Select-Statement

I am using this code for inventory management system, in which i want to retrieve stock in hand from four tables. i have tried with two table and got accurate result as i need it.please help me out.
Table Schema
Productmastertb
prod_id,
Product_name
salesdetailstb
sales_id,
Prod_id,
Prod_qty
estimatedetailstb
est_id,
Prod_id,
Prod_qty
Purchasedetailstb
est_id,
Prod_id,
Prod_qty
Query example (working):
SELECT
productmastertb.prod_id,
productmastertb.prod_name,
sum(estimatedetailstd.prod_qty) as Est_qty
FROM
productmaster
INNER JOIN
estimatedetailstb ON productmastertb.prodid = estimatedetails.prodid
GROUP BY
productmastertb.prod_id, productmastertb.prod_name
Similarly I have to retrieve sum of salesdetailstb.qty and purchasedetailstb.qty
Thanks in advance
You want to summarize across different "dimensions" -- that is tables. One good approach is to aggregate before doing the JOINs. Or to use subqueries. Here is the latter approach:
SELECT pm.prod_id, pm.prod_name,
(SELECT SUM(ed.prod_qty)
FROM estimatedetailstb as ed
WHERE ed.prodid = ed.prodidas
) as Est_qty,
(SELECT SUM(sd.prod_qty)
FROM salesdetailstb as sd
WHERE sd.prodid = pm.prodidas
) as Sales_qty,
(SELECT SUM(pd.prod_qty)
FROM purchasedetailstb as pd
WHERE pd.prodid = pm.prodid
) as Sales_qty
FROM productmaster pm;
This will give you all products, even those missing from one or more of the other tables.
You can add multiple joins.
SELECT t1.id, t4.name, count(t4.name)
FROM Table1 AS t1
INNER JOIN Table2 AS t2 -- the AS statement renames the table within
-- this query to t2. Columns from this table can be used
-- as t2.columnname. This needs to be done when you have
-- columns with the same name in different tables.
ON t1.id = t2.id
INNER JOIN Table3 as t3
ON t1.id = t3.id
INNER JOIN Table4 as t4
ON t3.name = t4.name
GROUP BY t1.id, t4.name

Does increasing the number of fields in JOIN statement increase/decrease the speed of execution?

I have two tables with 3.5 million rows of data. I am creating a left join between the two to create a new view.
Code 1:
SELECT t1.c1,t1.c2,t2.c3,t2.c4
from table1 as t1
left join table2 as t2
on t1.Location=t2.Location and t1.OrderNumber=t2.OrderNumber and t1.Customer=t2.Customer
Code 2:
SELECT t1.c1,t1.c2,t2.c3,t2.c4
from table1 as t1
left join table2 as t2
on t1.OrderNumber=t2.OrderNumber
Both snippets of code give the same desired result as the Order number field in table 2 has only unique values.
Is it better to give more fields to JOIN compared to only one?
SELECT t1.c1,t1.c2,t2.c3,t2.c4
from table1 as t1
left join table2 as t2
on t1.Location = t2.Location
and t1.OrderNumber = t2.OrderNumber
and t1.Customer = t2.Customer
If OrderNumber is the PK of either table then adding additional fields will not change the results and it will not improve performance unless an index as not present on the other side.
If Order number field in table 2 has only unique values it would not change the query. If it is a PK or has a unique constraint/index then addition fields would not help unless what Table2.OrderNumber was joined to was not indexed.

Compare two tables and insert the matching content to another table

I have been working on this problem for 3 months now and gave up once or twice. Yes, I am a novice. I created 3 tables with data. Table 1 has a letter and number. Table 2 has a name, letter and number. Table 3 has the end result. I want to compare the T1 and T2. If the name and number in T1 matches a name and number in table 2. I want the result to in T3 to include name, letter and number. This is what I have so far but it is not working.
SELECT * FROM T1 and SELECT * FROM T2
WHEN
TABLE T1(letter) && TABLE T2(letter)
AND
TABLE T1(number) && TABLE T2(letter)
INSERT INTO TABLE T3 (name,letter,number)
What you need is to do an inner join of the first and second tables based on the attributes name and letter.
SELECT T2.name, T2.letter, T2.number
FROM T1
INNER JOIN T2
ON T1.letter=T2.letter AND T1.number=T2.number;
For more details, you can refer http://www.w3schools.com/sql/sql_join.asp
To expand upon Dinesh's answer,
You'll need an inner join for this. Inner joins gives you rows that match the columns in both tables you've specified.
You can then combine it into an insert statement to put it into your T3 table. So this is one complete SQL statement:
INSERT INTO T3
SELECT T2.name, T2.letter, T2.number
FROM T2
INNER JOIN T1
ON T2.letter = T1.letter
AND T2.number = T1.number;
As a side note, there's also left joins and right joins (and heaps more). Think of Left, Inner, Right joins as two circles in a venn diagram.

SQL SELECT across two tables

I am a little confused as to how to approach this SQL query.
I have two tables (equal number of records), and I would like to return a column with which is the division between the two.
In other words, here is my not-working-correctly query:
SELECT( (SELECT v FROM Table1) / (SELECT DotProduct FROM Table2) );
How would I do this? All I want it a column where each row equals the same row in Table1 divided by the same row in Table2. The resulting table should have the same number of rows, but I am getting something with a lot more rows than the original two tables.
I am at a complete loss. Any advice?
It sounds like you have some kind of key between the two tables. You need an Inner Join:
select t1.v / t2.DotProduct
from Table1 as t1
inner join Table2 as t2
on t1.ForeignKey = t2.PrimaryKey
Should work. Just make sure you watch out for division by zero errors.
You didn't specify the full table structure so I will assume a common ID column to link rows in the tables.
SELECT table1.v/table2.DotProduct
FROM Table1 INNER JOIN Table2
ON (Table1.ID=Table2.ID)
You need to do a JOIN on the tables and divide the columns you want.
SELECT (Table1.v / Table2.DotProduct) FROM Table1 JOIN Table2 ON something
You need to substitue something to tell SQL how to match up the rows:
Something like: Table1.id = Table2.id
In case your fileds are both integers you need to do this to avoid integer math:
select t1.v / (t2.DotProduct*1.00)
from Table1 as t1
inner join Table2 as t2
on t1.ForeignKey = t2.PrimaryKey
If you have multiple values in table2 relating to values in table1 you need to specify which to use -here I chose the largest one.
select t1.v / (max(t2.DotProduct)*1.00)
from Table1 as t1
inner join Table2 as t2
on t1.ForeignKey = t2.PrimaryKey
Group By t1.v