I have a query which looks like this:
select id,name
from table1 as one
where one.duration < (select min(duration) from table2 where one.id = id)
Table2 contains a column called outcome and I would like to display it in the outer query. Table1 and Table2 have no relationship except for the fact that they both contain the column id.
How can I accomplish this?
You need a HAVING Clause including comparison of aggregated values for duration column together with GROUP BY expression :
SELECT t1.id, name, outcome
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
GROUP BY t1.id, name, outcome
HAVING MAX(t1.duration) < MIN(t2.duration)
I have two differents tables. The have some columns in common, for this example lets say 'name' and 'id'.
By making
( SELECT name,id FROM table1
EXCEPT
SELECT name,id FROM table2)
UNION ALL
( SELECT name,id FROM table2
EXCEPT
SELECT name,id FROM table1)
I get a list of the elements that are on one tablet but not in the other one.
Up to this point everything is OK.
But now, I want to make a select all from table1 where the name and the id matches the result of the query above.
After lots of comments I think this is what you're after...
SELECT T1.*
FROM table1 t1
LEFT JOIN table2 t2
on T1.ID = T2.ID
and T1.Name = T2.Name
AND E2.event_Time_UTC between convert(datetime,'2016-02-09 00:00:20',101) and convert(datetime '2016-02-09 23:59:52',101)
WHERE T2.Name is null
AND E1.Event_Time_UTC between convert(datetime,'2016-02-09 00:00:20',101) and convert(datetime,'2016-02-09 23:59:52',101)
You may allow implicit casting to work but above is the explicit approach.
If not then you would need to cast the string dates to a date time, assuming Event_Time_UTC is a date/time datatype.
A left join lets us return all records from the 1st table and only those that match from the 2nd.
The t1.* returns only the columns from table1. The join criteria (on) allows us to identify those records which match so they can then be eliminated in the where clause by 'where t2.name is null' they will always be null when no record match in t2.
Thus you get a result set that is: all records from t1 without a matching record on name and id in table2.
Old version
The below content is no longer relevant, based on comments.
I redacted previous answer a lot because you're using SQL Server not MySQL and I know you want multiple records not table1 and table2 joined.
In the below I create two tables: table1 and table2. I then populate table1 and table2 with some sample data
I then show how to get only those records which exist in one table but not the other; returning a separate ROW for each. I then go into detail as to why I choose this approach vs others. I'll finally review what you've tried and try to explain why I don't think it will work.
create table table1 (
ID int,
name varchar(20),
col1 varchar(20),
col2 varchar(20),
col3 varchar(20));
Create table table2 (
id int,
name varchar(20));
Insert into table1 values (1,'John','col1','col2','col3');
Insert into table1 values (2,'Paul','col1','col2','col3');
Insert into table1 values (3,'George','col1','col2','col3');
Insert into table2 values (1,'John');
Insert into table2 values (4,'Ringo');
Option 1
SELECT T1.name, T1.ID, T1.Col1, T1.Col2, T1.Col3
FROM Table1 T1
LEFT JOIN Table2 T2
on T1.Name = T2.Name
and T1.ID = T2.ID
WHERE T2.ID is null
UNION ALL
SELECT T2.name, T2.ID, NULL, NULL, NULL
FROM Table1 T1
RIGHT JOIN Table2 T2
on T1.Name = T2.Name
and T1.ID = T2.ID
WHERE T1.ID is null ;
which results in...
Notice John isn't there as it's in both tables. We have the other 2 records from table1, and the ID, name from table2 you're after.
Normally I would do this as a full outer join but since I think you want to reuse the name and id fields to relate to BOTH tables in the same column we had to use this approach and spell out all the column names in table 1 and put NULL for each column in table1 when displaying records from table2 in order to make the output of the second query union to the first. We simply can't use *
Option 2: Using a full outer join... with all columns from T1
SELECT T1.*
FROM Table1 T1
FULL OUTER JOIN Table2 T2
on T1.ID = T2.ID
and T1.Name = T2.Name
WHERE (T1.ID is null or T2.ID is null)
you get this... which doesn't show Ringo...
But then I would ask why you need anything from Table 2 at all so I think you're wanting to still show the ID, Name from table2 when it doesn't exist in table1.
Which is why What I think you're after is the results from the 1st query using the union all.
Option 3 I suppose we could avoid the second query in option 1 by doing...
SELECT coalesce(T1.Name, T2.name) as name, coalesce(T1.Id,T2.ID) as ID, T1.col1, T1.Col2, T1.Col3
FROM Table1 T1
FULL OUTER JOIN Table2 T2
on T1.ID = T2.ID
and T1.Name = T2.Name
WHERE (T1.ID is null or T2.ID is null)
which gives us what I believe to be the desired results as well.
This works because we know we only want the name,id from table2 and all the column values in table1 will be blank.
Notice however in all cases we simply can't use Tablename.* to select all records from table1.
This is what you tried:
( SELECT name,id FROM table1
EXCEPT
SELECT name,id FROM table2)
UNION ALL
( SELECT name,id FROM table2
EXCEPT
SELECT name,id FROM table1)
Assuming you want to reuse the ID, Name fields; you can't select *. Why? because the records in Table2 not in table1 aren't in table1. In my example if you want Ringo to show up you have to reference table2! Additionally, * gives you no ability to "Coalesce" the ID and name fields together as I did in option 3 above.
If you ONLY want the columns from table1, that means you will NEVER see data from table2. If you don't need the data from table2, (such as ringo in my example) then why do we need to do the union at all?) I'm assuming you want ringo, thus you HAVE to somewhere reference name, id from table2.
You could also do this with NOT EXISTS:
SELECT *
FROM table1
WHERE
NOT EXISTS
(SELECT 1
FROM table2
WHERE table1.id = table2.id
AND table1.name = table2.name)
;with cte as
(
( SELECT name,id FROM table1
EXCEPT
SELECT name,id FROM table2)
UNION ALL
( SELECT name,id FROM table2
EXCEPT
SELECT name,id FROM table1)
)
Select *
from table1 as tbl1
where
tbl1.id = cte.id
and tbl1.name = cte.name
I'm stuck with a query and I'd need some help.
I need to select values from a table which meets two conditions from other table, for example:
Select * from table1
where ID = (select ID from table2)
AND value = (select value from table2)
So, if I'd need only one value from the table, I could query:
Select * from table1 where ID = (id1) AND value = (value1)
The only solution that I know is using IN, but it wouldn't be the requested solution.
I need something similar to this, but counting that the data returned by table2 is not only one row, but multiple.
Could somebody give me some clue on how to find this?
Thanks.
One method uses exists:
Select *
from table1 t1
where exists (select 1
from table2 t2
where t2.id = t1.id and t2.value = t1.value
);
This is ANSI standard syntax, so it should work in any database. Some databases support this syntax:
select t1.*
from table1 t1
where (t1.id, t1.value) in (select t2.id, t2.value from table2 t2);
I would use AND and OR
select *
from table1
where (ID = (id1) AND value = (value1))
OR (ID = (id2) AND value = (value2))
Each check must be added in brackets. This allows all results matching the pairs ID and VALUE to be returned.
Select * from
table1 inner join table2 on
(table1.ID = table2.ID and
table1.value = table2.value) ;
Since as far as I can understand , you just need to select every rows from table1 where ID and value in table1 equals ID and value in table2. So you just need inner join which joins table1 and table2 checking the condition.
SELECT *
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
Both tables have a date_createField, so when I use select *, date_createField is returned twice. I could solve this by changing my select to:
SELECT T1.date_createField, T2.date_createField
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
But is it possible to not specify the specifik fields (keep select *), and force the table name in front of the property?
I'm having this problem because I'm joining 2 tables with a lot of columns and some columns have the same name. I would like to use select * and still have a distinction between columns present in both tables. Is this possible?
This is not possible you need to specify the column names or accept all columns. If this is a query you will execute often make a view which can be reused.
May this will help you
SELECT T1.*, T2.* FROM Table1 T1 LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
I have two tables with exactly the same structure i.e. same columns. I want to find out number of rows which exist in both tables but do not have exactly the same matching values for all columns. E.g. Table has colums Id, Name, Country. Id is the primary key. If Id=1 exists in both tables then other values should also match. Currently, I am using this kind of statement.
SELECT COUNT(*)
FROM ##Table1 t1 ,
##Table2 t2
WHERE t1.Id = t2.Id
AND ( t1.Name != t2.name
OR t1.Country != t2.Country
)
Table has too many columns hence this is becoming too unwieldly. Is there a better way to do it?
SELECT COUNT(*)
FROM ##Table1 t1
JOIN ##Table2 t2
ON t1.Id = t2.Id
AND EXISTS (SELECT t1.*
EXCEPT
SELECT t2.*)
SQL Fiddle
Perhaps you can concatenate the columns into a single field and check for equality that way?