473,396 Members | 1,814 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,396 software developers and data experts.

Join or Transpose?

I have a table that has the following fields:
Patient/Date/Problem
The problem field only has 3 different variables that can be used (which I'd like to make into fields).

Example of how it looks:
patient date problem
1 9/1/01 HSF
1 9/1/01 FTP
1 1/1/2000 SDQ
2 8/24/1999 HSF
2 7/21/1998 SDQ


What I am trying to do is make the table look like this:
patient date HSF FTP SDQ
1 9/1/01 Y Y Null
2 8/24/1999 Y Null Null
2 7/21/1998 Null Null Y


I have tried the Pivot but I can't seem to get it to work. I thought I could just use a join, but apparently I am doing that wrong as well. Can you please assist me.

Thank you,
TMP
Oct 7 '09 #1
2 2048
ck9663
2,878 Expert 2GB
If PIVOT is not working, just use JOIN

Expand|Select|Wrap|Line Numbers
  1.  
  2. select patient, date, 
  3. HSF = 
  4.    case 
  5.       when y1.problem = 'HSF' then 'Y
  6.    else NULL end,
  7. FTP =
  8.    case 
  9.       when ftp.patient is not null then 'Y'
  10.    else NULL end,
  11. SDQ =
  12.    case 
  13.       when sdq.patient is not null then 'Y'
  14.    else NULL end
  15. from YourTable y1
  16. left join YourTable FTP on y1.patient = ftp.patient and problem = 'FTP'
  17. left join YourTable SDQ on y1.patient = sdq.patient and problem = 'SDQ'
  18.  
  19.  
Happy Coding!!!


--- CK
Oct 7 '09 #2
Okay, I think you meant ftp.problem and sdq.problem and so on. This is incredibly helpful with the case. I'm still confused with the Join.

Once I use the Case
select y1.patient, y1.date,
HSF =
case when y1.problem = 'hsf' then 'y'
else null end,

FTP =
case when y1.problem = 'ftp' then 'y'
else null end,

SDQ =
case when y1.problem = 'sdq' then 'y'
else null end

from #b y1
I am able to create the new fields and the values are null where they should be. However, when I have a patient with the same ID and same date, and two different problems listed, I get two different rows. When I would like them to be on the same row. I would assume that is where the join comes in but I'm not exactly following it.

Thanks so much for your help. You helped a lot already.
Oct 11 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tuong Do | last post by:
Hi I have a table with data like this ID Type 1 A 1 B 1 C 2 A 2 C
1
by: jenny.rhodes | last post by:
Hello, Please can anyone guide me on how to transpose an access table where I have many records per id eg UserID Question Answer 1 1 a 1 2 d 1 3 ...
1
by: Jenny | last post by:
Hello, Please can anyone guide me on how to transpose an access table where I have many records per id eg UserID Question Answer 1 1 a 1 2 d
7
by: sangeetha | last post by:
Hi, i need to transpose a nx1 matrix to 1xn matrix inorder to multiply with nxn matrix in c language ... can anyone help in this coding ..the nx1 matrix is pi the data type is double *pi..this is...
7
by: Leszek Gruszka | last post by:
I wrote an aplication that write something into tableA in sql2000. I want to write the same, but transposed into tableB. Someone can help me? Any example? *** Sent via Developersdex...
3
by: Gerard Brunick | last post by:
My way is ugly. These has to be a better way. Thanks, Gerard
6
by: JayDawg | last post by:
Excel has this cool little function where you can copy data, and then paste it transposed so that which runs across the rows now runs down a colum and visa versa. Is there a way in access to...
0
by: shantanu | last post by:
I am trying to convert a macro code to c# that will copy the values of a column and paste to anather through paste special. Everything is working fine but the transpose meathod to paste the column...
2
by: prads | last post by:
Hello, Can anybody tell me how i shud go about to perform the transpose of a nonsquare matrix array. Pls give me an idea so that i can write one based on that and then post queries if errors r...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.