Importing Data using joins; more than one row returned - sql

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

Related

SQL Server : joining 2 tables where each row has unique PKEY value

I am attempting to join 2 tables with an equivalent amount of columns and where each column is named the same. Tb1 is an MS Access table that I have imported to SQL Server. Tb2 is a table that is updated from tb1 quarterly and is used to generate reports.
I have gone into design view and ensured that all column datatypes are the same and have the same names. Likewise, every row in each table is assigned a unique integer value in a column named PKEY.
What I would like to do is add all new entries present in tb1 (the MS Access table) to the existing tb2. I believe this can be done by writing a query that loads all unique pkeys found in tb1 (AKA load all keys that are NOT found in both tables, only load unique keys belonging to rows in the access table) and then appending these entries into Tb2.
Not really sure where to start when writing this query, I tried something like:
SELECT *
FROM tb1
WHERE PKEY <> Tb2.PKEY
Any help would be greatly appreciated. Thanks!
I would recommend not exists:
select tb1.*
from tb1
where not exists (select 1 from tb2 where tb2.pkey = tb1.pkey);
You can put an insert before this to insert the rows into the second table.
Insert into tb2 Select * from tb1 Where tb1.id not in (select Id from tb2)
The script above inserts records to tb2 from the results the first select query.
The select query only returns records with an ID that is not listed in the select sub query.

Merging tables in SQL Server

I have two tables in SQL Server that I want to merge.
The first table is dbo.bac and has a column counter, the second table is dbo.data and also has a column counter.
I want to load the first table and the second table with all their columns that have the same counter value. Thanks for the help...
You need to use JOIN to join both tables.
SELECT *
FROM dbo.bac A
INNER JOIN dbo.data B ON B.counter = A.counter
this should do it, but you need to filter your records as needed.

Getting way more results than expected in SQL left join query

My code is such:
SELECT COUNT(*)
FROM earned_dollars a
LEFT JOIN product_reference b ON a.product_code = b.product_code
WHERE a.activity_year = '2015'
I'm trying to match two tables based on their product codes. I would expect the same number of results back from this as total records in table a (with a year of 2015). But for some reason I'm getting close to 3 million.
Table a has about 40,000,000 records and table b has 2000. When I run this statement without the join I get 2,500,000 results, so I would expect this even with the left join, but somehow I'm getting 300,000,000. Any ideas? I even refered to the diagram in this post.
it means either your left join is using only part of foreign key, which causes row multiplication, or there are simply duplicate rows in the joined table.
use COUNT(DISTINCT a.product_code)
What is the question are are trying to answer with the tsql?
instead of select count(*) try select a.product_code, b.product_code. That will show you which records match and which don't.
Should also add a where b.product_code is not null. That should exclude the records that don't match.
b is the parent table and a is the child table? try a right join instead.
Or use the table's unique identifier, i.e.
SELECT COUNT(a.earned_dollars_id)
Not sure what your datamodel looks like and how it is structured, but i'm guessing you only care about earned_dollars?
SELECT COUNT(*)
FROM earned_dollars a
WHERE a.activity_year = '2015'
and exists (select 1 from product_reference b ON a.product_code = b.product_code)

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

SQL select and match from multiple tables

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.