473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL Query - Help

I have the following data in a table T1:

Col1 Col2 Col3 Col4 Col5
10 x 20 a b
10 x 20 a1 b1
10 y 20 c d

Now looking at this table T1, which I am generating dynamically as a temp-table, I want to "insert" data in a different table T2 as rows, in the following fashion:

Single_Column
10 x
20 a b
20 a1 b1
10 y
20 c d

To get you familiarity with the format of the data in T2, this would be more like an XML structure, where "10 x" is the data in the parent-node, and "20 a b", "20 a1 b1" are the two child nodes. But in the table T2 shown above, this child-node data will be in different rows, just below the parent-node data row.

Please advice about the insert statement.

Thanks.
Jan 6 '08 #1
1 874
camel
55
Code below will do what you ask, though I wonder a little why you need to do this..If data is hierachical the db schema should be designed to store it as such e.g. with parent-child id relations rather than having to sort it out after the event

Expand|Select|Wrap|Line Numbers
  1. /* Assemble Test Data */
  2. CREATE TABLE #OldStructure
  3. (ParentID int IDENTITY(1,1), Col1 int, Col2 char(1), Col3 int, Col4 varchar(2), Col5 varchar(3))
  4.  
  5. INSERT #OldStructure(Col1,Col2,Col3,Col4,Col5)
  6. SELECT 10,'x',20,'a','b'
  7. INSERT #OldStructure(Col1,Col2,Col3,Col4,Col5)
  8. SELECT 10,'x',20,'a1','b1'
  9. INSERT #OldStructure(Col1,Col2,Col3,Col4,Col5)
  10. SELECT 10,'y',20,'c','d'
  11.  
  12. /* Put Data in new table as desired */
  13. CREATE TABLE #NewStructure
  14. (SingleColumn varchar(20), ParentNode varchar(20), OriginalOrder int)
  15.  
  16. INSERT #NewStructure(SingleColumn, ParentNode)
  17. SELECT    CONVERT(varchar(8),Col1) + Col2, CONVERT(varchar(8),Col1) + Col2
  18. FROM    #OldStructure
  19. GROUP BY Col1, Col2
  20.  
  21. INSERT  #NewStructure(SingleColumn, ParentNode, OriginalOrder)
  22. SELECT    CONVERT(varchar(8),Col3) + Col4 + Col5, CONVERT(varchar(8),Col1) + Col2, ParentID  
  23. FROM    #OldStructure O
  24.  
  25. SELECT SingleColumn FROM #NewStructure
  26. ORDER BY ParentNode, OriginalOrder
  27.  
  28.  
Jan 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.