Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Friday, March 30, 2012

Please help Invalid Cloumn Name

Hi,
Here is my SP
I need to run this as this
ConFirmOrders_SP 'SVR,UCC' this is giving me
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'UCC'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'SVR'.
ALTER Procedure ConfirmOrders_sp
(
@.StrType Varchar(100)
)
As
BEGIN
SET NOCOUNT ON
Create table #Confirm
(
ShipName Varchar(80),
Reference Varchar(60),
ReqNo int,
CorpName varchar(255),
ReqDate datetime,
RptType nVarchar(24)
)
Declare @.SQL varchar(5000)
Insert into #Confirm(ShipName,Reference,ReqNo,CorpNa
me,ReqDate,RptType)
SELECT Sh.ShiptoName , Req.RefNo , Req.Reqno ,
Case Sm.ItemGroup
When 'UCC' Then Req.Debtor
When 'SVR' Then Req.Respecting
When 'SAT' Then Req.CorpName
When 'SRO' Then Req.CorpName
When 'TRO' Then Req.CorpName
When 'REG' Then Req.CorpName
End As [Name],Req.Reqdate,SM.ItemGroup As [Req Type]
FROM dbo.tblArShipTo Sh INNER JOIN
dbo.tblCorpRpt Req ON Sh.CustId = Req.CustId INNER JOIN dbo.tblSmItem SM
ON Req.ReqType = SM.ItemCode
WHERE Req.Reqstatus ='W' AND Sh.ShiptoId ='MAIL'
Order By Req.Reqno
Set @.SQL=
'Select ShipName AS [Cust Name],Reference,ReqNo,CorpName AS [Name],
ReqDate AS [Req Date],RptType AS [Rpt Type] from #Confirm WHERE Rpttype IN
(' + @.StrType + ')'
Exec(@.SQL)
ENDSVR and UCC are individual strings so they both need to be quoted
individually. Change your call to something like this:
ConFirmOrders_SP '''SVR'',''UCC'''
You'll need to double check that I have the quotes done correctly, but you
should get the idea.
--Brian
(Please reply to the newsgroups only.)
"Dib" <dNOSPAMshahene@.conNOSPAMsoftware.com> wrote in message
news:uRETsQ%23rFHA.2212@.TK2MSFTNGP15.phx.gbl...
> Hi,
> Here is my SP
> I need to run this as this
> ConFirmOrders_SP 'SVR,UCC' this is giving me
> Server: Msg 207, Level 16, State 3, Line 1
> Invalid column name 'UCC'.
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'SVR'.
>
> ALTER Procedure ConfirmOrders_sp
> (
> @.StrType Varchar(100)
> )
> As
> BEGIN
> SET NOCOUNT ON
> Create table #Confirm
> (
> ShipName Varchar(80),
> Reference Varchar(60),
> ReqNo int,
> CorpName varchar(255),
> ReqDate datetime,
> RptType nVarchar(24)
> )
> Declare @.SQL varchar(5000)
> Insert into #Confirm(ShipName,Reference,ReqNo,CorpNa
me,ReqDate,RptType)
> SELECT Sh.ShiptoName , Req.RefNo , Req.Reqno ,
> Case Sm.ItemGroup
> When 'UCC' Then Req.Debtor
> When 'SVR' Then Req.Respecting
> When 'SAT' Then Req.CorpName
> When 'SRO' Then Req.CorpName
> When 'TRO' Then Req.CorpName
> When 'REG' Then Req.CorpName
> End As [Name],Req.Reqdate,SM.ItemGroup As [Req Type]
> FROM dbo.tblArShipTo Sh INNER JOIN
> dbo.tblCorpRpt Req ON Sh.CustId = Req.CustId INNER JOIN dbo.tblSmItem SM
> ON Req.ReqType = SM.ItemCode
> WHERE Req.Reqstatus ='W' AND Sh.ShiptoId ='MAIL'
> Order By Req.Reqno
> Set @.SQL=
> 'Select ShipName AS [Cust Name],Reference,ReqNo,CorpName AS [Name],
> ReqDate AS [Req Date],RptType AS [Rpt Type] from #Confirm WHERE Rpttype IN
> (' + @.StrType + ')'
>
> Exec(@.SQL)
> END
>|||Thanks
Dib
"Brian Lawton" <brian.k.lawton@.redtailcr.com> wrote in message
news:OWca8W%23rFHA.908@.tk2msftngp13.phx.gbl...
> SVR and UCC are individual strings so they both need to be quoted
> individually. Change your call to something like this:
> ConFirmOrders_SP '''SVR'',''UCC'''
> You'll need to double check that I have the quotes done correctly, but you
> should get the idea.
> --
> --Brian
> (Please reply to the newsgroups only.)
>
> "Dib" <dNOSPAMshahene@.conNOSPAMsoftware.com> wrote in message
> news:uRETsQ%23rFHA.2212@.TK2MSFTNGP15.phx.gbl...
>sql

