SQL select and match from multiple tables - sql

While I have looked at different examples on the internet I am having trouble selecting data from multiple tables. I have 2 tables.
1st has 'logUID', 'groupUID' field.
the other
2nd has 'logUID' , 'User ID' , 'LogEntry' , 'LogTypeUID'
What I am trying to accomplish is to get the log entries where the LogUID match each other and return the groupUID field , so I know which group the log was against. When I have tried select's or joins I seem to be getting one logUID field repeating.
SELECT TOP 10000 [log].[dbo].[Logs].[LogUID], [log].[dbo].[LogGroups].[LogUID], [Weblog].[dbo].[LogGroups].[GroupUID]
FROM [log].[dbo].[LogGroups], [log].[dbo].[Logs] INNER JOIN [log].[dbo].[LogGroups] as LG
ON [log].[dbo].[Logs].[LogUID] = LG.LogUID ;
Any help.

Remove extra FROM [log].[dbo].[LogGroups],

You have the [LogGroups] table in there twice...
FROM table1, table2 means table1 CROSS JOIN table2
So, just change your code to the following...
SELECT TOP 10000
[Logs].[LogUID],
[LogGroups].[LogUID],
[LogGroups].[GroupUID]
FROM
[log].[dbo].[Logs]
INNER JOIN
[log].[dbo].[LogGroups]
ON [Logs].[LogUID] = [LogGroups].LogUID ;

See first of all you need to create a relationship between the tables you have created. After creating a relationship you can either create a key relationship using the two columns. So that whatever data goes in the logUID field of first table, the same data goes in the logUID field of second table. So this would help you to fetch the data from both the tables.
Hope this would help.

Related

Best way to combine two tables, remove duplicates, but keep all other non-duplicate values in SQL

