0

I'm trying to create a view of two tables that have many fields in common. enter image description here

The fields that exist in both tables should exist under one field in the new view. I've found some posts here and another I've lost track of that suggested using UNION and FULL OUTER JOIN to do what I want to do, but I'm quite new to this and the answers were a bit confusing. I think I need to select all fields from data and elongthat don't collide, then select the rest from a UNION ALL statement that combines all fields that the tables have in common, but how does the FULL OUTER JOIN come into play?

I think the FULL OUTER JOIN is needed to keep the number of rows in the view the same as the sum of the number of rows from each table, but I'm not quite sure.

Maybe this is my solution? I'm not sure how a CROSS JOIN will work with like fields though.

For those of you concerned with how I'm joining these, I'm trying to take each row from both tables and put them in a new view. My issues result from duplicate fields (c2, footnote1, footnote2, etc). I want them to remain as c2, footnote1, footnote2, but they keep recieving generated aliases. Everything that is a c2 is a c2, no mater which sheet it is from.

Edit: Thanks to the answers below, this is what I'm writing now, and it seems to be what I want. It is taking forever though because of the number of fields; is there a better way to do this? Something more automatic?

Edit2: This is what the complete query looks like, just in case it helps someone in the future. I hope there is a more automatic way to do this, because that took some time. Thank you to all who helped!

SELECT       ID, tableNum, UTC, colNum, c1, c2, null AS c3, c4, colRow, footnote1, footnote2, footnote3, footnote4, 
                                         property, orientation, specimenDesign, exposTempMinF AS exposTempFMin,   exposTempMaxF AS exposTempFMax, 
                                         exposTempRTMinF AS exposTempFRTMin,   exposTempRTMaxF AS exposTempFRTMax, designValueRoomTempF_A, 
                                         designValueRoomTempF_T99                                       , designValueRoomTempF_B                                     , 
                                         designValueRoomTempF_S                                    , designValueRoomTempF_Typical, designValueRoomTempF_Estimate, 
                                         designValueRoomTempF_Units                                         , conversionFactor, null AS form,  null AS thickRangeInMin, null AS thickRangeInMax, 
                                         null AS widthRangeInMin, null AS widthRangeInMax, null AS areaInSqrMin, null AS areaInSqrMax, note
FROM         dbo.[DesignAllowables-Data] AS data 
UNION ALL
SELECT       ID, tableNum, UTC, colNum, null, c2, c3         , null, colRow, footnote1, footnote2, null         , null           , 
                                         property, orientation, specimenDesign, exposTempF_min AS exposTempFMin, exposTempF_max AS exposTempFMax, 
                                         exposTempRTF_min AS exposTempFRTMin, exposTempRTF_max AS exposTempFRTMax, designValueRTF_A AS designValueRoomTempF_A, 
                                         designValueRTF_T99 AS designValueRoomTempF_T99, designValueRTF_B AS designValueRoomTempF_B, 
                                         designValueRTF_S AS designValueRoomTempF_S, null                                                , null                                                , 
                                         designValueRTF_Units AS designValueRoomTempF_Units, conversionFactor, form           , thickRangeInMin              , thickRangeInMax, 
                                         widthRangeInMin           , widthRangeInMax              , areaInSqrMin         , areaInSqrMax             , note
From           dbo.[DesignAllowables-Elong-RA] AS elong
Community
  • 1
  • 1
  • are you joining the 2 tables on ID = ID? – JamieD77 Oct 13 '15 at 21:28
  • It sounds like you're really looking for a union considering that the tables are so similar. – shawnt00 Oct 13 '15 at 21:28
  • 1
    A `CROSS JOIN` is (almost) never the right answer. You are on the right track with the `FULL OUTER JOIN`. Is the data in the "common fields" identical per primary key in both tables? – Tripp Kinetics Oct 13 '15 at 21:28
  • 2
    Make sure you understand the difference between UNION and UNION ALL - It sounds you need UNION ALL (a straight UNION would hide distinct rows) – snow_FFFFFF Oct 13 '15 at 21:31
  • @TrippKinetics yes, most of them. UTC, Col.No., and C* columns are used togeather as a key –  Oct 13 '15 at 22:01
  • @JamieD77 I understand now. no, I don't want to join the tables based on ID = ID. –  Oct 13 '15 at 22:22
  • I want to show all rows, and combine like fields. –  Oct 13 '15 at 22:23

2 Answers2

2

The question you linked to is not applicable to your question.

Here are the some forms of combining to try out:

Full Outer Join

