| 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 ##temp1from 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.prodgrpwhere salesperiod between 200708 and 200807and a.deleted is nulland a.blacklisted is nulland accounttype = '0001'and a.salesoffice <> '1000'group by a.customerno, d.categoryorder by a.customerno Does anyone know how I would go about this?Kind RegardsCarly |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
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 3Line 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 ##temp1from 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.prodgrpwhere salesperiod between 200708 and 200807and a.deleted is nulland a.blacklisted is nulland accounttype = '0001'and a.salesoffice <> '1000'group by a.customerno, d.categoryorder by a.customernoGOselect *from ##temppivot (sum(SumOflinesellvaluenet) for category in ([Clothing],[Footwear],[Gloves],[Personal Protection],[Sector Specific],[Workplace])) Kind RegardsCarlyP.S. I have tried this code in SQL 2000 & SQL 2005 |
 |
|
|
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 3Line 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 ##temp1from 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.prodgrpwhere salesperiod between 200708 and 200807and a.deleted is nulland a.blacklisted is nulland accounttype = '0001'and a.salesoffice <> '1000'group by a.customerno, d.categoryorder by a.customernoGOselect *from ##temppivot (sum(SumOflinesellvaluenet) for category in ([Clothing],[Footwear],[Gloves],[Personal Protection],[Sector Specific],[Workplace]))p Kind RegardsCarlyP.S. I have tried this code in SQL 2000 & SQL 2005
you missed an alias------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
CarlyBond
Starting Member
7 Posts |
Posted - 2010-02-17 : 10:35:17
|
I still get the same message:select *from ##temp1pivot (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. |
 |
|
|
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 tpivot (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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
CarlyBond
Starting Member
7 Posts |
Posted - 2010-02-17 : 11:00:56
|
| I now get:Server: Msg 170, Level 15, State 1, Line 3Line 3: Incorrect syntax near 'pivot'. |
 |
|
|
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 3Line 3: Incorrect syntax near 'pivot'.
Are you really on SQL 2005? If yes, whats the compatibility level set?run below and post resultSELECT @@VERSIONEXEC sp_dbcmptlevel 'your db name' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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) |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 RegardsCarly |
 |
|
|
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 RegardsCarly
yeah ..and obviously with GROUP BY and inside COUNT()------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 StatusI will show only some fields: Table Customer:UID int -> identityName nvarchar(25) -> primary key Table Status:UID int -> identityDescr nvarchar(15)Type nvarchar(15) Table Projects:UID int -> identityProject nvarchar(16) -> primary keyCustomerID 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 Type1 HW projecttype2 SW projecttype3 Opened projectstatus4 Closed projectstatus5 Nac salestype6 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 CustomerWhat I want is to execute a storedprocedure that returns in one line all information I want, like this:Project Customer FinalCustomer ProjectType ProjectStatus SalesType525 HotDogs BigMac SW Opened Nac526 Hamburgers BigMac SW Opened Nac527 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? |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 MVPhttp://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 intDescr nvarchar(15)Table ProjectStatusUID intDescr nvarchar(15)Table SalesTypeUID intDescr 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 tablesThanks for your reply visakh16 !!! |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/
Exactly!!But ... I have no idea on how to do it!What Function or Instruction I need to use? |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Next Page
|