?
Solved

Need help in writing Script with the given cindition

Posted on 2011-03-01
6
Medium Priority
?
367 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 1200 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 400 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 400 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

777 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