Tuesday, March 20, 2012

Placing multiple records on a single line (variables)

Hi, I am new to Crystal Reports, but I know Basic and other programming. I have Crystal Reports XI and am pulling data from our ERP/MRP system, Epicor Vista (Progress DB).

I've been asked to figure out a Crystal Reports for our company (I get thrown into these projects). I know what the report should look like and I know how I would go about some VB code in a macro in Excel if all the data was in worksheets(i.e. like tables).

Below is the data. Any help would be SO appreciated. So far I'm loving Crystal Reports and I can't wait to get some reports our company can start using but I'm stuck on understanding the timing and connection of formulas with the records.

Table1 "JobMtl"
Field "JobComplete":String
Field "JobNum":String
Table2 "JobOper"
Field "OpComplete":Boolean
Field "OprSeq":Number

{JobMtl.JobComplete}
False
True
{JobMtl.JobNum}
2010
2011
{JobOper.Complete}
False
True
{JobOper.OprSeq}
10
20
30
40

Let's say I dragged all 4 fields into a report. It would look like this.

JobNum JobComplete OprSeq OpComplete
2010 False 10 True
2010 False 20 True
2010 False 30 False
2010 False 40 False
2011 False 10 True
2011 False 20 False
2011 False 30 False

I would it to read like this

JobNum JobComplete PrevOp CurrOp NextOp
2010 False 20 30 40
2011 False 10 20 30

**Note: {JobMtl.JobComplete} will be used so I am only reporting jobs that are "not complete". I guess it means nothing to you guys, but I put it here because I was not sure if this will be involved in a formula.

Thanks,
Anthony

My email is ls1z282002_at_yahoo.com (replace "_at_" => "@.") if you would a *.RPT with the data I've shown.Check your e-mail.|||Come on
We all want to see the solution|||Here's what I got from her.

There is a second report that somone from another forum helped me on. I actually need to combine both of these into one because I like the report from SvB_NY because she mad the operations a single String. So I need to do some combining of the two.

I have another question that I'm posting below this.

Anthony|||After showing this to everyone at work, they of course asked for more detail in the report :)

What we have is for each operation {JobOper.OprSeq} it may be an Outsourced (There is a boolean field {JobOper.OprSeq}) that states whether that Operation is outsourced. If it is outsourced they want me to list the PONum & POLn. I know this is simple and I thought so too! I even got it too work! So here's the snag...if the Job does not have any outsourced operations it gets skipped in my report! My reasoning is our ERP software doesn't actually make a index for PO's if that job does not have any PO's against it. Makes sense to me. So how do I handle this?

In the report I attached there should be 3 jobs
2010
2011 => Not shown because no outsourced operations
2012

Am I going to have to create 2 seperate reports, save all the information in arrays. Then match up the arrays with some code and print out a report?

Thanks,
Anthony|||SvB_NY, Thanks for all the help!

I got my report to work and I published it into our ERP software. I appreciate all the help and attached is the final product, if anyone cares to look.

I did never get the PO's to work right because I found out we have multiple PO's to a given operation and that created multiple records for the operation, so my logic for getting the "Prev,Curr,Next" operation didn't work. This is OK though because I was running out of room for the data and I had to have a big comment field. If it was dire for us I'm sure I could get it to work, or I actually I would have posted the question here :)

Anthony

Monday, March 12, 2012

Placing labels on line chart

