This is the first time I've tried using PIVOT. I am using Microsoft SQL Server.
So, here is my problem, I read Pivot and decided that it is perfect for a project that exports patient data to a formatted file, i.e. report that can be printed, etc. etc.
VPatientPlusAllergyData is a VIEWER that displays it as a sample result with some data cut out for readability.
strPatientFullName strAllergy strAllergyMedication
Smith, John Henry Dogs Pounces
Smith, John Henry Dogs Orange Juice
Smith, John Henry Mustard Ketchup
Smith, John Henry Mustard Sugar
As a result i want
strPatientFullName strAllergy1 strAllergy1Medications strAllergy2 strAllergy2Medications
Smith, John Henry Dogs Pounces, OrangeJuice Mustard Ketchup, Sugar
After reading through W3Schools, watching a Youtube video, and even reading some articles on this site, I wonder if what I'm trying to do is possible
- , , IN, PIVOT, .
GO
SELECT
strPatientFullName
,strStreetAddress
,strCity
,strState
,strZipcode
,strPrimaryPhoneNumber
,strSecondaryPhoneNumber
,blnSmoker
,decPackYears
,blnHeadOfHousehold
,dtmDateOfBirth
,strSex
,strAllergy
,strAllergyMedication
,strEmailAddress
,strRecordCreator
FROM ( SELECT * FROM VPatientPlusAllergyData ) PatientAllergyData
PIVOT
(
MAX(strAllergyMedication)
FOR strAllergy
IN ()
)
GO
, -, Pivot, , , .
****** EDIT: , , , , SELECT , " ".
, , script , ******