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
Solved

Need help in writing Script with the given cindition

Posted on 2011-03-01
6
353 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

856 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