[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL query question

Posted on 2015-02-12
12
Medium Priority
?
86 Views
Last Modified: 2015-02-17
I ran across a problem I cannot figure out how to solve. I have to consolidate data from 4 temporary tables to one permanent. I am using INNER JOIN for three temp tables and LEFT OUTER JOIN for fourth one. The problem is in data. Some records have duplicate IDs. So when I perform INSERT INTO instead of two records in perm table I got four. This happened so far twice but there is no guarantee that it would not happen again. Any suggestions how to solve it?
0
Comment
Question by:Burzhuin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40606570
Do all four of these tables have the same schema?  If yes, better to do a UNION ALL if you want to make sure that the row counts in 1+2+3+4 = what is inserted into the target_table, and just UNION if you want to eliminate duplicates between 1, 2, 3, and 3.

INSERT INTO target_table(id, a, b, c)
SELECT id, a, b, c
FROM temp_1
UNION ALL
SELECT id, a, b, c
FROM temp_2
UNION ALL
SELECT id, a, b, c
FROM temp_3
UNION ALL
SELECT id, a, b, c
FROM temp_4

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40606723
Without sample data and expected result I would be guessing my way through an answer to this broad question.

Maybe Jim has guessed right?
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40607458
Sample data and table schemas are needed to we can provide a better solution.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Burzhuin
ID: 40607729
My apologies for being not clear. I have four tables. Each tables have only one column common: ProductID. Each temp table  contains only part of data (usually 6 columns) that should contain Perm table. The all tables belong to the same schema. My Insert query looks somewhat like this:

INSERT INTO PermTable (ProductID, value1, value2, value3, value4) SELECT t1.ProductID, t1.Value1, t2.Value2, t3.Value3, t4.Value4 FROM Temp1 t1 INNER JOIN Temp2 t2 ON t1.ProductID = t2.ProductID INNER JOIN Temp3 t3 ON t1.ProductID = t3.ProductID LEFT OUTER JOIN Temp4 t4 ON t1.ProductID = t4.ProductID

It works when ProductID is unique but if there is 2 records with the same ProductID in Temp Tables I will get four records in Perm table.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40607761
If the is a repeated product id in one of the temp tables, what is supposed to happen?

for example, let's say it is

table2
productid value2
12345        12345.6
12345         23456.7

i.e. if there is a difference in value2 (as shown) what would you expect to happen?
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40607774
I wanted to ask a similar question;
- What's a duplicate record? Rows with same ProductID and Value? Or only same ProductID?
- If it's only same ProductID, which value need to be selected? MIN? MAX? AVG? Other?
0
 

Author Comment

by:Burzhuin
ID: 40607821
In my cases I have two records with the same ProductID in Temp1, Temp2 tables. One record in Temp3 and none in Temp4. When I executed my query it inserted into Perm table not two but four records. So Perm table looks like that:

Perm
productid value2
12345        12345.6
12345         23456.7
12345        12345.6
12345         23456.7
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40607831
As been asked above:
i.e. if there is a difference in value2 (as shown) what would you expect to happen?
- If it's only same ProductID, which value need to be selected?
0
 

Author Comment

by:Burzhuin
ID: 40607865
I need both values. But I need TWO records which is correct NOT FOUR records I am getting that is wrong.
0
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40607870
What are the values that you are getting for the rest of the fields? If they are all the same you can use the DISTINCT keyword:
INSERT INTO PermTable (ProductID, value1, value2, value3, value4) 
SELECT DISTINCT t1.ProductID, t1.Value1, t2.Value2, t3.Value3, t4.Value4 
FROM Temp1 t1 
INNER JOIN Temp2 t2 ON t1.ProductID = t2.ProductID 
INNER JOIN Temp3 t3 ON t1.ProductID = t3.ProductID 
LEFT OUTER JOIN Temp4 t4 ON t1.ProductID = t4.ProductID

Open in new window

0
 

Author Comment

by:Burzhuin
ID: 40608474
I tried it but I got only one record but I have to get two.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40608925
Please provide samples for the 4 tables.
and the expected result.

This truly is the fastest way to resolution.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question