Hi,
Some of my charts have many data series on them and the colours can be
difficult to differentiate. I would like to include a label on the
actual line chart as the legend is not helpful - the colours are too
similar. Is this easy to do?
For an example see here:
http://tinyurl.com/3kkou
Thanks.
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.htmlFollowing the provided link shows an error on the server side. Not sure if
this is exactly what you are looking for, but you may be interested in the
thread "bar chart help" and the example attached at the bottom of the
posting.
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.sqlserver.reportingsvcs&mid=7f5be65f-e515-4993-9526-e9e85480c67a&sloc=en-us
For the line charts you may however want to have the labels e.g. only on the
first datapoint of the line and would need to use the First() aggregate
function to determine it.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mark Allison" <marka@.no.tinned.meat.mvps.org> wrote in message
news:eGPmtFZ7EHA.3336@.TK2MSFTNGP11.phx.gbl...
> Hi,
> Some of my charts have many data series on them and the colours can be
> difficult to differentiate. I would like to include a label on the
> actual line chart as the legend is not helpful - the colours are too
> similar. Is this easy to do?
> For an example see here:
> http://tinyurl.com/3kkou
> Thanks.
> --
> Mark Allison, SQL Server MVP
> http://www.markallison.co.uk
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602m.html

placing a new line character

hi all

i have a filed in my database with name address which contains addresses.

i want to split address in multiple line .

LIke

addressline1

addressline2

addressline3

i.e

Robart Peter

4th Banglow road

MC city

can any one help me.

vbNewLine or vbcrlf functions can be used to wrap the content to next line.|||thanks

placing a new line character

hi all

i have a filed in my database with name address which contains addresses.

i want to split address in multiple line .

LIke

addressline1

addressline2

addressline3

i.e

Robart Peter

4th Banglow road

MC city

can any one help me.

vbNewLine or vbcrlf functions can be used to wrap the content to next line.|||thanks

Friday, March 9, 2012

PKZip issue

I have command line PKZip (pkzipc.exe) installed on my SQL Server 2000.
I want to use it to zip up some text files that I receive and process
on a daily basis to save space. I want to schedule it to run as a job.
The problem is that when I run the job as a standard user, it just does
not work. No error messages or anything. If I make the job owned by
SA, it is fine.

My only thought is that when I am signed in as a SA, my account on the
box is in the Admin group. When I sign in as a standard user, I am not
sure what account it uses to hit the file system. I would assume it
would use the xp_cmdshelluser account I have set up but I am not sure.

Has anyone seen this before? It has me stumped.

Any ideas??

Thanks!

Jim Youmans
St Louis, MissouriIn SQL 2000, the SQL Agent Proxy account is used as the OS security context
for non-sysadmin users. This is configurable from Enterprise Manager under
SQL Agent-->Properties-->Job System. When the 'Only users with Sysadmin
privileges..' is unchecked, you can specify the Windows account to be used
as the security context for non-sysadmin users. It's best to specify a
minimally privileged account.

