March 7, 2007 at 2:54 am
Take 2 tables
Table1 | |
id | Name |
1 | AAA |
2 | AAA |
3 | AAA |
Table2 | |
id | Name |
null | BBB |
null | BBB |
null | BBB |
The final output should be
id | Name |
1 | AAA |
2 | AAA |
3 | AAA |
4 | BBB |
5 | BBB |
6 | BBB |
Good luck,
Sandeep
March 7, 2007 at 3:56 am
Sandeep,
check out with this Query.
Insert into Table3 (Name)
Select Name from Table1
Insert into Table3 (Name)
Select Name from Table2
Best Regards
Senthil Kumar
March 7, 2007 at 4:45 am
set id column's identity property "yes" in table1
insert into table1 (Name)
select name from table2
March 7, 2007 at 5:05 am
CREATE TABLE #Output (ID INT NOT NULL IDENTITY(1,1), NAME VARCHAR(50))
INSERT INTO #Output
SELECT Name FROM Table1
UNION ALL
SELECT Name FROM Table2
SELECT * FROM #Output
DROP TABLE #Output
Prasad Bhogadi
www.inforaise.com
March 7, 2007 at 8:56 am
This is more of an ordering issue on a union than a puzzle. There doesn't appear to be a relationship to the tables. Is there some reason you chose this as a puzzle?
March 8, 2007 at 7:53 am
Hi ,
Try this script . it will solve ur problem..
select * from tab_a
union
select distinct j.* from (
select distinct z.b,k.name from(
select * from (
select a.i_d,count(*) b from (
select * from tab_a a
union all
select * from tab_b b)a
cross join
(select * from (
select * from tab_a a
union all
select * from tab_b b)a)b
where isnull(a.i_d,'') >= isnull(b.i_d,'')
group by a.i_d)x
where x.i_d is not null)z
cross join
(select * from (
select * from tab_a a
union all
select * from tab_b b)k)k)j
join
tab_b w
on j.name=w.name
Regards ,
Amit Gupta
March 12, 2007 at 7:56 pm
Looks more like homework to me...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 13, 2016 at 1:11 am
SELECT ROW_NUMBER() OVER (ORDER BY NAME) Id , Name FROM
(
SELECT Id,Name FROM [dbo].[Table1]
UNION ALL
SELECT Id,Name FROM [dbo].[Table2]
)r
Pawan Khowal
_________________________________________________________________________________________________________
https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/
Regards,
Pawan Kumar Khowal
MSBISkills.com
June 14, 2016 at 12:13 am
Pawan Kumar Khowal (6/13/2016)
SELECT ROW_NUMBER() OVER (ORDER BY NAME) Id , Name FROM(
SELECT Id,Name FROM [dbo].[Table1]
UNION ALL
SELECT Id,Name FROM [dbo].[Table2]
)r
Pawan Khowal
_________________________________________________________________________________________________________
https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/
1. SQL 2000
2. Topic from 2007.
_____________
Code for TallyGenerator
July 2, 2016 at 10:36 am
I also tried something similar..............:-P
select ROW_NUMBER() over(order by id) , name From Table1
union
select ROW_NUMBER() over(order by id) +3, name From Table2
July 2, 2016 at 11:45 am
ammit.it2006 (7/2/2016)
I also tried something similar..............:-Pselect ROW_NUMBER() over(order by id) , name From Table1
union
select ROW_NUMBER() over(order by id) +3, name From Table2
Unfortunately, that presupposes the esoteric knowledge that you have 3 items in Table1. Not exactly flexible code.
Also, since there is the possibility of duplicates between the two tables, the use of UNION could return incorrect results.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply