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.
Related
I have a tableA with one of the string columns (rawData) and I have a table-valued function (functionA) that functions will accept 'rawData' as an input parameter and returns five new fields. what will that function do? - this will do some string manipulation and split some data into five values
My motive is to select that string column (rawData) and also pass that table-valued function (functionA) and get those five new fields for every row in tableA
Here is the sample:
Select
(Select * from function (rawData))
from tableA
my expectations are I should get every row with that values from functionA along with rawData, but I'm getting below error:
Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.
How do I achieve this?
You would use a CROSS APPLY:
SELECT a.rawData, b.*
FROM TableA a
CROSS APPLY FunctionA(a.rawdata) b
Okay, first off, let's examine why you are getting an error. What is wrong with your syntax?
Well, a subquery in the select clause is expected to return at most one column. Your subquery, by using *, is returning all of the columns from the results of your function call, which appears to be more than one in this case.
Next, what are you trying to accomplish? It looks like you are trying to take the value of a column from tableA and apply functionA to the value in that column for each row returned from tableA.
The proper syntax to accomplish that is by using an apply. There is both cross apply and outer apply. The difference between them is like the difference between an inner join and a left join (or left outer join).
In other words, for the cross apply, if no rows are returned from the call to functionA for a particular row from tableA, then that row from tableA will not be included. For the outer apply, that case would be reversed, as the row from tableA would still be included, just the columns from the function call would be null.
An example query with this syntax based on the query in your question would be the following:
select fa.*
from tableA ta
outer apply functionA(ta.rawData) fa;
Note that I used an outer apply in my example, since your query didn't seem to be expecting rows from tableA with no rows returned from functionA to be excluded.
I am using this query to find similar results in two tables, however some of the data repeat themselves, and I have no idea what might the problem be.
Could you please check this query, to see if there is something I did wrong or not.
All sorts of join(inner,left,right and join alone! returned the same results)
select dbo.netss.[CODE]
,dbo.netss.[NUM]
,dbo.netss.[state]
,dbo.netss.[county]
,dbo.netss.[zone]
,dbo.netss.[Mvillage]
,dbo.netss.[Village]
,dbo.netss.[operator]
,dbo.P1.*
from dbo.P1
inner join
dbo.netss
on (dbo.netss.[state]=dbo.p1.[state])
where dbo.P1.Name=dbo.netss.Village
Joining on [state] is probably not what you want. If the values in the [state] column are not unique in both tables, then you will get multiple matches from dbo.p1 to dbo.netss and possibly from dbo.netss to dbo.p1 also. Add more columns to your join list as follows, for example:
on dbo.netss.[state] = dbo.p1.[state] and dbo.netss.[county] = dbo.p1.[county] and dbo.netss.[CODE] = dbo.netss.[CODE]
In other words, join on the combination of columns that make a row unique and that are also in the other table. This will define a unique key in one table that can be a foreign key to the other table.
--You Can try this
select DISTINCT
dbo.netss.[CODE]
,dbo.netss.[NUM]
,dbo.netss.[state]
,dbo.netss.[county]
,dbo.netss.[zone]
,dbo.netss.[Mvillage]
,dbo.netss.[Village]
,dbo.netss.[operator]
,dbo.P1.*
from dbo.P1
inner join
dbo.netss
on (dbo.netss.[state]=dbo.p1.[state])
where dbo.P1.Name=dbo.netss.Village
----the DISTINCT keywords do eliminate duplicate
I have two identical tables A and B. And both the tables have same fields, as an example Table A (bin, storage, plant) and B (bin, storage, plant). But when I checked the data, table A has 5238 rows and B has 5249 rows. So I dont know which 11 rows are missing. I need help to write a query where I can find those missing rows.
Thanks for the help in advance.
Can use the EXCEPT command for your problem:
SELECT bin
FROM tableB
EXCEPT
SELECT bin
FROM tableA;
Shows all bins which are in tableB but not in tableA.
select *
from tableA
full outer join tableB on tableA.bin = tableB.bin
where tableA.bin is null or tableB.bin is null
SQL-Server allows a full outer join. You can select all records from both table and limit the result to those where the join does not find matches on the other table.
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;
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.