I see you are located in St. Louis, You might consider joining our local
SQL Server User Group (http://www.stlssug.org/index.html) if you haven't
already done so. We also have a BI special interest group.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Jim" <jdyoumans@.gmail.comwrote in message
news:1158185320.259108.169510@.p79g2000cwp.googlegr oups.com...

Quote:

Originally Posted by

>I have command line PKZip (pkzipc.exe) installed on my SQL Server 2000.
I want to use it to zip up some text files that I receive and process
on a daily basis to save space. I want to schedule it to run as a job.
The problem is that when I run the job as a standard user, it just does
not work. No error messages or anything. If I make the job owned by
SA, it is fine.
>
My only thought is that when I am signed in as a SA, my account on the
box is in the Admin group. When I sign in as a standard user, I am not
sure what account it uses to hit the file system. I would assume it
would use the xp_cmdshelluser account I have set up but I am not sure.
>
Has anyone seen this before? It has me stumped.
>
Any ideas??
>
Thanks!
>
Jim Youmans
St Louis, Missouri
>

|||Thank you for your suggestions. I am a member of the user group and my
coworker who solved this was at the meeting on Wed.

Here is how we ended up solving the issue. Hope it helps someone else.

"I was finally able to get PKZIPC to work as a non-SQL administrator on
FENSQLMLMD04, but it's a bit of a hack. Thanks for all your
suggestions and help. Kudos to Jerry for the final idea...

Tim, since you gave terminal services rights to US\US_SQLCmdshell,
pkzipc worked whenever I remoted in to the server as US_SQLCmdshell.
But as soon as I logged off, it stopped working. Same problem between
pkzipc version 8 and version 6 (except that 6 hangs), same problem on
the other sql server.

I think the problem is that pkzipc is looking in the HKCU registry key,
just to see if it exists. If it does, pkzipc reads the user's
environment information from another registry key,
HKLM...\ProfileList\... and then looks like it sets up the command
prompt environment variables. But if HKCU doesn't exist, it drops out
or hangs.

Maybe that's a standard Win32API thing whenever a command shell program
is launched? I don't think so, because I can run other commands like
"xcopy" fine. Anyway, when I'm not logged in as US_SQLCmdshell, the
HKCU key doesn't exist so pkzipc drops out at that point before it ever
loads the user's environment information.

So Jerry suggested that we use a "dummy" or "benign" Windows service
that is running as US\US_SQLCmdshell in order to keep it logged in.
Brilliant -- now it works! "

Jim Youmans
St Louis Missouri

Dan Guzman wrote:

Quote:

Originally Posted by

In SQL 2000, the SQL Agent Proxy account is used as the OS security context
for non-sysadmin users. This is configurable from Enterprise Manager under
SQL Agent-->Properties-->Job System. When the 'Only users with Sysadmin
privileges..' is unchecked, you can specify the Windows account to be used
as the security context for non-sysadmin users. It's best to specify a
minimally privileged account.
>
I see you are located in St. Louis, You might consider joining our local
SQL Server User Group (http://www.stlssug.org/index.html) if you haven't
already done so. We also have a BI special interest group.
>
--
Hope this helps.
>
Dan Guzman
SQL Server MVP
>
"Jim" <jdyoumans@.gmail.comwrote in message
news:1158185320.259108.169510@.p79g2000cwp.googlegr oups.com...

Quote:

Originally Posted by

I have command line PKZip (pkzipc.exe) installed on my SQL Server 2000.
I want to use it to zip up some text files that I receive and process
on a daily basis to save space. I want to schedule it to run as a job.
The problem is that when I run the job as a standard user, it just does
not work. No error messages or anything. If I make the job owned by
SA, it is fine.

My only thought is that when I am signed in as a SA, my account on the
box is in the Admin group. When I sign in as a standard user, I am not
sure what account it uses to hit the file system. I would assume it
would use the xp_cmdshelluser account I have set up but I am not sure.

Has anyone seen this before? It has me stumped.

Any ideas??

Thanks!

Jim Youmans
St Louis, Missouri

Monday, February 20, 2012

Pivot Error

Hi :

I am getting the following error message when I am trying to do the Pivot operation.

Msg 265, Level 16, State 1, Line 1

The column name "FirstName" specified in the PIVOT operator conflicts with the existing column name in the PIVOT argument.

Msg 265, Level 16, State 1, Line 1

The column name "LastName" specified in the PIVOT operator conflicts with the existing column name in the PIVOT argument.

Msg 8156, Level 16, State 1, Line 1

The column 'FirstName' was specified multiple times for 'Pvt'.

My SQL Query is:

SELECT UserID,FirstName,LastName

FROM

(

SELECT UserID,FirstName,LastName

FROM Tempreport AS ATR

PIVOT (

MAX(QuestionAnswer)

FOR QuestionText

IN ([CompanyAddress1],[CompanyAddress2],[CompanyName],[Country],[EmailAddress],[FirstName],[LastName],[PhoneNumber],[PostalCode],[Select the Category that best describes your Industry],[Select the Category that best describes your role],[Select the Category that best describes your title],[Town])) as Pvt

) Result (UserID,FirstName,LastName)

Here is the DDL:

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'LastName','Testing2')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Town','Testing6')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Country','Testing7')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other')

CREATE TABLE Tempreport

(UserID INT,

LastName VARCHAR(255),

FirstName VARCHAR(255),

OrderNumber INT,

QuestionText VARCHAR(255),

QuestionAnswer VARCHAR(255)

)

Drop table tempreport.

Can anyone please let me know what I am doing wrong and how to fix this error?. looks like I cant select the same column inside the pivot and outside the pivot. In such a case what is my alternative?.

Any help is appreciated.

Thanks

M

