Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cross Tab

Author  Topic 

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 08:51:46
I have written the following SQL code & I would like to know how to be able cross tab the results so have customerno as a row, category as columns & SumOflinesellvaluenet as the values.

select a.customerno, category, Sum(linesellvaluenet) as SumOflinesellvaluenet 
into ##temp1
from tCustomer a
inner join tCustMatRollup b
on a.customerno=b.customerno
inner join tSAPMaterials c
on b.material =c.material
inner join tCategory d
on c.pg4=d.prodgrp
where salesperiod between 200708 and 200807
and a.deleted is null
and a.blacklisted is null
and accounttype = '0001'
and a.salesoffice <> '1000'
group by a.customerno, d.category
order by a.customerno


Does anyone know how I would go about this?

Kind Regards

Carly

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 09:16:15
[code]SELECT *
FROM ##temp
PIVOT (SUM(SumOflinesellvaluenet) FOR category IN ([yourfirstcategory],[yoursecondcategory],...[yourlastcategory]))p
[/code]

if you want to determine category values dynamically use

http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 10:20:25
Thank you very much for your reply.

I have tried the simple version (as opposed to the dynamic version) as my skills are a bit lacking in that department, but I get the following error message:

Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '('.

Is there a simple solution to this error?
Code is now as follows:

select a.customerno, category, Sum(linesellvaluenet) as SumOflinesellvaluenet 
into ##temp1
from tCustomer a
inner join tCustMatRollup b
on a.customerno=b.customerno
inner join tSAPMaterials c
on b.material =c.material
inner join tCategory d
on c.pg4=d.prodgrp
where salesperiod between 200708 and 200807
and a.deleted is null
and a.blacklisted is null
and accounttype = '0001'
and a.salesoffice <> '1000'
group by a.customerno, d.category
order by a.customerno
GO
select *
from ##temp
pivot (sum(SumOflinesellvaluenet) for category in ([Clothing],[Footwear],[Gloves],[Personal Protection],[Sector Specific],[Workplace]))


Kind Regards

Carly

P.S. I have tried this code in SQL 2000 & SQL 2005
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 10:26:54
quote:
Originally posted by CarlyBond

Thank you very much for your reply.

I have tried the simple version (as opposed to the dynamic version) as my skills are a bit lacking in that department, but I get the following error message:

Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '('.

Is there a simple solution to this error?
Code is now as follows:

select a.customerno, category, Sum(linesellvaluenet) as SumOflinesellvaluenet 
into ##temp1
from tCustomer a
inner join tCustMatRollup b
on a.customerno=b.customerno
inner join tSAPMaterials c
on b.material =c.material
inner join tCategory d
on c.pg4=d.prodgrp
where salesperiod between 200708 and 200807
and a.deleted is null
and a.blacklisted is null
and accounttype = '0001'
and a.salesoffice <> '1000'
group by a.customerno, d.category
order by a.customerno
GO
select *
from ##temp
pivot (sum(SumOflinesellvaluenet) for category in ([Clothing],[Footwear],[Gloves],[Personal Protection],[Sector Specific],[Workplace]))p


Kind Regards

Carly

P.S. I have tried this code in SQL 2000 & SQL 2005


you missed an alias

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 10:27:44
b/w why are you using ## tables? are you aware of problems that can happen if this code happen to execute simultaenously by multiple users?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 10:35:17
I still get the same message:

select *
from ##temp1
pivot (sum(SumOflinesellvaluenet) for category in ([Clothing],[Footwear],[Gloves],[Personal Protection],[Sector Specific],[Workplace]))p


I am using ## tables as I am the only person who will be running this code.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 10:46:19
quote:
Originally posted by CarlyBond

I still get the same message:

select *
from ##temp1 t
pivot (sum(SumOflinesellvaluenet) for category in ([Clothing],[Footwear],[Gloves],[Personal Protection],[Sector Specific],[Workplace]))p


I am using ## tables as I am the only person who will be running this code.


try putting alias for main table too

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 11:00:56
I now get:

Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near 'pivot'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 11:03:56
quote:
Originally posted by CarlyBond

I now get:

Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near 'pivot'.


Are you really on SQL 2005? If yes, whats the compatibility level set?

run below and post result

SELECT @@VERSION
EXEC sp_dbcmptlevel 'your db name'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 11:22:58
I am actually using SQL 2000 but as the post you initially sent was a link about SQL 2005, I ran it in 2005 as well & still got the same message.

