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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Pivot question subcolumns

Author  Topic 

eurob
Posting Yak Master

100 Posts

Posted - 2006-02-28 : 16:21:02
I have this sql:

select client,function,items,errors from mytable

that will show like this

client1 function1,2,1
client2 function2,4,2
client1 function2,7,3

I would like to have it like this:
----------------function1--------------function2
--------------items---errors----------items---errors
client1---------2-------1---------------7--------3
client2---------4-------2---------------0--------0

I read the article about how to dynamically create pivot output (http://sqlteam.com/item.asp?ItemID=2955) , but how would I do the subcolumns (items,errors) ?

robert

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-28 : 18:10:20
[code]select client,
max(case when func = 'function1' then item else 0 end) as item1,
max(case when func = 'function1' then error else 0 end) as error1,
max(case when func = 'function2' then item else 0 end) as item2,
max(case when func = 'function2' then error else 0 end) as error2
from mytable
group by client

Result :
client item1 error1 item2 error2
---------- ----------- ----------- ----------- -----------
client1 2 1 7 3
client2 0 0 4 2[/code]

----------------------------------
'KH'


Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-03-01 : 08:09:59
What I need is a column with 2 subcolumns, like in my example.

robert
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-03-01 : 08:11:19
quote:
Originally posted by eurob

What I need is a column with 2 subcolumns, like in my example.

robert


What do you mean by this ?

----------------------------------
'KH'


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-01 : 08:16:15
quote:
Originally posted by eurob

What I need is a column with 2 subcolumns, like in my example.

robert


Where do you want to show the data?
Show those columns in your Front End application

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-03-01 : 08:34:37
I was hoping my first example would clarify. I use this data to populate an excel sheet.
So what I want is to regroup a column into two sub columns as in my example,
client1 has function1 that consists of 2 items and 1 error.
client2 has function1 that consists of 4 items and 2 errors etc.

robert
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-01 : 08:40:56
Excel has pivot tables built-in! Use Excel to do this! Just return summarized, grouped data using SQL, and let Excel pivot it. It can create headings, "sub-columns", totals, groups, filters, pages, anything you need.

Not only that, but it is easier to write (no complicated T-SQL, not fancy stored procs, no dynamic SQL), shorter, quicker, clearer and so on.

use SQL to return the *data*. Use Excel to *format* the data.

Makes sense, right? Don't make things more complicated than they need to be.
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-03-01 : 08:59:52
I did look into that but I couldn't figure out how to put two sub columns under one column but I agree that formatting shouldn't have to be done in sql. It's just that I would want to present all the data as the user want to see it, without the user having to do any formatting.

robert
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-01 : 09:03:48
Jeff, I think you have to start new post in your blog that explains where to format data so that we dont need to repeat it again and again and simply point to your blog post

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-01 : 09:52:38
quote:
Originally posted by eurob

I did look into that but I couldn't figure out how to put two sub columns under one column but I agree that formatting shouldn't have to be done in sql. It's just that I would want to present all the data as the user want to see it, without the user having to do any formatting.

robert



Drag function to the top, set it as a column.
drag client to the left, set it as a row.
Drag items and errors to the values part in the middle. Each will get it's own column.

You need to play around with it. It's very easy to use, the great part is that end-users can create any totals or subtotals or filters or groups they want just by dragging and dropping.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-01 : 09:53:13
quote:
Originally posted by madhivanan

Jeff, I think you have to start new post in your blog that explains where to format data so that we dont need to repeat it again and again and simply point to your blog post

Madhivanan

Failing to plan is Planning to fail



I know. It's one of the ones I've begun to write many times but never quite was able to finish it. Maybe soon!
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2006-03-01 : 10:44:34
When I drag the items and errors to the middle (data area), they are displayed vertically next to client vs horizontally next to eachother under the function column.
I found out that in Crystal Reports you have more control over how to display those columns by using the crosstab option and play with the style option under the summarizing fields.
I now am able to export it to Excel in the exact format that I wanted.


robert
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-01 : 13:15:02
You're welcome.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-02 : 01:56:38
>>I know. It's one of the ones I've begun to write many times but never quite was able to finish it. Maybe soon!

I am expecting it

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Julie19
Starting Member

32 Posts

Posted - 2012-06-13 : 14:52:43
Hello eurob,

Did you accomplish ypur requirement?
I have a similar requirement
but instead of functions I have dates
Instead of items and errors I have Daily and Attain.

Please share your knowledge
I would really aapreciate it

Thanks a lot




Go to Top of Page
   

- Advertisement -