Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Need a PIVOT-like solution

$
0
0

I have a stored procedure based on a primary table that has multiple rows for different dates within a year.  I want the result set to display all dates as separate columns. Normally there are no more than 4 dates for a year.  For example, my table may have the following filtered rows.

PatientID   LabDate

3020      2008-08-15

3020      2008-08-17

3020      2008-08-20

I want each of the dates above in a separate column like below

3020    2008-08-15    2008-08-17    2008-08-20

Below is stored procedure and table definition that I am working with.

SELECT [PatientLabID]
      ,[PatientID]
      ,[LabDate]
  FROM [dbo].[LabsPerPatient]
  WHERE PatientID = 3020
  AND YEAR(LabDate) = 2008

And table definition

CREATE TABLE [dbo].[LabsPerPatient](
	[PatientLabID] [int] IDENTITY(1,1) NOT NULL,
	[PatientID] [int] NULL,
	[LabDate] [smalldatetime] NULL,
 CONSTRAINT [PK_tblLabByPatient] PRIMARY KEY CLUSTERED 
(
	[PatientLabID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>