Showing posts with label union. Show all posts
Showing posts with label union. Show all posts

Thursday, March 29, 2012

Combining Two Tables Via T-SQL

Hello,

I have two tables that have different column names so I can not combine them using UNION statement. Is there a way to combine two tables and have all the columns from both tables.

Thank you for your help!

UNION does not require that the column names be the same, only that the datatypes are similar enough to combine. See this Example:

Code Snippet


USE Northwind
GO


SELECT CompanyName FROM Customers
UNION
SELECT FirstName + ' ' + LastName FROM Employees

CompanyName
-
Alfreds Futterkiste
Ana Trujillo Emparedados y helados
Andrew Fuller
Anne Dodsworth
Antonio Moreno Taquería
Around the Horn
Berglunds snabbk?p
Bill Smith
...

|||

Donnie:

The column names do not have to be the same for you to union together two tables. If you are trying to union together to tables column-for-column, it is sufficient to have:

The number of columns the same The datatypes of corresponding columns be the same|||

Can you expand on what you are trying to accomplish?. As long as the data type of the columns be the same, including collation, then there is no problem using union or "union all".

declare @.t1 table(c1 int, c2 int)

declare @.t2 table(c3 int, c4 int)

insertinto @.t1 values(1, 2)

insertinto @.t2 values(3, 4)

select c1, c2 from @.t1

union all

select c3, c4 from @.t2

AMB

|||

Are you certain that it is a UNION that you need to perform, and not a JOIN?

A JOIN will allow you to return all columns from both tables as individual columns within the same resultset (i.e. merge the data vertically), like so:

Code Snippet

Table 1 - Sample Data

Column1a Column2a Column3a

--

1 T1C2R1 T1C3R1

2 T1C2R2 T1C3R2

3 T1C2R3 T1C3R3

Table 2 - Sample Data

Column1b Column2b Column3b

--

1 T2C2R1 T2C3R1

2 T2C2R2 T2C3R2

3 T2C2R3 T2C3R3

Output

Column1a Column2a Column3a Column1b Column2b Column3b

--

1 T1C2R1 T1C3R1 1 T2C2R1 T2C3R1

2 T1C2R2 T1C3R2 2 T2C2R2 T2C3R2

3 T1C2R3 T1C3R3 3 T2C2R3 T2C3R3

SELECT t1.Column1a,
t1.Column2a,
t1.Column3a,
t2.Column1b,
t2.Column2b,
t2.Column3b
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.Column1a = t2.Column1b

A UNION will allow you to horizontally merge the data from both tables, like so:

Code Snippet

Table 1 - Sample Data

Column1a Column2a Column3a

--

1 T1C2R1 T1C3R1

2 T1C2R2 T1C3R2

3 T1C2R3 T1C3R3

Table 2 - Sample Data

Column1b Column2b Column3b

--

1 T2C2R1 T2C3R1

2 T2C2R2 T2C3R2

3 T2C2R3 T2C3R3

Output

Column1 Column2 Column3

-

1 T1C2R1 T1C3R1

2 T1C2R2 T1C3R2

3 T1C2R3 T1C3R3

1 T2C2R1 T2C3R1

2 T2C2R2 T2C3R2

3 T2C2R3 T2C3R3

SELECT t1.Column1a AS Column1,
t1.Column2a AS Column2,
t1.Column3a AS Column3

FROM Table1 t1

UNION ALL

SELECT t2.Column1b,
t2.Column2b,
t2.Column3b

FROM Table2 t2

Chris|||

Some kind of join is probably a good idea since I want to join matching rows as well as non matching rows from both tables. Maybe, a full join would be good but I don't want duplicates. Please see my example of the output. What do you think?

Thanks for your help!

Table 1 - Sample Data Column1a Column2a Column3a 1 T1C2R1 T1C3R1 2 T1C2R2 T1C3R2 3 T1C2R3 T1C3R3 5 T1C2R5 T1C3R5 Table 2 - Sample Data Column1b Column2b Column3b 4 T2C2R4 T2C3R4 2 T2C2R2 T2C3R2 3 T2C2R3 T2C3R3 6 T2C2R6 T2C3R6 Ouptput: Column1a Column2a Column3a Column1b Column2b Column3b 1 T1C2R1 T1C3R1 NULL NULL NULL 2 T1C2R2 T1C3R2 2 T2C2R2 T2C3R2 3 T1C2R3 T1C3R3 3 T2C2R3 T2C3R3 NULL NULL NULL 4 T2C2R4 T2C3R4 5 T1C2R5 T1C3R5 NULL NULL NULL NULL NULL NULL 6 T2C2R6 T2C3R6

|||

Yes, a full join should work for you.

SELECT a.Column1a, a.Column2a, a.Column3a,

b.Column1b, b.Column2b, b.Column3b

FROM Table1 a FULL JOIN Table2 b ON (a.Column1a = b.Column1b)

There should not be any duplicates in the result set.

Sunday, March 11, 2012

Columns to Rows - Group By count