SELECT 
COALESCE(T1.Col1,T2.Col1) As Col1,
COALESCE(T1.Col2,T2.Col2) As Col2
FROM 
T1
FULL OUTER JOIN
T2
ON T1.ID = T2.ID

Matches on ID to combine rows. ID must be unique in both tables or you'll get 'duplicate' rows

Union All

SELECT 
T.ID,
MAX(T.Col1) As Col1,
MAX(T.Col2) As Col2
FROM
(
SELECT Col1,Col2 FROM T1
UNION ALL
SELECT Col1,Col2 FROM T2
) T
GROUP BY T.ID

Effectively matches on ID

This will not introduce duplicates if ID is not unique. It will arbitrarily pick a combination of values

Here's how you just return all the combined rows and selectively combine columns. In this example, C1 only exists in table T1

SELECT C1,C2, C3 
FROM T1
UNION ALL
SELECT NULL, C2,C3 
FROM T2

The important question is: do you have a unique key on each table? Do you need to match on it?

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • By unique key, do you mean unique accross both tables? or unique to a row in one table, and unique to a row in another table? ie, one row with ID = 1 in t1 and one row with ID = 1 in t2? –  Oct 13 '15 at 22:06
  • I like your detailed answer by the way. Can you describe a little about what coalesce does? I think it takes the two separate fields and puts them into one, correct? –  Oct 13 '15 at 22:16
  • I think I want the Full Outer Join, but I want to generate a new set of ID's (becuase ID's are not unique between the tables) and I want any field that is shared between the tables to be pushed into one field of the same name in the new view (which is what coalesce does I think). –  Oct 13 '15 at 22:30
  • 1
    There's doco on COALESCE if you look it up. But basically one or both of those fields might be NULL and it picks the first non null one. You need to clarify how the tables will be put together. If you have `ID=1` in both tables, do you want those rows combined? If so, what of the column values are different? Or do you want to actually see both rows? at this point you need to post sample data and how you want it to be combined. If you have 100 rows in each table with matching ID's, does the result contain 100 rows or 200 rows? – Nick.Mc Oct 13 '15 at 22:44
  • "I want any field that is shared between the tables to be pushed into one field of the same name in the new view". But what about the rows. Do you even need to match on the ID at all? I'll post another case – Nick.Mc Oct 13 '15 at 22:46
  • there is no unique ID between the tables. And this has clarified how I should be asking for help here. If each table has 100 rows, the view should contain 200. –  Oct 13 '15 at 22:59
  • When you say it will arbitrarily pick a combination of values, what do you mean? arbitrary doesn't sound like a good thing for a data set. –  Oct 13 '15 at 23:00
  • Thank you by the way. You've brought me closer to both understanding my problem and finding a solution. –  Oct 13 '15 at 23:02
  • I'm glad you picked up on arbitrary. What I'm really saying is that the rules you want for combining the records are not defined. If you define some rules we can probably implement them and it won't be arbitrary. So the next thing you need to do is post some sample rows and sample output. What _do_ you want to happen when to rows have the same id but different data in columns? produce two rows? produce one row and pick columns from one table only? – Nick.Mc Oct 14 '15 at 00:11
  • 1
    ... oh and I'm glad I improved your understanding. – Nick.Mc Oct 14 '15 at 00:11
0

A UNION ALL basically returns both tables, one after another. JOIN basically returns the columns of both tables. This is an oversimplification, but a union combines rows and a join combines columns.

If both tables.have pretty much the same columns, it sounds like you want a UNION.

Chet
  • 3,461
  • 1
  • 19
  • 24
  • Will like fields be placed in the same field in the new view? ie t1.c2 and t2.c2 both go under newview.c2? or will an alias be generated for the second field? –  Oct 13 '15 at 22:15
  • It goes by order. So if you do `SELECT c1, c2 FROM t1 UNION ALL SELECT c1, c2 FROM t2` they will both be in the same columns, called c1 and c2. – Chet Oct 13 '15 at 22:21
  • Incidentally, every select n the union must have the same number of columns. If one table has more you will need to add some `null` placeholders. – Chet Oct 13 '15 at 22:23
  • What would a `null` place holder look like? only some of these fields are shared between the tables, so i'm going to need to make quite a few of them. –  Oct 13 '15 at 22:32
  • 1
    Let's say t1 had c1, c2, c4, and c5. Table t2 had c1, c2, and c3. Then: `SELECT c1, c2, null c3, c4, c5 FROM t1 UNION ALL SELECT c1, c2, c3, null, null FROM t2` – Chet Oct 13 '15 at 23:15