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

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.

Related

How to get only last row after inner join?

I need to return unique funeral_homes who contains not completed leads and sort these by last lead timestamp.
This is my sql query:
select distinct h.funeral_home_name, h.funeral_home_guid, h.address1, h.city, h.state, p.discount_available, t.date_created
from tblFuneralHomes h inner join tblFuneralTransactions t on h.funeral_home_guid = t.funeral_home_guid
inner join vwFuneralHomePricing p on h.funeral_home_guid = p.funeral_home_guid where completed=0 order by 'funeral_home_name' asc;
This is the result, but I need only unique homes with last added lead
What I should change here?
The problem here appears that you are joining into tables with 1 to many relationships with table tblFuneralHomes, yet you expect only one row per funeral home.
Instead of using distinct, I would suggest that instead you group by the required output funeral home columns, and then apply some kind of aggregate on the columns needed from the joined tables in order to return just a single computed value from all possible joined values.
For instance, below we find the first transaction date (min) associated with each funeral home:
select h.funeral_home_name, h.funeral_home_guid, h.address1, h.city, h.state,
p.discount_available, min(t.date_created)
from tblFuneralHomes h
inner join tblFuneralTransactions t on h.funeral_home_guid = t.funeral_home_guid
inner join vwFuneralHomePricing p on h.funeral_home_guid = p.funeral_home_guid
where completed=0
group by h.funeral_home_name, h.funeral_home_guid, h.address1, h.city, h.state,
p.discount_available
order by h.funeral_home_name asc
Note that depending on the cardinality of the association between tblFuneralHomes and vwFuneralHomePricing, you may also need to remove p.discount_available from the grouping and also introduce it with an aggregate function, similar to what I've done with t.date_created

Compare rows two tables

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

Create a table from full outer join query

I have two tables namely :- TDM & AccountMaster. Both are having three equal columns and I have to create a table retrieving all the rows from TDM-table joining the three columns,i.e. FD_BRANCH,FD_CUSTCODE & PRODUCTID.
while creating table through select into clause I get an error
Column names in each table must be unique. Column name 'FD_BRANCH' in table 'acty' is specified more than once.
Please find the following query from which I want to create a table which as per my requirement :-
SELECT * FROM (SELECT FD_BRANCH,FD_CUSTCODE,PRODUCTID FROM TDM
GROUP BY FD_BRANCH,FD_CUSTCODE,PRODUCTID) A full OUTER JOIN AccountMaster B
ON( A.FD_BRANCH=B.FD_BRANCH AND A.FD_CUSTCODE=B.FD_CUSTCODE AND
A.PRODUCTID=B.PRODUCTID)
Change your select to get only the fields you need from one of the 2 tables.
Select A.*
FROM (
or
Select B.FD_BRANCH,
B.FD_CUSTCODE,
B.PRODUCTID
FROM (
FULL OUTER JOIN combines the 2 sets of columns from both queries so you end up with at least 6 columns. Even though they're from different tables or aliases the columns names are the same.

Inner Joining 6 tables? [duplicate]

The following query returns >7000 rows when each table only has 340 rows.
SELECT Config.Spec, TempTable.Spec FROM Confg INNER JOIN TempTable on Config.Spec = TempTable.Spec
Why would this happen? If an INNER JOIN only returns a row if there is a match in both tables then why would it return multiple rows for a match.
If there is more than one row with the same Spec value in TempTable for the same Spec value in Confg, then you will get duplicate rows, and vice versa.
Are the Spec field values non unique? This might explain why the query returns too many results; with duplicates you get get an effective cross product for those.

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;

Resources