Can PIVOT do what I want

I have a stored procedure that returns using PIVOT.  

Column1        Column2       Column3
178.09             19.02               124.45        

Can PIVOT return the following?
 Date          Column1        Column2       Column3
12/01          50.09              4.00               50.45  
12/02          78.00              1.02                24.00
12/03           50.00             15.00              50.00
rcowen00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rcowen00Author Commented:
ALTER PROCEDURE [dbo].[SalesJournal_NEW]
	@Location varchar(25)='147',
	@Begin smalldatetime ='12/1/15' ,
	@End smalldatetime ='12/30/15',
	@UserId varchar (75)='ddd'
AS
BEGIN
	SET ARITHABORT ON


DECLARE @cols AS nvarchar(max)

SELECT @cols = STUFF((SELECT     ',' + QUOTENAME(MediaType.MediaGroup) 
FROM   MediaType INNER JOIN
       Tenders INNER JOIN
       Locations ON Tenders.[Location Name] = Locations.LocationNameOrig ON MediaType.MediaType = Tenders.Name
WHERE     (Tenders.Date >= @BEGIN) 
AND (Tenders.Date < DATEADD(dd, 1, @End)) 
AND (Locations.[Location Name] = @Location)
AND (Tenders.[Location Name] <> N'Cash')
GROUP BY MediaType.MediaGroup
--ORDER BY Tenders.[Location Name]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')

DECLARE @query AS nvarchar(max)

SET @query = 'SELECT * from   
             (
                SELECT m.MediaGroup, Tenders.Amount AS Amount   
		FROM MediaType m INNER JOIN
                     Tenders INNER JOIN
                     Locations ON Tenders.[Location Name] = Locations.LocationNameOrig ON M.MediaType = Tenders.Name
                WHERE     (Tenders.Date >= ''' + convert(varchar(8),@BEGIN,112) + ''') 
		AND (Tenders.Date < DATEADD(dd, 1,'''+convert(varchar(8),@End,112)+'''))
		AND (Locations.[Location Name] = '''+@Location+''')
		AND (Tenders.[Location Name] <> N''Cash'')
            ) x
           PIVOT 
            (
               sum(Amount)   
               for MediaGroup in (' + @cols + ')
            ) p '

EXECUTE(@query)

END
RETURN

Open in new window

Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
In the SELECT query that supplies data to the PIVOT operator, also fetch the required date and I believe you should be all set.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rcowen00Author Commented:
Added the date column to this section and it worked great.  Thanks!


SET @query = 'SELECT * from  
             (
                SELECT m.MediaGroup, Tenders.Amount AS Amount,Tenders.Date  
            FROM MediaType m INNER JOIN
                     Tenders INNER JOIN
                     Locations ON Tenders.[Location Name] = Locations.LocationNameOrig ON M.MediaType = Tenders.Name
                WHERE     (Tenders.Date >= ''' + convert(varchar(8),@BEGIN,112) + ''')
            AND (Tenders.Date < DATEADD(dd, 1,'''+convert(varchar(8),@End,112)+'''))
            AND (Locations.[Location Name] = '''+@Location+''')
            AND (Tenders.[Location Name] <> N''Cash'')
            ) x
           PIVOT
            (
               sum(Amount)  
               for MediaGroup in (' + @cols + ')
            ) p '

EXECUTE(@query)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.