join two tables in sql using common column - sql

I have two table.
tablea contains assetID, branchID, latID, lonID. Each row is unique.
assetID, branchID, latID, lonID
For every assetID in tablea, there are 32 entries in tableb in the following format:
assetID, branchID, risk1, risk2, risk3, risk4
I want to randomly select 10 rows from tablea, pull the data from tableb for these random assetID and join them together to get the table in following format
assetID, branchID, latID, lonID, risk1, risk2, risk3, risk4
So far I have the below sql query but I am unable to join the two tables:
select * from tableb where branchID <2 and assetID in
(select top 10 assetID from tablea where assetID is not null and branchID <2)

Does this solve your problem?
select * from (
select * from /*i get the first 10 rows from tablea*/
tablea
where branchID < 2
limit 10
) as tablea
join tableb /*i pull the relative data from tableb */
on tablea.assetID = tableb.assetID and tableb.branchID < 2

Related

Compare the results of a ROW COUNT

I have 2 databases in the same server and I need to compare the registers on each one, since one of the databases is not importing all the information
I was trying to do a ROW count but it's not working
Currently I am doing packages of 100,000 rows approximate, and lookup at them in Excel.
Let's say I want a query that does a count for each ID in TABLE A and then compares the count result VS TABLE B count for each ID, since they are the same ID the count should be the same, and I want that brings me the ID on which there where any mismatch between counts.
--this table will contain the count of occurences of each ID in tableA
declare #TableA_Results table(
ID bigint,
Total bigint
)
insert into #TableA_Results
select ID,count(*) from database1.TableA
group by ID
--this table will contain the count of occurences of each ID in tableB
declare #TableB_Results table(
ID bigint,
Total bigint
);
insert into #TableB_Results
select ID,count(*) from database2.TableB
group by ID
--this table will contain the IDs that doesn't have the same amount in both tables
declare #Discordances table(
ID bigint,
TotalA bigint,
TotalB bigint
)
insert into #Discordances
select TA.ID,TA.Total,TB.Total
from #TableA_Results TA
inner join #TableB_Results TB on TA.ID=TB.ID and TA.Total!=TB.Total
--the final output
select * from #Discordances
The question is vague, but maybe this SQL Code might help nudge you in the right direction.
It grabs the IDs and Counts of each ID from database one, the IDs and counts of IDs from database two, and compares them, listing out all the rows where the counts are DIFFERENT.
WITH DB1Counts AS (
SELECT ID, COUNT(ID) AS CountOfIDs
FROM DatabaseOne.dbo.TableOne
GROUP BY ID
), DB2Counts AS (
SELECT ID, COUNT(ID) AS CountOfIDs
FROM DatabaseTwo.dbo.TableTwo
GROUP BY ID
)
SELECT a.ID, a.CountOfIDs AS DBOneCount, b.CountOfIDs AS DBTwoCount
FROM DB1Counts a
INNER JOIN DB2Counts b ON a.ID = b.ID
WHERE a.CountOfIDs <> b.CountOfIDs
This SQL selects from the specific IDs using the "Database.Schema.Table" notation. So replace "DatabaseOne" and "DatabaseTwo" with the names of your two databases. And of course replace TableOne and TableTwo with the names of your tables (I'm assuming they're the same). This sets up two selects, one for each database, that groups by ID to get the count of each ID. It then joins these two selects on ID, and returns all rows where the counts are different.
You could full outer join two aggregate queries and pull out ids that are either missing in one table, or for which the record count is different:
select coalesce(ta.id, tb.id), ta.cnt, tb.cnt
from
(select id, count(*) cnt from tableA) ta
full outer join (select id, count(*) cnt from tableB) tb
on ta.id = tb.id
where
coalesce(ta.cnt, -1) <> coalesce(tb.cnt, -1)
You seem to want aggregation and a full join:
select coalesce(a.id, b.id) as id, a.cnt, b.cnt
from (select id, count(*) as cnt
from a
group by id
) a full join
(select id, count(*) as cnt
from b
group by id
) b
on a.id = b.id
where coalesce(a.cnt, 0) <> coalesce(b.cnt, 0);

Inner Join between a Table and a View gives Cartesian product of both

