| Author |
Topic |
|
NadJ
Starting Member
7 Posts |
Posted - 2004-12-04 : 10:14:08
|
| I have 2 tables, and there is a common field between them, one of the tables looks like this:ID Comment001 Alpha002 Tango002 Oscar002 Bravo 003 Alpha004 Charlie004 November 005 Zulu006 KiloIt's a poor design, someone could've easily had more fields and called them Comment2, Comment3, Comment4....and so on.I have another table (shown below) which also has the ID column but in that table, the ID is unique unlike table1. ID Port1 Port2 HeadLevel 001 7 2 1002 3 2 1003 1 0 2004 9 1 1005 0 5 1006 4 7 2My query requires information from both tables. But the problem I have is that table1 has more than one value or 'comment' for each ID.How can I write a query which takes each value for comment, combines them (in a string) and writes them out against a unique ID?Thanks for your time. |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-12-04 : 10:22:16
|
quote: It's a poor design
Wrong. It's the proper design for that kind of structure in a relational database.quote: someone could've easily had more fields and called them Comment2, Comment3, Comment4....and so on.
THAT would be a wrong design. Consider yourself fortunate that whoever designed this had the foresight to avoid doing that.These should help you out:http://www.sqlteam.com/searchresults.asp?SearchTerms=csvhttp://www.sqlteam.com/searchresults.asp?SearchTerms=cross+tabThe CSV string building will probably be what you're looking for. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-12-04 : 11:06:04
|
quote: Originally posted by NadJIt's a poor design, someone could've easily had more fields and called them Comment2, Comment3, Comment4....and so on.
The second table you described, you have not given any indication what it has to do with the comments! how is it related? What does port 1, port 2 and headLevel have to do with anything? or the ID column itself, for that matter?- Jeff |
 |
|
|
NadJ
Starting Member
7 Posts |
Posted - 2004-12-04 : 12:02:45
|
OK, pardon one's ignorance on what's a good db design and what's not. But if you understood the purpose of my db you would agree with me that the raw data could've been collected differently (functionality is more important than observing good relational database standards on this occasion IMHO). I've been trough the example at http://www.sqlteam.com/item.asp?ItemID=11021. I have managed to get their example working with no problems. But it seems like an awful lot of work and steps to do just to merge data that is unique in one column but not the other!There must be a simpler method to do what I am after, I hope this is not a case of seasoned experts over flexing their muscles The data I have given is made up. It makes no sense, I know |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-12-04 : 12:43:29
|
quote: The data I have given is made up. It makes no sense, I know
it's not that the DATA makes no sense, it's that the tables themselves -- the structure, column names, the relationship between the two -- makes no sense. big difference.If you give us you real table structures, and just some sample data, and what you are looking for in terms of results, we can help you much better. you didn't really provide us with any information at all. quote: There must be a simpler method to do what I am after, I hope this is not a case of seasoned experts over flexing their muscles
On the article you used, in the comments, there is a really simple UDF technique that is overall the best, IMHO. and it's really easy to use, it's about 4 lines of code. that is the way to get this done. but, again, if you give us more info, we can help you much more. Does this make sense?- Jeff |
 |
|
|
NadJ
Starting Member
7 Posts |
Posted - 2004-12-04 : 13:21:44
|
Hi again, thanks for your help. The database contains 2 tables. One is a list of airports in the USA, and the other contains a list of Runways at each airport. As you know, there can be more than one runway strip at any airpot. R1 and R2 do not indicate two runways but simply denote the ID of the same strip at both ends. You can have runway 9 (090 degrees), but the other end will be 27 (270 degrees - it's reciprocol). If you have a look at example US00071 you will see an example of an airport that has two runway strips.There is another small complication here, the id field is not numeric! Regardsselect top 50 ID, NAME, FAA_ID from Airports where ID like 'US%' ORDER BY ID select top 50 ID, R1, R2 from Runways where ID like 'US%' ORDER BY ID [code]ID Name FAA_IDUS00027 WHITE PLAINS KZUS00033 MASON CO KZUS00071 BROWNWOOD RGNL KZ[code]ID R1 R2US00027 27 09US00033 25 07US00071 31 13US00071 35 17 |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-12-04 : 13:56:05
|
| OK and what are you trying to do?How do you know that the two entries for US00071 are the same runway and is that important?It looks like the second table should have a column to identify unique runways per airport but maybe it's not needed from a system point of view.What are you trying to do with the data?==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-12-04 : 13:56:39
|
| ok ... but ... i have absolutely no idea how that information relates to your original question....so you have these two tables, and ... ??- Jeff |
 |
|
|
NadJ
Starting Member
7 Posts |
Posted - 2004-12-04 : 14:06:36
|
quote: Originally posted by nr OK and what are you trying to do?How do you know that the two entries for US00071 are the same runway and is that important?It looks like the second table should have a column to identify unique runways per airport but maybe it's not needed from a system point of view.What are you trying to do with the data?
It's for a flight planning application I am developing. I need to clean and align the raw data before I bring it into the app.As for your second question - trust me I know these things being in the field! (no seriously there are a hundred and one sources that can verify it) |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-12-04 : 14:11:04
|
quote: As for your second question - trust me I know these things being in the field! (no seriously there are a hundred and one sources that can verify it)
what field? the airline industry? and all this relates to your original question how? what are you looking for?- Jeff |
 |
|
|
NadJ
Starting Member
7 Posts |
Posted - 2004-12-04 : 14:15:00
|
Let's forget about the original question then as it is obviously confusing people.My requirement is to list all Runways for each airport. Table 2 shows there are multiple runways per airport. I would like to have a result whereby the airport ID and name is displayed on the left and every runway displayed one by one, followed by commas, displayed on the right. OR if it's easier, to display each runway in a new column side by side.So, having create the SP first here is the SQL I have prepared so far... (Doesn't work though, fails at line marked with asteriks)--create a temp tableSelect ID, R1, R2into #workingtablefrom Runwayswhere ID like 'US%'group by ID, R1, R2--create a table to hold the listscreate table #Temp1 ( i int not null primary key, list varchar(8000) not null)declare @i int, @maxrowid int, @sql varchar(8000), @list varchar(8000)select @maxrowid = count(ID), --using count because ID is not an integer @i = 0from #workingtablewhile @i <= @maxrowidbegin select @sql = 'select ID' --********FAILS HERE*********-- from #workingtable where rowid = @i exec sp_MakeCharList @codelistselect=@sql, @delimitedlist=@list output insert into #temp1 (i,list) select i, @list from #workingtable where rowid = @i and @list is not null select @i = @i + 1end--return a sample from the final rowsetselect top 10 i, case when len(list) > 50 then convert(varchar(50), left(list,47) + '...') else convert(varchar(50),list) end as listfrom #temp1order by newid()go |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-12-04 : 14:24:30
|
| Doesn't sound like a very useful resultset but you can do it easily using a udfseehttp://www.nigelrivett.net/CSVStringFromTableEntries.htmlThen the query bcomesselect ID , dbo.getrunways(id)from tblgroup by IDYou might want to think about whay you are doing though. What do you want to do with this data.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|