September 17, 2010 at 9:30 am
Hi,
I have a database with several tables, containing clinical info. One of the tables (let's name it TableA) has a one-to-many relationship with another table (let's name it TableB).
TableA contains one row per patient, with several variables like age, gender,...
TableB contains info about antibiotics taken by these patients. Of course it may happen one patient takes several antibiotics (maximum 4) at the same time or no antibiotic at all. So the cardinality of the relation would be 0-4.
If I do a left join on my tables, I get a result table with all the patients and all the variables. Example:
PatientID - PatientAge - PatientGender - AntibioticVar1 - AntibioticVar2
----------------------------------------------------------------------
2 - 35 - Male - Vancomycin - 200mg a day
3 - 48 - Female - Vancomycin - 100mg a day
3 - 48 - Female - Methicillin - 200 mg a day
In this case, for patients who are taking several antibiotics, I get several rows. And my client asks me to provide her with a table where there is only one row/patient. So, I need a table like this:
PatientID - PatientAge - PatientGender - Antibiotic1Var1 - Antibiotic1Var2 - Antibiotic2Var1 - Antibiotic2Var2
----------------------------------------------------------------------------------------------------------
2 - 35 - Male - Vancomycin - 200mg a day - NULL - NULL
3 - 48 - Female - Vancomycin - 100mg a day - Methicillin - 200 mg a day
My question is: can I get such a table through SQL (ideally in a view), or do I have to compute it with a language like C#?
Thanks for any advice,
Pierre
September 17, 2010 at 9:37 am
You can do this in SQL, but SQL is not designed to produce this sort of output (lots of reasons, interesting if you like relational theory) and the code can get nasty.
Ideally you would do this client side, in C# or SSRS or Excel or whatever your consumer is.
There's some nice stuff by Jeff Smith that covers the FE and BE options:
September 17, 2010 at 9:55 am
Please refer to the links in my signature that deal with Cross-Tabs and Pivot tables, Parts 1 and 2.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 17, 2010 at 10:29 am
Thanks for your help. I will dwelve into your links on Monday. Have a nice weekend.
September 20, 2010 at 2:31 am
Hi,
I looked at "Cross Tabs and Pivots, Part 1 – Converting Rows to Columns" and it looks like The Multi-Aggregate Cross Tab is the solution for me. But I need a column to "pivot" my data. In my case I should have a column identifying for each patient the nth occurrence of an antibiotic, which would look like:
PatientID - PatientAge - PatientGender - AntibioticOccurrence - AntibioticVar1 - AntibioticVar2
----------------------------------------------------------------------
2 - 35 - Male - 1 - Vancomycin - 200mg a day
3 - 48 - Female - 1 - Vancomycin - 100mg a day
3 - 48 - Female - 2 - Methicillin - 200 mg a day
3 - 48 - Female - 3 - Amoxicillin- 50 mg a day
In this way I would "pivot" the data using the AntibioticOccurrence column. My next question is: how can I add such a column to my table? Using the rank() function?
Thanks for your help,
Pierre
September 20, 2010 at 1:06 pm
pierre.daubresse (9/20/2010)
Hi,I looked at "Cross Tabs and Pivots, Part 1 – Converting Rows to Columns" and it looks like The Multi-Aggregate Cross Tab is the solution for me. But I need a column to "pivot" my data. In my case I should have a column identifying for each patient the nth occurrence of an antibiotic, which would look like:
PatientID - PatientAge - PatientGender - AntibioticOccurrence - AntibioticVar1 - AntibioticVar2
----------------------------------------------------------------------
2 - 35 - Male - 1 - Vancomycin - 200mg a day
3 - 48 - Female - 1 - Vancomycin - 100mg a day
3 - 48 - Female - 2 - Methicillin - 200 mg a day
3 - 48 - Female - 3 - Amoxicillin- 50 mg a day
In this way I would "pivot" the data using the AntibioticOccurrence column. My next question is: how can I add such a column to my table? Using the rank() function?
Thanks for your help,
Pierre
You could use ROW_NUMBER() OVER(PARTITION BY PatientID ORDER BY AntibioticVar1) (if you want to have a different order per patientid you'd need to find a column that'll provide that order - or the reverse order...)
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply