I have two tables t1 and t2.
t1 having 10k records and t2 having 2k records. The 2k records of t2 is present in t1.
I wanted the 8k different records from t1 which is not present in t2.
I'm doing this as below:
select id, second_telphon from t1
except
select id, second_telphon from t2
However, I'm still getting all the 10k records. Is "except" keyword not working?
how can I achieve this?
you can perform a Join to get the unique data from the tables .
like the tables t1 & t2 both you cna perform left or right join .
example:
SELECT T1.*
FROM T1
WHERE NOT EXISTS(SELECT NULL
FROM T2
WHERE T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour)
OR .
SELECT T1.*
FROM T1
LEFT JOIN T2
ON T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour
WHERE T2.ID IS NULL
Try this:
SELECT *
FROM T1
WHERE NOT EXISTS(SELECT id,second_telphon FROM t2)
If ID is a unique value, Try this also:
SELECT *
FROM T1
WHERE ID NOT IN(SELECT ID FROM t2)
You could try a union, followed by an aggregation to restrict to those records in the first table which were not duplicated by the second table:
SELECT id, second_telphon
FROM
(
SELECT id, second_telphon FROM t1
UNION ALL
SELECT id, second_telphon FROM t2
) t
GROUP BY id, second_telphon
HAVING COUNT(*) = 1;
If a record, being defined as an id, second_telphon pair, has a record count of only one after the union, it implies that this record was unique to the first table.
Just do left join
select t1.id,t1.second_telphon from t1
left join t2 on
t1.id = t2.id
and t1.second_telphon =t2.second_telphon
where t2.id is null
Related
I have two table t1 and t2,
Where t1 has the data like:
Id. Name
1. Ab
2. Dc
3. Cd
t2 has the data as given:
Id. Revenue
1. 100
2. 0
3. 200
And my SQL query is:
select t1.id ,t1.name,sum(t2.rev)
from t1
inner join t2 on t1.id= t2.id
where t1.id=100 and t2.Revenue <> 0
group by t1.id
Just put to GROUP BY columns that you want to select:
select t1.id ,t1.name,sum(t2.rev)
from t1
inner join t2 on t1.id= t2.id
where t1.id=100 and t2.Revenue <> 0
group by t1.id, t1.name, t2.rev
The error means that that you've got SUM of t2.rev field - one row, but SQL Server has also many rows with calculated column and it does not know what a row exactly should be chosen.
UPDATE:
If one of your some column has type such as text, ntext or image, then you should cast it to NVARCHAR type:
select t1.id ,t1.name,sum(t2.rev)
from t1
inner join t2 on t1.id= t2.id
where t1.id=100 and t2.Revenue <> 0
group by t1.id, CAST( t1.name AS NVARCHAR(100)), CAST( t2.rev AS NVARCHAR(100))
UPDATE 1:
TEXT, NTEXT and IMAGE are old type of variable and there types are deprecated. So these types be replaced or casted by the corresponding types VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX).
If you have just one column of type of TEXT, then just CAST just this column:
select t1.id ,t1.name,sum(t2.rev)
from t1
inner join t2 on t1.id= t2.id
where t1.id=100 and t2.Revenue <> 0
group by t1.id, CAST( t1.name AS NVARCHAR(100)), t2.rev
If anything, the column causing the error would be t1.name1 Since the name is completely dependent on the ID, you can artificially add it to the group by clause without harming the query's correctness:
select t1.id ,t1.name,sum(t2.rev)
from t1
inner join t2 on t1.id= t2.id
where t1.id=100 and t2.Revenue <> 0
group by t1.id, t1.name
if you want to display any column and you used group by than you must group by one those column also
select t1.id ,t1.name,sum(t2.Revenue) from t1
inner join t2 on t1.id= t2.id where t1.id=1 and t2.Revenue <> 0 group by t1.id,t1.Name
I'm wondering if I can just do an inner join as kind of a where clause by itself. Or if I use a field from the joined table in my where clause, if it's redundant.
select * from T1 inner join T2 on T1.id = T2.id where T2.z is not null
Is the "T2.z is null" part redundant if all I want returned are records in T1 where the same id exists in T2?
For one thing, select * from t1 inner join t2 [...] will not return records in t1 - it will return all the columns of t1 and t2. You could fix that by selecting specifically the columns in t1 - don't select *.
Then, if there are many rows in t2 with the same t2.id, matching a given t1.id, you will get a whole bunch of rows in the result for that one row in the input t1. So you will not always "reduce" the result set.
It seems what you want can be achieved with the in operator, something like
select * from t1 where t1.id in (select id from t2);
This is equivalent to the following modification of your query. You do not need a where clause for this to work:
select t1.* from t1 inner join (select distinct id from t2) b on t1.id = b.id;
In the following query,
select t1.* from T1 inner join T2 on T1.id = T2.id where T2.z is NOT null
The WHERE condition is redundant, assuming that T2.Z is a NOT NULL column.
That would leave you with this:
select t1.* from T1 inner join T2 on T1.id = T2.id
, which is a little odd because, in a normally designed database, either T1.id or T2.id would be the primary key of its table.
If T1.id is the primary key of T1, then your query is going to return duplicates -- each T1 row will be repeated once for each child that exists in T2.
If T2.id is the primary key of T2, then you should not need to join to T2 at all, because every possible T1.id value must exist in T2.id, because of the FOREIGN KEY relationship that (should) exist. In that case, you could have written:
select t1.* from T1 WHERE T1.id is not null;
So, the answer to your question is that you do not need to reference the tables outside of the join condition in order for the join to be applied. But something seems a little off about the approach.
I'm trying to figure out some basic rules in T-SQL.
What I'm trying to achieve here, is to get only the records from Table1 which has a match in Table2 - AND - all records from Table1 where the 'Valid' column has a value of 1 (=true).
Previously I've done this with two selects and a UNION like this:
SELECT T1.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.ID = T2.ID
UNION
SELECT T1.*
FROM Table1 T1
WHERE T1.Valid = 1
But isn't there any other way than using multiple selects and UNION to achieve this?
While fiddling, I did the following code bit, which however only works if there's exactly one match in Table2 (otherwise it'll multiply the records by the number of matches in T2).
SELECT T1.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.ID = T2.ID
OR T1.Valid = 1
What would be the best way to achieve my goal in terms of performance?
Also please don't hold back on the comments, possible flaws, or explanations of how and why another solution might be better.
assuming that T1.ID and T2.ID is unique or a primary key:
If there are duplicates you may have to write SELECT DISTINCT T1.*. The UNION operator in the orinal selects only distinct values.
this one should do:
SELECT T1.*
FROM Table1 T1
WHERE T1.ID IN ( SELECT T2.ID FROM Table2 T2 WHERE T2.ID IS NOT NULL)
OR T1.Valid = 1
or
SELECT T1.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.ID = T2.ID
WHERE T2.ID IS NOT NULL OR T1.Valid = 1
but i think, the execution plan will be the same at the end.
Any method to do this?
Table1
1
2
3
4
5
Table2
3 (with the condition)
4 (without the condition)
I want to:
Select all records from Table1 if it exists in Table 2, where...(condition)
Select all records from Table1 if it not exists in Table2
Combine both select results. Sort all results with their created date.
For example, the result should be:
Result
1
2
3
5
Hopefully this can help.
SELECT t1.* from table1 t1
JOIN table2 t2
ON t1.ID = t2.ID
UNION ALL
SELECT t1.* from table1 t1 where ID in
(
SELECT t2.ID from table1 t1 except Select t2.ID from table2 t2
)
ORDER BY t1.CreatedDate
You can achieve this by doing:
SELECT t1.id
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.id
WHERE condition OR t2.id IS NULL
ORDER BY t1.CreatedDate;
See fiddle (I assumed condition to be t2.id!=4, but it can be anything else depending on other data in your tables).
There could be multiple solution.
One way
we can get the result set using two different queries and at last combine both of the result-set using UNION
Another way,
First statement is saying that get all the result set from TABLE1 if it exists in TABLE2 as well with some criteria (condition in where clause)
means using INNER JOIN we can achieve this
Second statement is saying get all the result set from TABLE1 which are not present in TABLE2
means along with INNER JOIN ed query also include the TABLE1's data if not present in TABLE2
here we can take the help of LEFT OUTER JOIN (taking TABLE1 on the left side)
Assumption: (condition: t1.Id != 4)
Let's try to understand the query using both of the above mentioned ways
---- -- --Step1 Create table and insert records
---- create table1 with Id int identity columsn
--CREATE TABLE Table1 (Id INT IDENTITY(1,1), CreatedDate smalldatetime default(getdate()));
--go
---- insert 1st 5 integers into Table1
--INSERT INTO Table1 DEFAULT VALUES
--go 5
---- create Table2 with Id int column
--CREATE TABLE Table2 (Id INT , CreatedDate smalldatetime default(getdate()));
--go
---- insert records 3,5 into Table2
--INSERT INTO Table2(Id) VALUES (3), (4);
-- -- -- Solution: one way
; WITH cteMyFirstResult AS
(
-- 2.1. Select all records from Table1 if it exists in Table 2, where...(condition)
SELECT
Id, CreatedDate
FROM Table1 AS t1
WHERE t1.Id IN (SELECT Id FROM Table2 AS t2)
AND t1.Id != 4 -- assumption it can be any condition
),cteMySecondResult AS (
-- 2.2. Select all records from Table1 if it not exists in Table2
SELECT
Id, CreatedDate
FROM Table1 AS t1 WHERE t1.Id NOT IN (SELECT Id FROM Table2 AS t2)
)
-- 2.3. Combine both select results. Sort all results with their created date.
SELECT
Id, CreatedDate
FROM cteMyFirstResult
UNION
SELECT
Id, CreatedDate
FROM cteMySecondResult
ORDER BY CreatedDate;
-- -- Solution: Another way (with bug)
SELECT t1.Id, t1.CreatedDate
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.id = t2.id
WHERE t1.Id != 4
Order by T1.CreatedDate;
-- in this query we are using the criteria after doing the join operation.
-- thus after filtering out the result set based on JOIN Condition this condition will get applied
-- and if there is any null record in the Table1 for column Id (used in join) will not come in the final result-set
-- to avoid this we can include NULL check along with our criteria
-- -- Solution: Another way
SELECT t1.Id, t1.CreatedDate
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.id = t2.id
WHERE ( t1.Id != 4 ) OR t1.Id IS NULL -- include all your criteria within small-barcket)
Order by T1.CreatedDate;
Thanks for all responses.
I come out with the answer I want:
SELECT *
FROM Table1 t1
WHERE NOT EXISTS(SELECT 1 FROM Table2 t2
WHERE t1.ID = t2.ID
AND t2.CIF_KEY = #CifKey
AND t2.STATUS <> ''3'')
AND (condition in where clause)
Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);