When I INNER JOIN a Table with a View, it gives me the cartesian product of both Table and View
I'm using DB2 database. I used INNER JOIN between table and View joining them based on a column which is present in both Table and View
select * FROM MYSCHEMA.TABLE_A a INNER JOIN MYSCHEMA.MY_VIEW_A va on a.PRICE_ID=va.PRICE_ID
My Table is having total of 200 records and View returns 300 records. All PRICE_ID in TABLE_A matches with PRICE_ID column from View. Instead of returning 300 result set, I'm getting 60000 records while joining them.
You would seem to have the same value of PRICE_ID in both tables.
This is easy enough to check for duplicates:
select PRICE_ID, count(*)
from MYSCHEMA.TABLE_A a
group by PRICE_ID
having count(*) > 1
order by count(*) desc;
select PRICE_ID, count(*)
from MYSCHEMA.MY_VIEW_A va
group by PRICE_ID
having count(*) > 1
order by count(*) desc;
The duplicates explain why you are getting so many rows.
select a.*
FROM MYSCHEMA.TABLE_A a
INNER JOIN (select distinct PRICE_ID
MYSCHEMA.MY_VIEW_A) tmp on tmp.PRICE_ID = a.PRICE_ID

How to get the records of table A, which are in one row in table B

SELECT ClaimID, CPTCode FROM TABLEA
ClaimId CPTCode
**60 62000**
**60 0213T**
60 99383
60 93230
60 96372
SELECT cpt1,CPT2 FROM TABLEB
cpt1 CPT2
**62000 0213T**
**62000 0230T**
62000 0216T
62000 0228T
SELECT the record from tableA only that which is at same row in tableB
Result should be
60 62000
60 0213T
I think this does what you want:
select ClaimID, CPTCode
from tablea a
where exists (select 1
from tableb b
where b.cpt1 = a.cptcode
) or
exists (select 1
from tableb b
where b.cpt2 = a.cptcode
);
This query can take advantage of two indexes: tableb(cpt1) and tableb(cpt2).
You can write this as:
select ClaimID, CPTCode
from tablea a
where exists (select 1
from tableb b
where a.cptcode in (b.cpt1, b.cpt2)
);
However, this version is much harder to optimize.
try this-
select obj.ClaimID, obj.CPTCode from (
select row_number() as row_noA, ClaimID, CPTCode FROM TABLEA
join
select row_number() as row_noB, cpt1,CPT2 FROM TABLEB
on TABLEA.CPTCode = TABLEB.CPT2 and TABLEA.row_noA = TABLEB.row_noB
)obj
join two tables and match each row using same row number and get the output

getting top row of joined table

I have 2 tables, tableA and tableB
tableA - id int
name varchar(50)
tableB - id int
fkid int
name varchar(50)
Both tables are joined between id and fkid.
Below are sample rows from tableA
Below is output from tableB
I want to join both tables and get only top row of joined table. So output will be like below
Id Name fkid
1 P1 1
2 P2 4
3 P3 null
Here is Sql fiddle
How can i achieve this with single query? I know that i can loop through in my .net code and retrieve top rows. But i want it in single query.
select a.id,a.name,b.fid from tableA a left join
(
select min(id) fid ,fkid from tableB group by fkid
)b
on a.id = b.fkid
select ta.id, ta.name, min(tb.id) from tableA ta
left join tableB tb on tb.fkid=ta.id
group by ta.id, ta.name
You could do this:
;WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY fkID ORDER BY ID) AS RowNbr,
tableB.*
FROM
tableB
)
SELECT
*
FROM
tableA
LEFT JOIN CTE
ON CTE.fkID=tableA.id
AND CTE.RowNbr=1
Demo here
Or without window function. Like this:
SELECT
*
FROM
tableA
LEFT JOIN
(
SELECT
ROW_NUMBER() OVER(PARTITION BY fkID ORDER BY ID) AS RowNbr,
tableB.*
FROM
tableB
) as tbl
ON tbl.fkID=tableA.id
AND tbl.RowNbr=1
Demo here
Update:
The reason why I choose to do it with row_number is that if there is more columns in tableB then the example. Then there is no need for additional aggregate if you want to show more columns. For me personally it is more clear with an order by on the ID

how to select only 1 unique data when joining two tables, if the table structure is one to many?

i am using an oracle database, i have two tables.
table A
primary key = productid
table B
references productid of table A
primary key = imageid
flow:
each product should have 4 images stored in the table B (mandatory)
problem:
there are some products that has only 2 or sometimes 3 or sometimes 1 image only
despite of the fact that the 4 images rule is mandatory based from code level.
Question:
how to count unique number of products that has images in table b?Because, if I do
select count(*) from tableA join tableB on tableA.productid = tableB.productid
the result is double, because it's a one to many...as in , one product has many images.
So let's say productID = 12345 has 4 images in table B, once I ran my query, the result is 4 , when i want to only get 1...so how?
SELECT Count(DISTINCT TableA.productid)
FROM TableA
JOIN TableB ON TableA.productid = TableB.productid;
Do a sub query with where exists
select count(*) from tableA
where exists (select 1 from tableB where tableA.productid = tableB.productid)
SELECT COUNT(*) FROM
(
select A.productId from tableA A join tableB B on A.productid = B.productid
GROUP BY A.productId
HAVING COUNT(B.imageId) > 1 ) T