Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Stuck with sorting columns
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


Report this thread to moderator Post Follow-up to this message
Old Post
wgerrard@gmail.com
12-23-05 12:23 PM


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!


Report this thread to moderator Post Follow-up to this message
Old Post
wgerrard@gmail.com
12-23-05 12:23 PM


Re: Stuck with sorting columns
Oh and I need it my documentid not patient!!!  doh....


Report this thread to moderator Post Follow-up to this message
Old Post
wgerrard@gmail.com
12-23-05 12:23 PM


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/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
12-24-05 01:24 AM


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)

Report this thread to moderator Post Follow-up to this message
Old Post
Hugo Kornelis
12-24-05 01:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 04:17 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006