| Author |
Topic |
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-06-15 : 15:22:32
|
My question:How to have an unknown number of authors returned for a list of books, with the multiple authors being returned on the SAME row as each bookMy table structure: Table A - Books-------------------BookID | BookName-------------------1 | Book One Table B - RelatingTable-------------------BookID | AuthorID-------------------1 | 601 | 61 Table C - Authors-------------------AuthorID | AuthorName-------------------60 | Bob61 | Joe My query:SELECT * FROM Books, RelatingTable, Authors AS aINNER JOIN Books AS b ON b.BookID = r.BookIDINNER JOIN RelatedTable AS r ON r.AuthorID = a.AuthorID Output I am getting:-------------------------------------------------------------------------b.BookID | b.BookName | r.BookID | r.AuthorID | a.AuthorID | a.AuthorName---------|------------|----------|------------|------------|-------------1 | Book One | 1 | 60 | 60 | Bob1 | Book One | 1 | 61 | 61 | Joe------------------------------------------------------------------------- Desired output:-----------------------------------------------------------------------------------------------------------------------------------------------------b.BookID | b.BookName | r.BookID[0] | r.AuthorID[0] | a.AuthorID[0] | a.AuthorName[0] | r.BookID[1] | r.AuthorID[1] | a.AuthorID[1] | a.AuthorName[1]---------|------------|-------------|---------------|---------------|-----------------|-------------|---------------|---------------|----------------1 | Book One | 1 | 60 | 60 | Bob | 1 | 61 | 61 | Joe----------------------------------------------------------------------------------------------------------------------------------------------------- The desired output does not have to be like an array - I only assume thats what it would need to beIf you can post anything to help me get in the right direction I would really appreciate itThanks for your time! |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-15 : 15:36:34
|
| You are confusing presentation with data. You already have the SQL statement you need that gives you the exact data you want; the question is, how are you presenting this data and where? You should not try to create multiple columns in a database result to make things look a certain way, such as horizontally aligned in columns -- that is something for your presentation layer to handle. SQL is simply not designed to efficiently handle things like this; plus, what happens when an author has 10 books? Or 50 books? How many columns should it go to? There's no such thing as "column arrays" in SQL; sets of data is always represented in rows, not columns.Also, if you have a result set with varying column names from call to call depending on how many authors and books there are, you can't do any SQL processing of the results since SQL is a declarative language where table and column names are constants; they should be known ahead of time and remain consistent until your database design changes.Always focus on outputting and presenting data at your presentation layer, never at the database layer.If you need help outputting these results in a certain way, that is question of how to do in ASP.NET or in Crystal or Excel or wherever it is you are ultimately displaying this data.I hope this helps.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-06-15 : 15:41:55
|
| thanks jeff, that makes a lot of sense!basically i am returning my results in coldfusion in a table format, the problem is i get the same book returned for however many authors are connected to it, when i want the book to be returned once, and then have a column that lists the authors for iti have a UDF that converts my relationship into a comma list, but all this does for me is return two results for one book with two comma listsit's like i need the relationship to exist, i just only want the comma list returned and not the JOINed columnsdoes that make sense? |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-15 : 15:44:40
|
| It's a cold fusion presentation issue you are having, not a SQL Server issue. I strongly advise you to do things the easiest, simplest, most direct and more efficient way and do your formatting in Cold Fusion. If you know SQL well but not cold fusion, then you should visit a cold fusion forum and ask for help there.Also: why do you want BookID to be repeated over and over, as well as AuthorID being repeated at least twice per author? That makes no sense in either SQL or when you are presenting your output.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-06-15 : 16:08:33
|
| i was only repeating stuff to illustrate how i thought the columns might have to work if the many-many relationship returned on one rowthe reason i was trying to get it to return on one row is because whenever people view the list of books they get something likeBook One by BobBook One by Joeinstead of something like Book One by Bob Joewhich i could reformat to be more presentable etcI have tried using a comma UDF that COALESCE's my many-many into a list, but i still have a problem with the CFM output because the JOINs still existI get something likeBook one by Bob, JoeBook one by Bob, Joe |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-15 : 16:11:31
|
| Since you are not showing use your UDF code, it is hard to guess what you are doing, but you should not be joining at all if your UDF is what is returning a list of authors based on the bookID. You just need to:select b.*, dbo.GetAuthors(bookID) as Authorsfrom books... that's it. - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-06-15 : 16:15:44
|
| yes but heres where things get more complexeven though i have a comma list - i still need the JOINs because you select to see the list of books by clicking on an authors namefor exampleClick on Bob (&author=60)Book One by Bob, JoeBook Three by BobClick on Joe (&author=61)Book One by Bob, JoeBook Two by Joethe relationship still needs to exist for filtering purposes!the only other way i can think of getting this to work is doing something like'Joe' LIKE authorlist which does not seem efficient to me(so simply put: the comma list is used for display purposes only, the relationship is used to determine when certain books are displayed)the other problem is that i need an accurate rowcount because i am using a next-n records script that is based on how many rows are returnedeven if i am able to get CFM to not display the dupes, they will still be counted in the rowcount and thus mess up next-n records |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-15 : 20:01:03
|
| You've confirm even more than before that your issue is 100% a DISPLAY and presentation issue in cold fusion. You have our SQL query done; pat yourself on the back and be happy that your database tier is all set for this task. You don't need a UDF, don't need to concatenate things into a CSV string, just return a simple resultset with a simple join, you already showed the sql for that in your original post.Now open up cold fusion, find a good cold fusion reference book if you need it, visit a cold fusion forum, and get to work on writing the *presentation code* to display the data returned on your web page exactly how you need it. All you need to do is loop through your data and output the HTML that you need to align the authors horizontally. I know you can do this very, very easily in cold fusion, but if you try to force SQL to do it, with UDFs and other things that are completely unnecessary, it will only complicate both your SQL code AND your cold fusion code.I can only repeat the same thing so many times, hopefully you will consider following my advice and give it a shot. You will be a better and more efficient programmer if you learn to do things in the proper place in your code.Good luck.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-06-15 : 20:10:16
|
| thanks jeff you have really been a helpmy issue with coldfusion is that i cannot get the code to display it in the format i want - a cfloop does not have a group="" field which is necessary to fixing the dupe returns - this is why i looked to solving the issue before cf even gets the results, thats why i turned to SQL UDF'si will keep trying though, i will continue posting around on CFM boards and keep searchingat least now i understand that my SQL structures have been correctthanks for your time |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-15 : 21:09:21
|
| if cold fusion, just delcare a variable, call it "BookID" or something, and set it to the bookID of the first row. Then, as long as each row as the same bookID, you keep putting them all together into 1 row. If the bookID of the row is a different one, start a new row. It's the standard way to do grouping in a language like ASP or coldfusion where you loop through the rows in a query result and output them to the page.so, in pseudo-code, you have something like this:1) declare bookID variable, set it to -12) open up recordset, return rows3) for each row in result:a) if the row's BookID != the bookID variable: start a new row, output the book name, set bookID variable equal to bookIDb) output author info4) loop5) you are done!- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
sjlsam
Starting Member
9 Posts |
Posted - 2007-06-15 : 21:23:54
|
| jeff you've been most helpfuli think with those resources i will be able to solve my problem,i can't tell you how long i've been scratching my head over thisthe support on this forum appears to be of excellent quality!thanks for all your help, i will try these techniques first thing when im back at work monday |
 |
|
|
|