| Author |
Topic |
|
leewebdev
Starting Member
3 Posts |
Posted - 2007-03-14 : 09:24:35
|
| Hi everyone! I'm new to the this forum, but have a little SQL background. Looking forward to learning a lot from everyone here. I have a report grouping question, but I don’t think the GROUP BY command will work. Look at the sample table below:Person FavColor------ --------David RedMike BlackJohn RedSteve RedSam BlackAdam PurpleCurrently, the above temp table is created via a SQL sproc and returned to a Cold Fusion web page and displayed as a simple table. What I’d like to do is have the display look like this:BLACKMikeSamPURPLEAdamREDDavidJohnSteveMy question is, is it possible to program SQL to output the above so I can just dump the output into the Web page, or do I have to program the Web page to take my current result set and somehow iterate through the table to pull out and display group headings?I don’t need to show subtotals or grand totals, so I'm thinking GROUP BY will not work here. I’m using SQL 2000 and do NOT have Reporting Services. Any help would be greatly appreciated. Thanks!David |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 10:46:18
|
| [code]-- Prepare sample dataDECLARE @Sample TABLE (Person VARCHAR(5), FavColor VARCHAR(6))INSERT @SampleSELECT 'David', 'Red' UNION ALLSELECT 'Mike', 'Black' UNION ALLSELECT 'John', 'Red' UNION ALLSELECT 'Steve', 'Red' UNION ALLSELECT 'Sam', 'Black' UNION ALLSELECT 'Adam', 'Purple'-- Show the expected resultSELECT CASE WHEN RecID = 0 THEN UPPER(Person) ELSE Person END AS PersonFROM ( SELECT 1 AS RecID, FavColor, Person FROM @Sample UNION ALL SELECT DISTINCT 0, FavColor, FavColor FROM @Sample UNION ALL SELECT DISTINCT 2, s1.FavColor, '' FROM @Sample AS s1 WHERE s1.FavColor <> (SELECT MAX(s2.FavColor) FROM @Sample AS s2) ) AS xORDER BY FavColor, RecID, Person[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
leewebdev
Starting Member
3 Posts |
Posted - 2007-03-14 : 11:07:39
|
| Pure genius! Thanks so much Peter. The code is simple and was easy for me to modify. It tested flawlessly in my app. Thanks again.David |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-03-14 : 11:16:46
|
| leewebdev -- this type of formatting should always be done on your web page. It is very, very easy to do. SQL should just return the results -- formatting should never be done at the database layer.What are you using? ASP? ASP.NET? It takes about 4-5 lines of code in those languages to do the formatting, and your sql is much shorter, faster, simpler, and more maintainable if you keep it focused on the *data* and not the formatting.Peso -- Do you really think that you are providing good advice to someone just learning by suggesting that they do this in T-SQL ?- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 11:32:33
|
| We do not know what the modifications are and I have not much knowledge of ColdFusion.Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-03-14 : 11:37:04
|
quote: Originally posted by X002548 Pure Genius!SELECT Person, FavColorFROM myTable99ORDER BY FavColor, PersonBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
Brett -- you just "optimized" the solution and made it about 4 times as fast! Amazing work!Oh -- cold fusion. I missed that part, thanks for pointing it out. Not sure about cold fusion as well. But I am quite sure that you must have some sort of looping mechanism and can do a simple "if Favorite <> LastFavorite then output header" check .....- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 11:54:16
|
Are you certain that next question from leewebdev is not about"How to do looping and grouping in ColdFusion"or "how do if Favorite <> LastFavorite work"? Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-03-14 : 12:07:16
|
that's fine, that is a perfectly good question to ask if they don't know -- They should learn how to do that if they will be using coldfusion. And, of course, they should probably ask that simple question on a coldfusion forum.And, I already gave them a link! - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
leewebdev
Starting Member
3 Posts |
Posted - 2007-03-14 : 12:10:18
|
| Good points. I do not know Cold Fusion -- I prefer ASP.net -- but that's what I'm forced to use in this case. I will research Cold Fusion loops. However, in the short term, I find it satisfying that I can produce grouping via SQL and maybe it will only be an issue with large record sets, I don't know. The one issue I am having now with the SQL example is if I want to return a few more fields in the SELECT statement; these additional fields are causing the DISTINCT call to breakdown. I'm getting close.David |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-03-14 : 12:14:21
|
| David -- I strongly suggest looking at the link I gave you. I do not know cold fusion either and it was pretty easy to follow along what you need to do.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
|