? What output are you looking for? -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <Meher@.discussions.microsoft.com> wrote in message news:70fe23b8-d832-4a8d-b10e-e4bfec6c69bb@.discussions.microsoft.com... Hi : I am getting the following error message when I am trying to do the Pivot operation. Msg 265, Level 16, State 1, Line 1 The column name "FirstName" specified in the PIVOT operator conflicts with the existing column name in the PIVOT argument. Msg 265, Level 16, State 1, Line 1 The column name "LastName" specified in the PIVOT operator conflicts with the existing column name in the PIVOT argument. Msg 8156, Level 16, State 1, Line 1 The column 'FirstName' was specified multiple times for 'Pvt'. My SQL Query is: SELECT UserID,FirstName,LastName FROM ( SELECT UserID,FirstName,LastName FROM Tempreport AS ATR PIVOT ( MAX(QuestionAnswer) FOR QuestionText IN ([CompanyAddress1],[CompanyAddress2],[CompanyName],[Country],[EmailAddress],[FirstName],[LastName],[PhoneNumber],[PostalCode],[Select the Category that best describes your Industry],[Select the Category that best describes your role],[Select the Category that best describes your title],[Town])) as Pvt ) Result (UserID,FirstName,LastName) Here is the DDL: INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'LastName','Testing2') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Town','Testing6') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Country','Testing7') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other') CREATE TABLE Tempreport (UserID INT, LastName VARCHAR(255), FirstName VARCHAR(255), OrderNumber INT, QuestionText VARCHAR(255), QuestionAnswer VARCHAR(255) ) Drop table tempreport. Can anyone please let me know what I am doing wrong and how to fix this error?. looks like I cant select the same column inside the pivot and outside the pivot. In such a case what is my alternative?. Any help is appreciated. Thanks M|||

The output I am looking is for the Number of Questions that the survey has the responses something like this:

LastName FirstName UserID Address1 Address2 Select the category Select the role

Veerman Maek 10 123 XXX Other Other

Mark Smith 20 324 YYY IT Developer

|||? Something like this, perhaps: CREATE TABLE Tempreport (UserID INT,LastName VARCHAR(255),FirstName VARCHAR(255),OrderNumber INT,QuestionText VARCHAR(255),QuestionAnswer VARCHAR(255)) INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'LastName','Testing2')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'Town','Testing6')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'Country','Testing7')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other')INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other')go SELECT UserID, FName, LName, TotalAnswers, pvt.*FROM ( SELECT UserID, FirstName AS FName, LastName AS LName, QuestionText, QuestionAnswer, COUNT(*) OVER(PARTITION BY UserId) AS TotalAnswers FROM Tempreport) AS ATRPIVOT ( MAX(QuestionAnswer) FOR QuestionText IN ([CompanyAddress1],[CompanyAddress2],[CompanyName], [Country],[EmailAddress],[FirstName],[LastName], [PhoneNumber],[PostalCode], [Select the Category that best describes your Industry], [Select the Category that best describes your role], [Select the Category that best describes your title],[Town])) as Pvtgo drop table tempreportgo -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <Meher@.discussions.microsoft.com> wrote in message news:4bc96011-34ee-46fb-bdfd-822456ee11e4@.discussions.microsoft.com... The output I am looking is for the Number of Questions that the survey has the responses something like this: LastName FirstName UserID Address1 Address2 Select the category Select the role Veerman Maek 10 123 XXX Other Other Mark Smith 20 324 YYY IT Developer|||Thanks Adam. It works!!!.|||

ok there seems to be another issue. For some questions in the survey, the respoonse is more than one. Because there is a check box on the web and the user can choose more than one response. in that case when I pivot the question text it shows up as null since the question text is inserted only once. How do we show all the possible answers (in other words concatenate the answers).

I tried to follow the article of http://www.aspfaq.com/show.asp?id=2529 and use the cross apply operator in SQL Server 2005 but it still shows as null. My query for cross apply was along the lines of

Select UserID,

QuestionText,

QuestionAnswer=LEFT(o.list,LEN(o.list) -1)

FROM Tempreport T

CROSS APPLY

(SELECT

CONVERT(VARCHAR(500),QuestionAnswer)+',' AS [text()]

FROM TempReport T2

WHERE T.UserID=T2.UserID

ORDER BY QuestionAnswer

FOR XML PATH('')

) o (list)