Results of query is:

Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) Feb 9 2007 22:47:07 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 11:25:46
quote:
Originally posted by CarlyBond

I am actually using SQL 2000 but as the post you initially sent was a link about SQL 2005, I ran it in 2005 as well & still got the same message.

Results of query is:

Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) Feb 9 2007 22:47:07 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 1)


you didnt post result of compatibility level

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 11:29:08
Sorry the message appeared on a seperate tab, I'm not used to using 2005.

The current compatibility level is 80.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 11:35:45
yup..that explains it. pivot is available only from compatibility level 90 onwards. so though you're in sql 2005 effectively you're in backward compatibility (sql 2000) mode.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CarlyBond
Starting Member

7 Posts

Posted - 2010-02-17 : 11:38:37
Oh, I see...well thats helpful :) The IT guys must have set it up this way, but forgot to mention it!

No worries, thanks for helping anyway...

I will just have to resort to a CASE...WHEN query instead.

Kind Regards

Carly
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 11:40:33
quote:
Originally posted by CarlyBond

Oh, I see...well thats helpful :) The IT guys must have set it up this way, but forgot to mention it!

No worries, thanks for helping anyway...

I will just have to resort to a CASE...WHEN query instead.

Kind Regards

Carly


yeah ..and obviously with GROUP BY and inside COUNT()

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GnR_Slash
Starting Member

14 Posts

Posted - 2010-02-17 : 11:50:35
Hi,

I don't know if my question is about this discussion, but....

I have a table named: Customer, Projects and Status
I will show only some fields:





Table Customer:
UID int -> identity
Name nvarchar(25) -> primary key



Table Status:
UID int -> identity
Descr nvarchar(15)
Type nvarchar(15)



Table Projects:
UID int -> identity
Project nvarchar(16) -> primary key
CustomerID int -> foreigner key (from Customer table)
FinalCustomerID int -> foreigner key (from Customer table)
ProjectTypeID int -> foreigner key (from Status table)
ProjectStatusID int -> foreigner key (from status table)
SalesTypeID int -> foreigner key (from status table)



The contents of Status Table maybe:
UID Descr Type
1 HW projecttype
2 SW projecttype
3 Opened projectstatus
4 Closed projectstatus
5 Nac salestype
6 Imp salestype





The contents of Customer and Project tables doesn't matter.
Note that I have some fields inside table Projects that is an index of the same table Status, and Customer

What I want is to execute a storedprocedure that returns in one line all information I want, like this:

Project Customer FinalCustomer ProjectType ProjectStatus SalesType
525 HotDogs BigMac SW Opened Nac
526 Hamburgers BigMac SW Opened Nac
527 Bearings SKF HW Closed Imp



Note that I want the description of the field, not the index!

Is it possible?
Is this the right way to do it?

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 11:55:00
its possible. b/w whats the relevance of type field in Status? is that field that distinguishes ProjectType,ProjectStatus, SalesType values?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GnR_Slash
Starting Member

14 Posts

Posted - 2010-02-17 : 12:27:26
quote:
Originally posted by visakh16

its possible. b/w whats the relevance of type field in Status? is that field that distinguishes ProjectType,ProjectStatus, SalesType values?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





It is because I want to use only 1 table to store 'combobox' information instead of more tables.
I made this table called Status instead of:

Table ProjectType:
UID int
Descr nvarchar(15)

Table ProjectStatus
UID int
Descr nvarchar(15)

Table SalesType
UID int
Descr nvarchar(15)

So, I can fill my ComboBox in C#.NET with only one query like: select UID, descr from Status and use Filter property of BindingSource Object

I don't know if this is the better way to do it or i it is better to create 3 identical tables

Thanks for your reply visakh16 !!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 12:41:34
Anyways you've to join with Customer and Status on each of ID values to get the relevant details as columns

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GnR_Slash
Starting Member

14 Posts

Posted - 2010-02-17 : 12:49:13
quote:
Originally posted by visakh16

Anyways you've to join with Customer and Status on each of ID values to get the relevant details as columns

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Exactly!!

But ... I have no idea on how to do it!

What Function or Instruction I need to use?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 12:50:27
INNER JOIN or LEFT JOIN based on if you've data for all of them or not

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
    Next Page

- Advertisement -