I have a table which has many columns but need to query it as rows.
The UNION ALL does the trick
My question is when we do a GROUP BY, is there a way we can generate a
sequence number automatically for columns that are grouped by.
Please see query below.
i.e. If there are many rows for this group
GROUP BY REFERENCE_ID, RECORD_KEY, SEQ_NO
it should start from 1, increment by 1
E.g.
REF1, KEY1, 1 , 1 , EXCEPTION1
REF1, KEY1, 1 , 2 , EXCEPTION2
REF1, KEY1, 1 , 3 , EXCEPTION3
REF1, KEY1, 2 , 1 , EXCEPTION1
REF1, KEY1, 2 , 2 , EXCEPTION2
REF1, KEY1, 3 , 1 , EXCEPTION1
REF1, KEY1, 3 , 2 , EXCEPTION2
REF2, KEY3, 1 , 1 , EXCEPTION1
REF2, KEY3, 2 , 1 , EXCEPTION1
REF2, KEY3, 2 , 2 , EXCEPTION2
REF2, KEY3, 2 , 3 , EXCEPTION3
REF2, KEY3, 3 , 1 , EXCEPTION1
REF2, KEY3, 3 , 2 , EXCEPTION2
Thank you.
Chris
Query:
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, EOB_EXCP FROM EXCEPTIONS WHERE
LTRIM(EOB_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, PRIC_DIS_EXCP FROM EXCEPTIONS
WHERE LTRIM( PRIC_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, LT_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( LT_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, UM_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( UM_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, SE_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( SE_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, PCA_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( PCA_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, CE_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( CE_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, PENALTY_EXCP FROM EXCEPTIONS WHERE
LTRIM( PENALTY_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, RW_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( RW_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, DISC_DIS_EXCP FROM EXCEPTIONS
WHERE LTRIM( DISC_DIS_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, SUP_DISC_EXCP FROM EXCEPTIONS
WHERE LTRIM( SUP_DISC_EXCP) IS NOT NULL
UNION ALL
SELECT REFERENCE_ID, RECORD_KEY, SEQ_NO, SD_DIS_EXCP FROM EXCEPTIONS WHERE
LTRIM( SD_DIS_EXCP) IS NOT NULL
GROUP BY REFERENCE_ID, RECORD_KEY, SEQ_NOHi Chris,
Can you elaborate on this. The DDL, sample, result with how the sequence
should be generated'|||Well I can elaborate:)
This is the same question posed a million times most recently in the thread:
microsoft.public.sqlserver.programming
Top 5 Quesiton
Friday, April 21, 2006 11:39 AM
I'm sure you can now do your thing:)
(Super easy in Rac but I won't steal your thunder:)
www.rac4sql.net
"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
news:B4A92362-AD6F-4DC1-8EDD-4B0CED9E0E2A@.microsoft.com...
> Hi Chris,
> Can you elaborate on this. The DDL, sample, result with how the sequence
> should be generated'|||same question.. different context.. :)
Can you elaborate this
I'm sure you can now do your thing:)
(Super easy in Rac but I won't steal your thunder:)|||Hello,
You may (or may not) want to check out the Help file online @.
www.rac4sql.net/onlinehelp.asp
See:
Summarizing and Displaying Data
Working with Grouped Data
8. Obtaining the Top N Rows Using Summary Value(s)
Better yet post a challenge:)
"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
news:84C0A77E-6BDD-4026-BC53-4A04E2D2D199@.microsoft.com...
> same question.. different context.. :)
> Can you elaborate this
> I'm sure you can now do your thing:)
> (Super easy in Rac but I won't steal your thunder:)
>|||There you go changing your name again - I was just getting used to the
last one. At least this one looks like a real-life name. :P
*mike hodgson*
http://sqlnerd.blogspot.com
Steve Dassin wrote:

>Hello,
>You may (or may not) want to check out the Help file online @.
>www.rac4sql.net/onlinehelp.asp
>See:
>Summarizing and Displaying Data
> Working with Grouped Data
> 8. Obtaining the Top N Rows Using Summary Value(s)
>Better yet post a challenge:)
>"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
>news:84C0A77E-6BDD-4026-BC53-4A04E2D2D199@.microsoft.com...
>
>
>|||All the better to add to your 'Other SQL Nerds' :P
Steve Dassin
http://racster.blogspot.com/
:)
"Mike Hodgson" <e1minst3r@.gmail.com> wrote in message news:%23bTZEAcaGHA.454
8@.TK2MSFTNGP04.phx.gbl...
There you go changing your name again - I was just getting used to the last
one. At least this one looks like a real-life name. :P
mike hodgson
http://sqlnerd.blogspot.com
Steve Dassin wrote:
Hello,
You may (or may not) want to check out the Help file online @.
www.rac4sql.net/onlinehelp.asp
See:
Summarizing and Displaying Data
Working with Grouped Data
8. Obtaining the Top N Rows Using Summary Value(s)
Better yet post a challenge:)
"Omnibuzz" <Omnibuzz@.discussions.microsoft.com> wrote in message
news:84C0A77E-6BDD-4026-BC53-4A04E2D2D199@.microsoft.com...
same question.. different context.. :)
Can you elaborate this
I'm sure you can now do your thing:)
(Super easy in Rac but I won't steal your thunder:)