473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Change "Group By" data in one column into multiple columns based on Group

Hi,

I have a SQL table that has data like this:

Title Month Info
1 ---- 7 ---- 100
1 ---- 7 ---- 100
1 ---- 8 ---- 200
1 ---- 8 ---- 250
2 ---- 7 ---- 150
2 ---- 7 ---- 150
2 ---- 8 ---- 300
2 ---- 8 ---- 300
3 ---- 7 ---- 250
3 ---- 7 ---- 250
3 ---- 8 ---- 100
3 ---- 8 ---- 200

I am then summing the Info column and grouping it by Title and Month, so my query now looks like this:

Title Month Info
1 ---- 7 ---- 200
1 ---- 8 ---- 450
2 ---- 7 ---- 300
2 ---- 8 ---- 600
3 ---- 7 ---- 500
3 ---- 8 ---- 300

What I would like to do is have the Group By Months display as individual columns, and the applicable Info data to appear underneath them. For example, this is what I want to appear:

Title --- 7 ----- 8
1 ---- 200 ---- 450
2 ---- 300 ---- 600
3 ---- 500 ---- 300

I'm not sure how to accomplish this task. If anyone has any ideas, I'd greatly appreciate it. Thanks!
Aug 31 '07 #1
2 2594
azimmer
200 Expert 100+
Hi,

I have a SQL table that has data like this:

Title Month Info
1 ---- 7 ---- 100
1 ---- 7 ---- 100
1 ---- 8 ---- 200
1 ---- 8 ---- 250
2 ---- 7 ---- 150
2 ---- 7 ---- 150
2 ---- 8 ---- 300
2 ---- 8 ---- 300
3 ---- 7 ---- 250
3 ---- 7 ---- 250
3 ---- 8 ---- 100
3 ---- 8 ---- 200

I am then summing the Info column and grouping it by Title and Month, so my query now looks like this:

Title Month Info
1 ---- 7 ---- 200
1 ---- 8 ---- 450
2 ---- 7 ---- 300
2 ---- 8 ---- 600
3 ---- 7 ---- 500
3 ---- 8 ---- 300

What I would like to do is have the Group By Months display as individual columns, and the applicable Info data to appear underneath them. For example, this is what I want to appear:

Title --- 7 ----- 8
1 ---- 200 ---- 450
2 ---- 300 ---- 600
3 ---- 500 ---- 300

I'm not sure how to accomplish this task. If anyone has any ideas, I'd greatly appreciate it. Thanks!
If you only have 7 and 8 in the month column (or a small, fixed set) the following pattern should help:
Expand|Select|Wrap|Line Numbers
  1. select X.Title,
  2.           sum(case when X.Month=7 then X.Info else 0 end) as '7', 
  3.           sum(case when X.Month=8 then X.Info else 0 end) as '8' 
  4. from (
  5.     select Title, Month, sum(Info)
  6.     from yourTable
  7.     group by Title, Month ) as X
  8. group by X.Title
  9.  
Otherwise you need more elaborate solutions.
Aug 31 '07 #2
azimmer
200 Expert 100+
...
Otherwise you need more elaborate solutions.
I've found this link useful: http://www.mssqltips.com/tip.asp?tip=937
Aug 31 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
2
by: John Morris | last post by:
I'm running a simple DBCC DBREINDEX ('myTable') and I receive the following error: "Server: Msg 169, Level 15, State 2, Line 2 A column has been specified more than once in the order by list....
3
by: Dmitry | last post by:
Hi, I have defined interface for COM components which inludes an argument being filled with additional error info, if such occurs. If inside I raise COM Error, I populate that parameter. In COM...
1
by: Eric Hirst | last post by:
I have a Visual Studio Solution .sln which contains multiple projects, including an ODBC driver .dll and a client .exe. The client .exe is a simple MFC query tool. Nothing I can find in the...
0
by: TaeHo Yoo | last post by:
After running my code, group tree(the left section of the report) changes but not the content in the report. My code is ---------------------------------------------------------- Private Sub...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
10
by: John Bailo | last post by:
I want to pass a SqlCommand object as a input parameter to a method. I want to pass the SqlCommand "by value" so that any updates to the original object are *not* reflected in the object within...
1
by: Thomas Qi | last post by:
There is a basic sql below: SELECT CTEnr AS ID, TimeStamp, DatagramSize, Source AS LocalIP, Destination AS RemoteIP, Protocol, Messages, SourcePort AS LocalPort, DestPort AS RemotePort FROM...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.