sql left join with distinct correct? - sql-server-2008

I'm trying to do a join between tables 1 and 2 which have a 1 to many relationship.
table1 has the following fields
createdate
contact
tkey (surrogate key)
table2 has the following fields
tkey (primary key)
status
userfld1
description
I want to show all items in table2 with their corresponding items in table1 grouped by table2.userfld1
select distinct t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
from table2 as t2 left join table1 as t1
on t2.tkey = t1.tkey
group by t2.userfld1
is this correct?

No that's not correct, you can't select columns that aren't in the group by unless they are contained in an aggregate function. And I think what you are asking for doesn't even make sense. My best guess is that you mean ORDER BY, not GROUP BY:
SELECT DISTINCT t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
FROM table2 t2
LEFT JOIN table1 t1
ON t2.tkey = t1.tkey
ORDER BY t2.userfld1
Three other errors that I've fixed:
SELECT ... FROM not SELECT ... WHERE
You should join with a table, not a column.
You had no aliases after the table names, but later refer to these missing aliases.

I think what you're looking for is order by, not group by, and I also fixed your query:
select t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
where table2 t2 left join table1 t1
on t2.tkey = t1.tkey
order by t2.userfld1
Is this what you were looking for?

Related

More rows after Left Join in MySQL

I've two tables, table1 contains 22780 rows. Now I left join table1 with table2 (which doesn't contain any duplicates) and I get 23588 rows.
SELECT * FROM Table1
left join Tabelle6 ON CAST(Table1.Customer AS Int) = table2.Customer
Why do I get more rows now? I only need every row from table1 once.
Edit: found my issue, table 2 does contain duplicates. But is there any way to join every row only once and ignore any further matches?
As the comment suggests, the easiest way to handle this would probably be to do SELECT DISTINCT to remove duplicates from your result set:
SELECT DISTINCT
t1.col1,
t1.col2,
t1.Customer,
...
FROM Table1 t1
LEFT JOIN Table2 t2
ON CAST(t1.Customer AS Int) = t2.Customer
But there is another option here. We could also join to a subquery which removes duplicate customers. This would ensure that no record from the first table gets duplicated from matching to more than one record in the second table.
SELECT *
FROM Table1 t1
LEFT JOIN
(
SELECT DISTINCT Customer
FROM Table2
) t2
ON CAST(t1.Customer AS Int) = t2.Customer

Why does group by and sort by give 0 response

The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

Looking for an alternative SQL command to find the same values in two tables

I am using this command to find the same values in two tables when the tables have 100-200 records. But When the tables have 100000-20000 records, the sql manager, browsers, shortly the computer is freesing.
Is there any alternative command for this?
SELECT
distinct
names
FROM
table1
WHERE
names in (SELECT names FROM table2)
Try with join
SELECT distinct t1.names
FROM table1 t1
join table2 t2 on t2.names = t1.names
Use EXISTS:
SELECT distinct t1.names
FROM Table1 t1
WHERE EXISTS(
SELECT 1 FROM tabl2 t2 WHERE t2.names=t1.names
)
SELECT DISTINCT t1.names
FROM table1 t1
INNER JOIN table2 t2 on t1.names=t2.names
The use of the INNER JOIN ensures that there are only exact matches returned from both tables. It should be relatively quick, but indexes may be required over the long term, especially if you're using them for other JOINs and GROUP BYs etc.
a simple join will also do it.
make sure the column is indexed.
select distinct t1.names
from table1 t1, table2 t2
where t1.names = t2.names
Show names from both tables where there is a match
SELECT names
FROM table1
UNION ALL
SELECT names
FROM table2
This query will return duplicated values if there are any. If you only want distinct values then try this but note that there will be an impact on performance
SELECT names
FROM table1
UNION
SELECT names
FROM table2
SELECT table1.names
FROM table1
INNER JOIn table2
ON table1.names = table2.names
Group By table1.names

SQL - joining data

Is it possible to get 1 result where I require data from 3 tables.
First table: I will need to grab all the fields (1 row found by a primary key)
Second table: I will need to grab the field 'username' (connected to first table by 'master_id')
Third table: I will need to grab the latest added row with the associated master_id key (table has 'date', 'master_id', 'previous_name').
select top 1 first.*, second.username, third.*
from first
inner join second on first.id = second.master_id
inner join third on first.id = third.master_id
order by
third.date desc
As always there are dozens of ways to skin a cat, I'm not sure if this is optimized as the subquery methods, but it should work.
You can join the three tables together. Then, you can use a "filter" join to keep only the latest Table3 row:
select *
from Table1 t1
join Table2 t2
on t2.master_id = t1.master_id
join Table3 t3
on t3.master_id = t1.master_id
join (
select master_id
, max(date) as max_date
from Table3
group by
master_id
) as filter
on t3.master_id = filter.master_id
and t3.date = filter.max_date
You'll need a correlated subquery for that third table.
SELECT t1.*, username, date, previous_name
FROM FirstTable t1
INNER JOIN SecondTable t2 ON t1.master_id=t2.master_id
INNER JOIN
(SELECT master_id, date, previous_name
FROM ThirdTable AS t3_1
WHERE date = (
SELECT MAX(date)
FROM ThirdTable AS t3_2
WHERE t3_2.master_id=t3_1.master_id)) q1 ON q1.master_id=t1.master_id;
NOTE: Untested.