Compare rows two tables - sql-server

I want to compare rows value from one table with rows value from another table. Structure of the first table is :
The other table is more complex than first table and have the same structure but number of rows is million rows, by the way first have 60.000 rows.
I need to compare these two tables by biling_profiles so I have an insight into the price but that billing profile turn in the column. It's easily done using the left outer join because approximately 6 or 7 billing_profiles . Next thing I need is that if the other table has no value (for example, the first table has row with (destination 201425, cost 2,624) and the other does not , then I have trimmed one character from right to left and then search (destination - 20142). If no again result i repeat again trim from right to left (destination - 2014), when i found same destination then show in table)
So, how to solve that ?

try
SELECT DISTINCT A.Id
FROM
table1 A
INNER JOIN table 2B
ON A.Id = B.Id AND A.biling_profiles = B.biling_profiles
or
select t1.biling_profiles,t2.biling_profiles
from table1 t1
inner join table2 t2 on
t1.id= t2.id
where t1.biling_profiles= #biling_profiles

Related

Query for comparing dynamic columns from similar tables

I have two "LINKED SERVER" databases OLDDUMP and LATESTDUMP that have multiple tables. Each given table has the same name with same columns e.g both OLDDUMP and LATESTDUMP have table SIB, and both tables have same columns like (angle, tilt,param1,param2,param3.....param200 etc..). I need to compare each column in these tables (e.g. compare t1.tilt with t2.tilt..etc) and find unmatched records. The code i have works for few columns. But for 200+ columns i do not want to write the condition (where t1.angle<> t2.angle...etc)
SELECT * FROM (
SELECT *
FROM LATESTDUMP...SIB$) t1
FULL OUTER JOIN (
SELECT *
FROM OLDDUMP...SIB$) t2
ON t1.id = t2.id
WHERE
t1.id IS NULL OR
t2.id IS NULL OR
t1.angle<>t2.angle OR
t1.tilt<>t2.tilt...
t1.param1<>t2.param1
t1.param2<>t2.param2
.
t1.param200<>t2.param200
.
etc
The other reason i dont want to write a static query is the columns in table SIB can change in future

display data from two different table which don't have any relation?

table1: in that three columns billno, billamt, wno
table2: in that three column chequeentry, chequeamt, wno
i want display the result which is not repeated rows where wno is equal to same
if from table1 i got four rows and from table2 i got two rows. I want display the rows but not repeated. if i use join its done the multiply and show the result and my table2's rows are repeated. But dont want this. it display the null value it i accepted.
i pass the one query i.e join the table
select b.billno, b.billamt, c.chequeentry, c.chequeamt
from t2 as b
inner join t3 as c on b.wno=c.wno where b.wno=1
i got output but some value are repeated.
Use the Full outer Join, it return both matching, and not matching rows from both tables.
Syntax:
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;

Table recursive relationship-query

The above table has recursive relationship ie. the crfor column has reference to resourceid column.
So the number 1 in the crfor column means resourceid = 1 .How should I change my query to make crforname column (dynamic column) display the proper name Arjun instead of Ashok?
I used the query below to get the result above. You can ignore the other table call PCR in the query
select t1.*,t2.pcrname,t1.ResourceName AS crforName from
Resource t1 left join Pcr t2 on t1.Pcrid=t2.pcrid
where t1.contractid=1
Try adding a self join on the Resource table joining the ResourceId to the CrFor:
select t1.*,t2.pcrname,t3.ResourceName AS crforName from
Resource t1 left join Pcr t2 on t1.Pcrid=t2.pcrid left join Resource t3 on t1.CrFor = t3.ResourceId
where t1.contractid=1

Inner Join and Group By in SQL with out an aggregate function.

I am trying to retrieve data from 2 table with some conditions. When I just do the inner join with the conditions , I get a huge value (200000 data). But when I group by I get a very less value like (8000 data).
SELECT Tcg.SK_tID, Tcg.SK_ServiceProviderID
INTO #CHDetails
FROM #ClientGroup Tcg
INNER JOIN dbo.Component AS chd ON chd.SK_PID = Tcg.SK_PID
AND chd.SK_ServiceProviderID = Tcg.SK_ServiceProviderID
AND chd.SK_CompID = #CHD
AND chd.ReportDate < #ReportDate
GROUP BY Tcg.SK_PID ,Tcg.SK_ServiceProviderID
Can you please let me know the cause for this. Inner join always takes the common data.
The data in the #ClientGroup table is around 70000 , while data in the dbo.Component is very huge. When I query for common PID and Service provider logically it shoul give me the records equal to or less than #ClientGroup. How is it giving more ?
When I do group by i get 8000. But why should I do group by in a inner join for 2 tables.
The group by is essentially performing a distinct on the result. The reason you have to do this is likely because you have duplicates in both tables.
See this sqlfiddle: http://sqlfiddle.com/#!3/cbdca/2
In it, table1 has 3 rows and table2 has 3 rows. When joined together, they return 9 rows.
if a join is giving you more records than expected that means that your join criterea is not complete. The extreme case for this would be a cartesian join.
check your joining criteria.
Group by combines rows together. Usually it is done to produce aggregate data or produce a list of unique values. In your example, the #ClientGroup table is grouped together and the total number of rows you get returned shrinks. You should be getting less rows than are in #ClientGroup.
It will not reduce the number of rows because The INNER JOIN does not perform DISTINCT instead INNER JOIN finds all matching data from both tables.
Let’s say you have table A and table B. both has column ID.
Table A has total 3 rows in it and all values are unique. values are 1,2,3.
TABLE B has total 300 rows, but only 3 unique values. Values are 1,2,3.
(table B has 100 rows for each unique value)
Now in this example if I do INNER JOIN between Table A and Table B
then every row from A will get join Every matching row in table B.
So
value 1 from Table A will match 100 rows in table B
value 2 from Table A will match 100 rows in table B
value 3 from Table A will match 100 rows in table B
so after doing INNER join I will get total 300 rows.
On the contrary if Table B has completely different values (other than 1,2,3) then INNER JOIN would has produced no result.

Merging Two Tables in SQL Server

I have two tables, each with some columns that are the same. However, each table also contains data that is unique. (Similar data includes a row name).
What I need to do is tack on the data from table two to it's matching row in table one (matching the name column).
Is there any way to do this?
I need stuff from table two to go into table 1 where the names match:
The following query should return all matching rows with columns from both tables. Note that any unique rows (that only exist in table one or two) will be excluded.
SELECT
one.matchingColum,
one.oddColum,
two.evenColumn
FROM one
JOIN two on one.matchingColumn = two.matchingColumn
If the data types are the same, then you can do a union
SELECT *
FROM table1
UNION
SELECT *
FROM table2
If the datatypes are not the same and you have a field that you can JOIN on, then you can do a JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

Resources