Solved

Need help in writing Script with the given cindition

Posted on 2011-03-01
6
347 Views
Last Modified: 2012-06-27
I have the table value like below and need to write script to get the below required format result. Please send me the script. thanks.
Table:            
Reportdate      Name      mark
1/1/2011      XXX      10
1/2/2011      XXX      5
1/1/2011      yyy      25
1/2/2011      yyy      60

Required result            
NAME      1/1/2011      1/2/2011
XXX      10      5
YYY      25      60
0
Comment
Question by:PKTG
6 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35014969
try this.
select * 
  from your_table
 pivot (max(mark) for ReportDate in ([1/1/2011],[1/2/2011])) as p

Open in new window

0
 

Author Comment

by:PKTG
ID: 35014996
It is not working and i have many reportdate value like i need to give the report for 1 or 2 month.
when i tried the above script mark values are showing Null.
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 300 total points
ID: 35015012
Hi PKTG,

try this:
--Generate date list
DECLARE @sDates VARCHAR(MAX)

SET @sDates = (SELECT DISTINCT '[' + CONVERT(VARCHAR(50), Reportdate, 101) + '],' FROM YourTable ORDER BY 1 FOR XML PATH(''))
SET @sDates = LEFT(@sDates, LEN(@sDates)- 1)
--SELECT @sDates

---Generate dynamic PIVOT
DECLARE @sSQL NVARCHAR(MAX)

SET @sSQL = 'SELECT pvt.[Name], ' + @sDates + ' FROM (SELECT Reportdate, [Name], MARK FROM YourTable) AS t 
				PIVOT (SUM(MARK) FOR Reportdate IN (' + @sDates + ')) AS pvt ORDER BY 1'
EXEC (@sSQL)				

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 100 total points
ID: 35015014
Can you post the data which is not working.I checked with your data and got the marks fine.
declare @table table(Reportdate date,Name varchar(3),mark int)
insert @table values('1/1/2011','XXX',10)
insert @table values('1/2/2011','XXX',5)
insert @table values('1/1/2011','yyy',25)
insert @table values('1/2/2011','yyy',60)
select * 
  from @table
 pivot (max(mark) for ReportDate in ([1/1/2011],[1/2/2011])) as p
/*
Name	1/1/2011	1/2/2011
XXX	10	5
yyy	25	60
*/

Open in new window

0
 
LVL 10

Assisted Solution

by:John Claes
John Claes earned 100 total points
ID: 35015078
because you're having a dynamical set of Columns in your pivot you should do something like this

 
DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + convert(nvarchar(8),reportdate,112) + ']',
'[' + convert(nvarchar(8),reportdate,112)+ ']')
FROM #tmp
GROUP BY reportdate

DECLARE @query VARCHAR(8000)
SET @query = '
SELECT *
FROM #tmp
PIVOT
(
MAX(mark)
FOR reportdate
IN (' + @columns + ')
)
AS p'

exec (@query)

Open in new window



example
 
create table #tmp
(
reportdate datetime,
[Name] nvarchar(20),
mark int
)

insert into #tmp select '20110101','XXX',10
insert into #tmp select '20110201','XXX',05
insert into #tmp select '20110101','YYY',25
insert into #tmp select '20110201','YYY',60


DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + convert(nvarchar(8),reportdate,112) + ']',
'[' + convert(nvarchar(8),reportdate,112)+ ']')
FROM #tmp
GROUP BY reportdate

DECLARE @query VARCHAR(8000)
SET @query = '
SELECT *
FROM #tmp
PIVOT
(
MAX(mark)
FOR reportdate
IN (' + @columns + ')
)
AS p'

exec (@query)

drop table #tmp

Open in new window



source : http://www.tsqltutorials.com/pivot.php
Part : Dynamic PIVOT - PIVOT without specifying columns

regards

poor beggar
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35016420
It is not working and i have many reportdate value like i need to give the report for 1 or 2 month.
when i tried the above script mark values are showing Null.


so explain what the report is trying to show...

ie 1 want a daily report covering the last 2 months...
or i have weekly figures and want a report for the last 2 months...

are the row values to be discrete or need aggregation for the period...

do the date include a time component?
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question