Unix Technical Forum

Stuck with sorting columns

This is a discussion on Stuck with sorting columns within the SQL Server forums, part of the Microsoft SQL Server category; --> I have the following table (which is an import from another system I can't mod):- CREATE TABLE [tbl_wsg_maternity_observations] ( ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 02:18 PM
wgerrard@gmail.com
 
Posts: n/a
Default Stuck with sorting columns

I have the following table (which is an import from another system I
can't mod):-

CREATE TABLE [tbl_wsg_maternity_observations] (
[documentname] [varchar] (40),
[clientguid] [decimal](16, 0) ,
[docguid] [decimal](16, 0) ,
[displayname] [varchar] (80),
[valuetext] [varchar] (255) ,
[valuenum] [float] NULL
) ON [PRIMARY]
GO

Where
documentname is the name of the document
clientguid is the unique identifier for my patient
docguid is the unique id for the document
displayname is the dataitem (e.g. diagnosis)
valuetext is the "answer" (e.g. kidney failure)
valuenum is used instead if the valuetext is an integer (e.g.
number of toes)

I am trying to split/change this table so that I have a different table
per document, with one row per patient occurance with the displaynames
as columns.

I have been using the following but it is slow and for large tables
takes hours (literally) to run:-

SELECT distinct
clientguid,
(SELECT DISTINCT case when t2.[ValueText] is null then
cast(t2.[Valuenum] as varchar(10)) else t2.[ValueText]end FROM
tbl_wsg_maternity_observations t2 WHERE 'How many vessels present in
cord' = t2.[Displayname] AND t1.ClientGUID = t2.ClientGUID AND
t1.docGUID = t2.docGUID) as [How many vessels present in cord],

<SNIP...more identical lines, one per dataitem>

INTO tbl_wsg_baby_delivery_details

FROM
tbl_wsg_maternity_observations t1

WHERE
documentname = 'Mat Baby Delivery Details'


Does anyone have any ideas how to do this faster and preferably more
simply?

Will

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 02:18 PM
wgerrard@gmail.com
 
Posts: n/a
Default Re: Stuck with sorting columns

Ah and one thing... I don't want to get flamed with "you shouldn't be
doing this becasue it overturns X years of Y" . Strangly enough I'd
rather not be doing it but its what the user wants!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 02:18 PM
wgerrard@gmail.com
 
Posts: n/a
Default Re: Stuck with sorting columns

Oh and I need it my documentid not patient!!! doh....

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 02:19 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Stuck with sorting columns

(wgerrard@gmail.com) writes:
> Where
> documentname is the name of the document
> clientguid is the unique identifier for my patient
> docguid is the unique id for the document
> displayname is the dataitem (e.g. diagnosis)
> valuetext is the "answer" (e.g. kidney failure)
> valuenum is used instead if the valuetext is an integer (e.g.
> number of toes)
>
> I am trying to split/change this table so that I have a different table
> per document, with one row per patient occurance with the displaynames
> as columns.
>
> I have been using the following but it is slow and for large tables
> takes hours (literally) to run:-


I will have to admit that I don't really understand what the result
is to be. Could you post some sample data (preferrably as INSERT
statements), and the desired result?



--
Erland Sommarskog, SQL Server MVP, esquel@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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 02:19 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Stuck with sorting columns

On 23 Dec 2005 04:04:26 -0800, wgerrard@gmail.com wrote:

>Ah and one thing... I don't want to get flamed with "you shouldn't be
>doing this becasue it overturns X years of Y" .


Hi Will,

I won't flame you, but I will tell you, in all patience, that you should
not do this.

Like Erland, I have trouble understanding what EXACTLY it is that you
want. (I also have trouble understanding WHY you want it, but I just
agreed not to flame you with that <bg>).

But your query LOOKS like some sort of crosstab report. If I'm correct,
then you should indeed look into doing the formatting on the client. It
will be much faster.

If you really have to do it server-side, then check if the following
often-used format for crosstab queries helps you:

USE pubs
go
SELECT pub_id,
SUM(CASE WHEN type = 'business'
THEN ytd_sales ELSE 0 END) AS business,
SUM(CASE WHEN type = 'popular_comp'
THEN ytd_sales ELSE 0 END) AS popular_comp,
SUM(CASE WHEN type = 'psychology'
THEN ytd_sales ELSE 0 END) AS psychology,
SUM(CASE WHEN type = 'trad_cook'
THEN ytd_sales ELSE 0 END) AS trad_cook
FROM titles
GROUP BY pub_id
go


Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:51 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com