|
Home > Archive > Microsoft SQL Server forum > December 2005 > Stuck with sorting columns
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Stuck with sorting columns
|
|
| wgerrard@gmail.com 2005-12-23, 7:23 am |
| I have the following table (which is an import from another system I
can't mod):-
CREATE TABLE & #91;tbl_wsg_maternit
y_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_ob
servations 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_de
livery_details
FROM
tbl_wsg_maternity_ob
servations t1
WHERE
documentname = 'Mat Baby Delivery Details'
Does anyone have any ideas how to do this faster and preferably more
simply?
Will
| |
| wgerrard@gmail.com 2005-12-23, 7:23 am |
| 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!
| |
| wgerrard@gmail.com 2005-12-23, 7:23 am |
| Oh and I need it my documentid not patient!!! doh....
| |
| Erland Sommarskog 2005-12-23, 8:24 pm |
| (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/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx
| |
| Hugo Kornelis 2005-12-23, 8:24 pm |
| 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)
|
|
|
|
|