I am looking for the best way to combine two tables in a way that will remove duplicate records based on email with a priority of replacing any duplicates with the values in "Table 2", I have considered full outer join and UNION ALL but Union all will be too large as each table has several 1000 columns. I want to create this combination table as my full reference table and save as a view so I can reference it without always adding a union or something to that effect in my already complex statements. From my understanding, a full outer join will not necessarily remove duplicates. I want to:
a. Create table with ALL columns from both tables (fields that don't apply to records in one table will just have null values)
b. Remove duplicate records from this master table based on email field but only remove the table 1 records and keep the table 2 duplicates as they have the information that I want
c. A left-join will not work as both tables have unique records that I want to retain and I would like all 1000+ columns to be retained from each table
I don't know how feasible this even is but thank you so much for any answers!
If I understand your question correctly you want to join two large tables with thousands of columns that (hopefully) are the same between the two tables using the email column as the join condition and replacing duplicate records between the two tables with the records from Table 2.
I had to do something similar a few days ago so maybe you can modify my query for your purposes:
WITH only_in_table_1 AS(
SELECT *
FROM table_1 A
WHERE NOT EXISTS
(SELECT * FROM table_2 B WHERE B.email_field = A.email_field))
SELECT * FROM table_2
UNION ALL
SELECT * FROM only_in_table_1
If the columns/fields aren't the same between tables you can use a full outer join on only_in_table_1 and table_2
try using a FULL OUTER JOIN between the two tables and then a COALESCE function on each resultset column to determine from which table/column the resultset column is populated

Importing Data using joins; more than one row returned

Im trying to move data from a source table to a Master table. my source table has a category, but no ID. I need to take the category name from the source table, join it to the category table, and insert that ID into my target table.
Here is what I am working with so far (SQL Server):
Select C_CATID, C_CategoryDesc, b.Record_No (using this to differentiate records, as i keep getting duplicates) from [Category_Tree_Lookup]
inner join [dbo].[TBL_C_Mapped SpendData_12_2014OLD03312015] b
on C_categorydescription = b.category
where C_CategoryDesc = b.category
I keep receiving too many records that are duplicates, so i cannot insert into the table.
Any help would be greatly appreciated!
You can use distinct function like this
select distinct C_CATID, C_CategoryDesc, b.Record_No from tablename

SQL Query CREATE TABLE on multiple conditions

I am trying to deduplicate a large table where values are present but broken into several rows.
For example:
Table 1: Client_Code,Account#, First and last names, address.
Table 2: Client_Code,Account#, First and last names, address, TAX_ID.
Now what I want to do may seem pretty obvious at this point.
I want my results to pull from Table 1 into a new table and the query to be "Select From Table 1 where client code and account# from table 1 match client code and account# from table 2." TAble 2 has all values populated, Table 1 has everyone except TAX ID.
The code i tried looked like this.
CREATE TABLE Dedupe_1 AS SELECT * FROM `TABLE 1`
WHERE `TABLE 1`.`Client_Code`=`TABLE 2`.`Client_Code`
AND
WHERE `TABLE 1`.`account#`=`TABLE 2`.`account#`
ORDER BY `TABLE 2`.`account#`
I keep getting a syntax error. I am very new to this programming language so I apologize if this question is hard to understand.
I was just under the impression that I could call to a field from another table by simply using the 'WHERE' statement.
I think you want to use an exists clause:
CREATE TABLE Dedupe_1 AS
SELECT *
FROM `TABLE 1` t1
WHERE EXISTS (select 1
from table2 t2
where t2.Client_Code = t1.Client_Code and t2.`account#` = t1.`account#`
);
You may want to use Join to connect two tables. You can make use of common column among two tables for Join statement. Common syntax goes like
SELECT table1.column1, table2.column 2 and as many you want in common table
FROM table1 name
INNER JOIN table2 name
ON table1.commoncolumn=table2.Common column;
You may learn more about joins here.

How to get names present in both views?

I have a very large view containing 5 million records containing repeated names with each row having unique transaction number. Another view of 9000 records containing unique names is also present. Now I want to retrieve records in first view whose names are present in second view
select * from v1 where name in (select name from v2)
But the query is taking very long to run. Is there any short cut method?
Did you try just using a INNER JOIN. This will return all rows that exist in both tables:
select v1.*
from v1
INNER JOIN v2
on v1.name = v2.name
If you need help learning JOIN syntax, here is a great visual explanation.
You can add the DISTINCT keyword which will remove any duplicate values that the query returns.
use JOIN.
The DISTINCT will allow you to return only unique records from the list since you are joining from the other table and there could be possibilities that a record may have more than one matches on the other table.
SELECT DISTINCT a.*
FROM v1 a
INNER JOIN v2 b
ON a.name = b.name
For faster performance, add an index on column NAME on both tables since you are joining through it.
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Use two DISTINCT statements in SQL

I have combined two different tables together, one side is named DynDom and the other is CATH. I am trying to remove duplicates from that table such as below:
However, if i select distinct Dyndom pdbcode from the table, it returns distinct values of that pdbcode.
and
Based on the pictures above, I commented out the DynDom/CATH columns in the table and ran the query separately for DynDom/CATH and it returned those values accordingly, which is what i need and i was wondering if it's possible for me to use 2 distinct statements to return distinct values of the entire table based on the pdbcode.
Here's my code :
select DISTINCT
cath_dyndom_table_2."DYNDOM_DOMAINID",
cath_dyndom_table_2."DYNDOM_DSTART",
cath_dyndom_table_2."DYNDOM_DEND",
cath_dyndom_table_2."DYNDOM_CONFORMERID",
cath_dyndom_table_2.pdbcode,
cath_dyndom_table_2."DYNDOM_ChainID",
cath_dyndom_table_2.cath_pdbcode,
cath_dyndom_table_2."CATH_BEGIN",
cath_dyndom_table_2."CATH_END"
from
cath_dyndom_table_2
where
pdbcode = '2hun'
order by
cath_dyndom_table_2."DYNDOM_DOMAINID",
cath_dyndom_table_2."DYNDOM_DSTART",
cath_dyndom_table_2."DYNDOM_DEND",
cath_dyndom_table_2.pdbcode,
cath_dyndom_table_2.cath_pdbcode,
cath_dyndom_table_2."CATH_BEGIN",
cath_dyndom_table_2."CATH_END";
In the end, i would like to search domains from DynDom and CATH, based on the pdbcode and return the rows without having duplicate values.
Thank you.
UPDATE :
This is my VIEW table that i have done.
CREATE VIEW cath_dyndom_table AS
SELECT
r.domainid AS "DYNDOM_DOMAINID",
r.DomainStart AS "DYNDOM_DSTART",
r.Domain_End AS "DYNDOM_DEND",
r.ddid AS "DYN_DDID",
r.confid AS "DYNDOM_CONFORMERID",
r.pdbcode,
r.chainid AS "DYNDOM_ChainID",
d.cath_pdbcode,
d.cathbegin AS "CATH_BEGIN",
d.cathend AS "CATH_END"
FROM dyndom_domain_table r
FULL OUTER JOIN cath_domains d ON d.cath_pdbcode::character(4) = r.pdbcode
ORDER BY confid ASC;
What you are getting is the cartesian product of the ´two tables`.
In order to get one line without duplicates you need to have to have a 1-to-1 relation between both tables.
You can see HERE what are cartesian joins and HERE how to avoid them!
It sounds as though you want a UNION of domain name and ranges from each table - this can be achieved like so:
SELECT DYNDOM_DOMAINID, DYNDOM_DSTART, DYNDOM_DEND
FROM DynDom
UNION
SELECT RTRIM(cath_pdbcode), CATH_BEGIN, CATH_END
FROM CATH
This should eliminate exact duplicates (ie. where the domain name, start and end are all identical) but will not eliminate duplicate domain names with different ranges - if these exist you will need to decide how to handle them (retain them as separate entries, combine them with lowest start and highest end, or whatever other option is preferred).
EDIT: Actually, I believe you can get the desired results simply by changing the JOIN ON condition in your view to be:
FULL OUTER JOIN cath_domains d
ON d.cath_pdbcode::character(5) = r.pdbcode || r.chainid AND
r.DomainStart <= d.cathbegin AND
r.Domain_End >= d.cathend