Hey all
I'm writing a large extract report on clients of a pharmacy and the drugs they are prescribed (maximum of 6 drugs in total).
Data structure of the drugs table looks like this:
Client_id, Drug_id, Is_Main_drug
123, 789, 1
123, 526, 0
124, 478, 0
124, 789, 0
124, 526, 1
124, 775, 0
124, 885, 0
124, 993, 0
My aim is to get the csv extract to look like this:
Client_id, Main_drugname, Other_drugname1 , Other_drugname2 , Other_drugname3 , Other_drugname4 , Other_drugname5
All is fine until I get to the Drug table as its obviously normalised, and we have a third column to consider (Is_Main_drug) which has a 1 or 0 flag to indicate if it is the main drug being prescribed. If it is flagged as the main drug (1) then we need to get it into the Main_drugname column. All the client's other drugs (maximum of 5) need to each go in the Other_drugnameX columns...there is no set order in which they need to go in.
All help appreciated!!!
Cheers
James
P.S. I'm using SQL Server 2008.
I'm writing a large extract report on clients of a pharmacy and the drugs they are prescribed (maximum of 6 drugs in total).
Data structure of the drugs table looks like this:
Client_id, Drug_id, Is_Main_drug
123, 789, 1
123, 526, 0
124, 478, 0
124, 789, 0
124, 526, 1
124, 775, 0
124, 885, 0
124, 993, 0
My aim is to get the csv extract to look like this:
Client_id, Main_drugname, Other_drugname1 , Other_drugname2 , Other_drugname3 , Other_drugname4 , Other_drugname5
All is fine until I get to the Drug table as its obviously normalised, and we have a third column to consider (Is_Main_drug) which has a 1 or 0 flag to indicate if it is the main drug being prescribed. If it is flagged as the main drug (1) then we need to get it into the Main_drugname column. All the client's other drugs (maximum of 5) need to each go in the Other_drugnameX columns...there is no set order in which they need to go in.
All help appreciated!!!
Cheers
James
P.S. I'm using SQL Server 2008.
Comment