ORDER BY QuestionText

How do I incorporate the Cross apply into my original solution to come out with the question and show all the possible answers concatenated?. I dont want to see NULLs. Any re-write is really helpful.

Thanks

DDL is given below:


CREATE TABLE Tempreport

(UserID INT,

LastName VARCHAR(255),

FirstName VARCHAR(255),

OrderNumber INT,

QuestionText VARCHAR(255),

QuestionAnswer VARCHAR(255)

)

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'LastName','Testing2')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'Town','Testing6')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'Country','Testing7')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'Please indicate which apps you are interested in','Accounting')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,NULL,'Payroll')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,NULL'Unavailable to attend')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,'How often do you pay','Weekly')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,Null,'Fortnightly')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,Null,'Four weekly')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,Null,'Monthly')
INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)
VALUES (200253,'Veerman','Mark',2,Null,'other')

--Original solution

DECLARE @.ListLen INT

DECLARE @.INDEX INT

DECLARE @.month nvarchar(25)

DECLARE @.year nvarchar(25)

DECLARE @.strResult VARCHAR(500)

select @.cstlist = (select DISTINCT QUOTENAME(LTRIM(RTRIM(QuestionText)))+','as [text()]

from Tempreport

order by QUOTENAME(LTRIM(RTRIM(QuestionText)))+','

for xml path(''))

SET @.ListLen=LEN(@.cstList)

SET @.cstList=LEFT(@.cstList,(@.ListLen-1))

SET @.strSQL=@.cstList

PRINT (@.strSQL)

select @.sql = 'SELECT UserID,FName,LName,pvt.* FROM

(SELECT UserID,FirstName AS FName,

LastName AS LName,QuestionText,QuestionAnswer,

COUNT(*) OVER(PARTITION BY UserId) AS TotalAnswers

FROM Tempreport) AS ATR '

+ 'PIVOT (MAX(QuestionAnswer) FOR QuestionText IN (' + @.strSQL+ ')) AS Pvt'

PRINT (@.sql)

--EXEC (@.sql)

|||? How do you know which answers belong to which questions? Can you introduce a QuestionId column or something along those lines? Otherwise, I don't think there's any way to solve this problem... -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <Meher@.discussions.microsoft.com> wrote in message news:16b34575-d985-4cde-9a9a-844627b4f068@.discussions.microsoft.com... ok there seems to be another issue. For some questions in the survey, the respoonse is more than one. Because there is a check box on the web and the user can choose more than one response. in that case when I pivot the question text it shows up as null since the question text is inserted only once. How do we show all the possible answers (in other words concatenate the answers). I tried to follow the article of http://www.aspfaq.com/show.asp?id=2529 and use the cross apply operator in SQL Server 2005 but it still shows as null. My query for cross apply was along the lines of|||

Hi Adam:

I have added the questionID (column name QuestionTextID) as you have suggested. I am helping some one with this problem and their db design is not good. The new DDL is given below:

CREATE TABLE Tempreport

(UserID INT,

LastName VARCHAR(255),

FirstName VARCHAR(255),

OrderNumber INT,

QuestionTextID INT IDENTITY (1,1),

QuestionText VARCHAR(255),

QuestionAnswer VARCHAR(255)

)

GO

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'LastName','Testing2')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Town','Testing6')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Country','Testing7')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Please indicate which apps you are interested in','Accounting')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,NULL,'Payroll')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,NULL,'Unavailable to attend')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'How often do you pay','Weekly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,Null,'Fortnightly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,Null,'Four weekly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,Null,'Monthly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,Null,'other')

