Author |
Topic |
blaze6947
Starting Member
6 Posts |
Posted - 2007-04-21 : 20:32:57
|
Does anybody have any examples of CrossTab Queries without using Cursors or Temp Tables or a stored proc? I would like a pure T-SQL solution if possible. I searched for a couple of hours and the only examples I can find are using SUM(CASE , but I am trying to find an example that would work with the following Data that doesn't have any aggregates.PersonID EmployeeID nextOfKinName1 2 John2 2 Jack3 2 Bob4 2 Sarah5 5 Mary6 5 Tom7 5 Mark |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2007-04-21 : 22:33:14
|
What are the results you want?e4 d5 xd5 Nf6 |
 |
|
blaze6947
Starting Member
6 Posts |
Posted - 2007-04-22 : 00:34:46
|
I would like to see the reults like this:EmployeeID Name1 Name2 Name3 Name42 John Jack BoB Sarah5 Mary Tom Mark Null |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-22 : 03:04:25
|
quote: Does anybody have any examples of CrossTab Queries without using Cursors or Temp Tables or a stored proc?
How about doing it in your front end reporting tool ? KH |
 |
|
blaze6947
Starting Member
6 Posts |
Posted - 2007-04-22 : 13:46:23
|
quote: Originally posted by khtan
quote: Does anybody have any examples of CrossTab Queries without using Cursors or Temp Tables or a stored proc?
How about doing it in your front end reporting tool ? KH
Sorry that is not an option for me, I need to figure out how to do it in pure T-SQL. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-22 : 14:07:08
|
Why do you need to do it in T-SQL ? Transforming data into that format isn't something that relational databases are really good at. You'll end up needing much more code and it will be much less efficient than doing it at your presentation later.How and where are you outputting your results? A web page, a report, windows app, something else? It is usually very trivial to format/arrange your output there, since that's where it should be done, not at the database layer.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
blaze6947
Starting Member
6 Posts |
Posted - 2007-04-22 : 14:24:29
|
quote: Originally posted by jsmith8858 Why do you need to do it in T-SQL ? Transforming data into that format isn't something that relational databases are really good at. You'll end up needing much more code and it will be much less efficient than doing it at your presentation later.How and where are you outputting your results? A web page, a report, windows app, something else? It is usually very trivial to format/arrange your output there, since that's where it should be done, not at the database layer.- Jeffhttp://weblogs.sqlteam.com/JeffS
Sorry if this posts sounds rude but I am not a novice programmer, if I didn't have the constraints on how I could formulate the solution then I would be done with this problem in about 10 minutes. I need a pure T-SQL solution if possible, if it is not possible then please explain why. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-22 : 14:47:42
|
Guess what, I am not a novice programmer either. If you ask for advice, sometimes it is worthwhile to actually listen to it and consider it, right ? Otherwise, why bother?Why do you refuse to explain to us why you feel it MUST be done in T-SQL? If you can explain that, I'll show you how to do this in T-SQL ... it will be ugly, long and inefficient, but if you must do it in T-SQL, it can be done. my entire point is that it is very rare -- if ever -- that you NEED to do this in T-SQL.How are you outputting these results ????- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
blaze6947
Starting Member
6 Posts |
Posted - 2007-04-22 : 15:15:18
|
quote: Originally posted by jsmith8858 Guess what, I am not a novice programmer either. If you ask for advice, sometimes it is worthwhile to actually listen to it and consider it, right ? Otherwise, why bother?Why do you refuse to explain to us why you feel it MUST be done in T-SQL? If you can explain that, I'll show you how to do this in T-SQL ... it will be ugly, long and inefficient, but if you must do it in T-SQL, it can be done. my entire point is that it is very rare -- if ever -- that you NEED to do this in T-SQL.How are you outputting these results ????- Jeffhttp://weblogs.sqlteam.com/JeffS
I totally agree with all the statements you made about the rarity and the ugliness and inefficiency of trying to do this in T-SQL.I did explain already that I am being required to stick to certain constraints in formulating this answer, the why isn't even known to me. As for the output format, I posted what the output should look like in an earlier post.Thank You. |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2007-04-22 : 18:22:38
|
An exercise in pure MSQLbation, but I'm bored right now and trying to avoid real work:select EmployeeID, Max(Case when KinNumber = 1 then NextofKinName else null end) as Name1, Max(Case when KinNumber = 2 then NextofKinName else null end) as Name2, Max(Case when KinNumber = 3 then NextofKinName else null end) as Name3, Max(Case when KinNumber = 4 then NextofKinName else null end) as Name4from (select EmployeeID, NextofKinName, count(*) as KinNumber from [YourTable] inner join [YourTable] Ordinal on [YourTable].EmployeeID = Ordinal.EmployeeID and [YourTable].PersonID >= Ordinal.PersonID group by EmployeeID, NextOfKinName) Subquerygroup by EmployeeID e4 d5 xd5 Nf6 |
 |
|
blaze6947
Starting Member
6 Posts |
Posted - 2007-04-22 : 20:43:53
|
Thanks Blindman for giving me the correct answer and thanks to anyone else who replied. All replies are greatly appreciated. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-23 : 09:21:49
|
Good stuff, blindman, that's what I was thinking. blaze -- There a couple of things to keep in mind, however, which I why I recommend not doing this in T-SQL unless absolutely necessary (which I doubt it is):1) What happens when there are more than 4 names?2) compare the performance of this SQL statement with a standard SQL statement that just returns the results normally to let the front-end format it horizontally. The performance penalty is HUGE. - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|