Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


inserting horizontal records from table1 as vertical records in table2


inserting horizontal records from table1 as vertical records in table2

Author
Message
ssurekha2000
ssurekha2000
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 207
i have table with horizontal records , which i need to insert in another table as vertical records

example
FDT1 TODT1 FDT2 TODT2 FDT3 TODT3
2009-12-14 2009-12-28 2009-12-29 2010-01-13 NULL NULL
2010-01-14 2010-01-28 2010-01-29 2010-02-13 NULL NULL
2010-02-14 2010-02-28 2010-03-01 2010-03-13 NULL NULL
2010-03-14 2010-03-28 2010-03-29 2010-04-13 NULL NULL
2010-04-14 2010-04-28 2010-04-29 2010-05-13 NULL NULL
2010-05-14 2010-05-28 2010-05-29 2010-06-13 NULL NULL
2010-06-14 2010-06-28 2010-06-29 2010-07-13 NULL NULL
2010-07-14 2010-07-28 2010-07-29 2010-08-13 NULL NULL
2010-08-14 2010-08-28 2010-08-29 2010-09-13 NULL NULL
2010-09-14 2010-09-28 2010-09-29 2010-10-13 NULL NULL
2010-10-14 2010-10-28 2010-10-29 2010-11-13 NULL NULL
2010-11-14 2010-11-28 2010-11-29 2010-12-13 NULL NULL
2010-12-14 2010-12-28 2010-12-29 2011-01-13 NULL NULL
2011-01-14 2011-01-28 2011-01-29 2011-02-13 NULL NULL
2011-02-14 2011-02-28 2011-03-01 2011-03-13 NULL NULL
2011-03-14 2011-03-28 2011-03-29 2011-04-13 NULL NULL
2011-04-14 2011-04-28 2011-04-29 2011-05-13 NULL NULL
2011-05-14 2011-05-28 2011-05-29 2011-06-13 NULL NULL
2011-06-14 2011-06-28 2011-06-29 2011-07-13 NULL NULL
2011-07-14 2011-07-28 2011-07-29 2011-08-13 NULL NULL
2011-08-14 2011-08-28 2011-08-29 2011-09-13 NULL NULL
2011-09-14 2011-09-28 2011-09-29 2011-10-13 NULL NULL
2011-10-14 2011-10-28 2011-10-29 2011-11-13 NULL NULL
2011-11-14 2011-11-28 2011-11-29 2011-12-13 NULL NULL
2011-12-14 2011-12-28 2011-12-29 2012-01-13 NULL NULL
2012-01-14 2012-01-28 2012-01-29 2012-02-13 NULL NULL
2012-02-14 2012-02-28 2012-03-01 2012-03-13 NULL NULL
2012-03-14 2012-03-28 2012-03-29 2012-04-13 NULL NULL
2012-04-14 2012-04-28 2012-04-29 2012-05-13 NULL NULL

the above records i need to insert in another table ie table2
as horizontal rows

table2 has fields

frmdt todt
i need the records frm table1 to be inserted as

frmdt todt

FDT1 TODT1
FDT2 TODT2
FDT1 TODT1
FDT2 TODT2
FDT1 TODT1
FDT2 TODT2
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 3625
INSERT INTO table2
SELECT FDT1, TODT1 FROM table1
UNION ALL
SELECT FDT2, TODT2 FROM table1




Edit: corrected the code with comma's between the columns

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2086 Visits: 22778
INSERT INTO table2(frmdt,todt)
SELECT ca.frmdt,ca.todt
FROM table1 t
CROSS APPLY(SELECT FDT1, TODT1
UNION ALL
SELECT FDT2, TODT2
UNION ALL
SELECT FDT3, TODT3) ca(frmdt,todt)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8297 Visits: 19444
HanShi (7/15/2013)
INSERT INTO table2
SELECT FDT1 TODT1 FROM table1
UNION ALL
SELECT FDT2 TODT2 FROM table1



I think you might need to add a comma between the column names ...


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 3625
Phil Parkin (7/15/2013)
I think you might need to add a comma between the column names ...

You're absolutely right!! I have to look better if I copy/paste from a post.Hehe

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
ssurekha2000
ssurekha2000
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 207
currently the data is inserted as
frmdate           todate
2009-12-14       2009-12-28
2010-01-14 2010-01-28
2010-02-14 2010-02-28
instead of

frmdate       todate
2009-12-14       2009-12-28
2009-12-29 2010-01-13
2010-01-14 2010-01-28
2010-01-29 2010-02-13
2010-02-14 2010-02-28
2010-03-01 2010-03-13
so used cross apply
its working fine
but with a issue in other fields which were taken i query
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 3625
Remember, the output is only quaranteed in a specific order if you specify the ORDER BY in the query. How the data is phisically structured is not importand.

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
ssurekha2000
ssurekha2000
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 207
used INSERT INTO table2(frmdt,todt)
SELECT ca.frmdt,ca.todt
FROM table1 t
CROSS APPLY(SELECT FDT1, TODT1
UNION ALL
SELECT FDT2, TODT2
UNION ALL
SELECT FDT3, TODT3) ca(frmdt,todt)
the records are inserted as needed
thanks very for the same

but a slight issue with other fields that is being inserted

insert into table2(year,frmdt,todt,No,Consno)
SELECT t.YEAR,ca.frmdt,ca.todt @No,@Consno
FROM table1 t
CROSS APPLY(SELECT FDT1, TODT1
UNION ALL
SELECT FDT2, TODT2
UNION ALL
SELECT FDT3, TODT3) ca(frmdt,todt)



@No is a count taken from table3
if @no is 2
then the No column from the table2 will have data as
1
2
1
2
1
2
@Consno is a consecutive no
i am taking max(Consno)+1
so each record inserted will have
1
2
3
4....... consecutive nos
due to union all same no gets inserted in all records
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search