473,407 Members | 2,629 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,407 software developers and data experts.

Crosstabbing (or rotating) a sql table

I have a table DEFINITION with this content:

def_id game_id generaldef_id definition
========================================
1 1 1
firstname
2 1 2 lastname
3 1 3 age
4 1 4 status
5 1 5 position
6 2 6 firstname
7 2 7 lastname
8 2 8 nickname
etc...

Note: There are many possible values to the "definition" row. So the
solutions needs to be DYNAMIC!
Note 2: Don't worry too much about different game_id's, they are for
different kind of games...

Then I have another table PLAYERDEF with the following values:

playedef_id user_id generaldef_id value
======================================
1 1 1 allan
2 1 2
shearer
3 1 3 44
4 1 4 retired
5 1 5 forward
6 2 1 george
7 2 2
mitropoulos
etc...

Then I have a USER TABLE somewhere, but I leave it out now...

How can I merge these tables into this kind of RESULT_SET:

game_id user_id firstname lastname age
status position ....
================================================== ===============
1 1 allan shearer
44 retired forward
1 2 george mitropoulos 26
active goalie
etc...

So I need some kind of crosstabbing, or pivoting or rotating....
Any help appreciated.
I'm on SQL Server 2005.

-pom-
Feb 4 '08 #1
4 1687
pompair (ti*******@gmail.com) writes:
I have a table DEFINITION with this content:

def_id game_id generaldef_id definition
========================================
1 1 1
firstname
2 1 2 lastname
3 1 3 age
4 1 4 status
5 1 5 position
6 2 6 firstname
7 2 7 lastname
8 2 8 nickname
etc...

Note: There are many possible values to the "definition" row. So the
solutions needs to be DYNAMIC!
Note 2: Don't worry too much about different game_id's, they are for
different kind of games...
...
So I need some kind of crosstabbing, or pivoting or rotating....
Any help appreciated.
I'm on SQL Server 2005.
While static pivoting is possible to do in SQL Server, there is nothing
built-in for dynamic pivoting. But have a look at SQL Server MVP Itzik
Ben-Gan's sp_pivot at
http://www.sqlmag.com/Article/Articl...ver_94268.html.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 4 '08 #2
Consider avoiding t-SQL for such tasks primarily because it is the wrong
tool for the job. SQL has little to do with data represented or retrieved in
a varying set of columns.

A better way would be to get the resultset to the client side, use procedure
programming language to loop through the rows and create the display format
you need.

--
Anith
Feb 5 '08 #3
Anith Sen wrote:
Consider avoiding t-SQL for such tasks primarily because it is the wrong
tool for the job. SQL has little to do with data represented or retrieved in
a varying set of columns.

A better way would be to get the resultset to the client side, use procedure
programming language to loop through the rows and create the display format
you need.
Unless of course you just happen to live in a country with laws that
regulate such matters as audit trails on financial data.

Take it out of the database, into Excel no doubt, and you have destroyed
any nuance of accountability.

The proper place for data is in the database.

The proper place for cross-tabulation is in a reporting tool.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)
Feb 5 '08 #4
On Feb 5, 8:34 am, DA Morgan <damor...@psoug.orgwrote:
Anith Sen wrote:
Consider avoiding t-SQL for such tasks primarily because it is the wrong
tool for the job. SQL has little to do with data represented or retrieved in
a varying set of columns.
A better way would be to get the resultset to the client side, use procedure
programming language to loop through the rows and create the display format
you need.

Unless of course you just happen to live in a country with laws that
regulate such matters as audit trails on financial data.

Take it out of the database, into Excel no doubt, and you have destroyed
any nuance of accountability.

The proper place for data is in the database.

The proper place for cross-tabulation is in a reporting tool.
--
Daniel A. Morgan
University of Washington
damor...@x.washington.edu (replace x with u to respond)
Hi Daniel,
I think you misunderstoodd Anith's point. Anith meant that you should
use something else to access the data directly from the database and
loop through each relevant record generating a display as needed. For
example ASP.Net, C#, VB.Net can all access SQL directly through ADO
but you can code to present the data any way you want - not something
you can necessarily do in T-SQL. Not to export data then manipulate
the data which is how I believe you have read the suggestion.

Cheers
Chris

Feb 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ian Hubling | last post by:
I'm trying to complete a rotating banner ad within a page I have. The rotating add has four images that rotate in three-second increments. I've got the images to rotate ok - but now I want to go...
39
by: Zak McGregor | last post by:
Hi all Are there any good solutions to aligning form field names and input boxes without resorting to tables? I am struggling to do this nicely at the moment. Thanks Ciao Zak
1
by: Sandy Bremmer | last post by:
I have seen many Javascripts that rotate images with each load or refresh of the page but so far all I've found require hard coding the image filename into the script. Does anyone know of a script...
1
by: Grunt | last post by:
Hi, I have been trying to put together a rotating banner. the code works but I am having a problem with the caching of the banner images. no matter what I try the page is constantly reloading the...
31
by: Royal Denning | last post by:
I am designing a table with 2 columns and 20 rows. I want to insert small images (each with a link) and a text title in each cell of the table. On refresh of the page, I would like to have the...
4
by: Roozbeh GHolizadeh | last post by:
Hi there... i think it is a very common problem but i dont know why i didnt find anything good for me on internet... i want to simply rotate an array with 90,180,270 degrees.... here is how...
3
by: avalence | last post by:
Hello, I am trying to create a nice rotating earth globe (on mouse) on my web site, in order to display my professional relationships all over the world. The best way seems to be a javascript. In...
1
by: AR123 | last post by:
Hi I want to set up a rotating banner. Not sure how to incorporate my rotating banner code into the code below. I want the rotating banner to be the main feature image? This is set up in...
6
by: swethak | last post by:
Hi, I displayed the image taken from database.How to raotate that image using javascript.plz tell that how to start the logic.plz tell that some reference websites.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.