Solved

Need help in writing Script with the given cindition

Posted on 2011-03-01
6
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 41

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 41

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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