|||? Unfortunately, that won't work quite right as-is. It needs to be QuestionId, and needs to correspond to each question. So every answer to the same question will have the same QuestionId. That way, you can simply correlate in the FOR XML PATH subquery: SELECT UserID, FName, LName, TotalAnswers, pvt.*FROM ( SELECT UserID, FirstName AS FName, LastName AS LName, QuestionText, ( SELECT QuestionAnswer + ',' AS [data()] FROM TempReport TR2 WHERE TR2.UserId = TempReport.UserId AND TR2.QuestionId = TempReport.QuestionId FOR XML PATH('') ) AS QuestionAnswers, COUNT(*) OVER(PARTITION BY UserId) AS TotalAnswers FROM Tempreport WHERE QuestionText IS NOT NULL) AS ATRPIVOT ( MAX(QuestionAnswer) FOR QuestionText IN ([CompanyAddress1],[CompanyAddress2],[CompanyName], [Country],[EmailAddress],[FirstName],[LastName], [PhoneNumber],[PostalCode], [Select the Category that best describes your Industry], [Select the Category that best describes your role], [Select the Category that best describes your title],[Town])) as Pvtgo -- Adam MachanicPro SQL Server 2005, available nowhttp://www..apress.com/book/bookDisplay.html?bID=457-- <Meher@.discussions.microsoft.com> wrote in message news:0191df4c-9ecb-4505-8509-e8b5836f73a8@.discussions.microsoft.com... Hi Adam: I have added the questionID (column name QuestionTextID) as you have suggested. I am helping some one with this problem and their db design is not good. The new DDL is given below: CREATE TABLE Tempreport (UserID INT, LastName VARCHAR(255), FirstName VARCHAR(255), OrderNumber INT, QuestionTextID INT IDENTITY (1,1), QuestionText VARCHAR(255), QuestionAnswer VARCHAR(255) ) GO INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'LastName','Testing2') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Town','Testing6') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Country','Testing7') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'Please indicate which apps you are interested in','Accounting') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,NULL,'Payroll') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,NULL,'Unavailable to attend') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,'How often do you pay','Weekly') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,Null,'Fortnightly') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,Null,'Four weekly') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,Null,'Monthly') INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer) VALUES (200253,'Veerman','Mark',2,Null,'other')|||Any ideas?.|||

Thanks Adam, I can make it as the same questionID for the answer as you have suggested.
In that case, would your solution hold good or does it need any more changes?.
Here is the DDL:

CREATE TABLE Tempreport

(UserID INT,

LastName VARCHAR(255),

FirstName VARCHAR(255),

OrderNumber INT,

QuestionTextID INT IDENTITY (1,1),

QuestionText VARCHAR(255),

QuestionAnswer VARCHAR(255)

)

GO

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'FirstName','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'LastName','Testing2')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyName','Testing3')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyAddress1','Testing4')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'CompanyAddress2','Testing5')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Town','Testing6')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Country','Testing7')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'PostalCode','Testing8')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'PhoneNumber','Testing9')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'EmailAddress','Testing9')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your Industry','Testing1')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your role','Other')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',2,'Select the Category that best describes your title','Other')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',12,'Please indicate which apps you are interested in','Accounting')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',12,NULL,'Payroll')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',12,NULL,'Unavailable to attend')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',32,'How often do you pay','Weekly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',32,Null,'Fortnightly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',32,Null,'Four weekly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',32,Null,'Monthly')

INSERT INTO Tempreport (UserID,LastName,FirstName,OrderNumber,QuestionText,QuestionAnswer)

VALUES (200253,'Veerman','Mark',32,Null,'other')

|||

Hi Adam:

Dont worry about this. I got it working after I changed the questionID to be the same for the check box list. Here is the final query that brings back the data i want.

Thanks a lot for your tremendous help. you are awesome!!!

SELECT

UserID,

FName,

LName,

TotalAnswers,

pvt.*

FROM

(

SELECT

UserID,

FirstName AS FName,

LastName AS LName,

QuestionText,

(

SELECT QuestionAnswer + ',' AS [data()]

FROM TempReport TR2

WHERE

TR2.UserId = TempReport.UserId

AND TR2.QuestionTextId = TempReport.QuestionTextId

FOR XML PATH('')

) AS QuestionAnswers,

COUNT(*) OVER(PARTITION BY UserId) AS TotalAnswers

FROM Tempreport

WHERE QuestionText IS NOT NULL

) AS ATR

PIVOT

(

MAX(QuestionAnswers)

FOR QuestionText

IN ([CompanyAddress1],[CompanyAddress2],[CompanyName],

[Country],[EmailAddress],[FirstName],[LastName],

[PhoneNumber],[PostalCode],

[Select the Category that best describes your Industry],

[Select the Category that best describes your role],

[Please indicate which apps you are interested in],[Town],

[How often do you pay])

) as Pvt

go