473,769 Members | 7,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

28 New Member
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 2610
azimmer
200 Recognized Expert New Member
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 Recognized Expert New Member
...
Otherwise you need more elaborate solutions.
I've found this link useful: http://www.mssqltips.c om/tip.asp?tip=937
Aug 31 '07 #3

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

Similar topics

23
5685
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 reduced my form request to a simple text string entry, instead of my desired optional parameters. As i have been stuck with a single unfathomable glitch for over a year. Basically, if i enter queries such as ; "select * from table" "select * from...
2
4246
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. Columns in the order by list must be unique. DBCC execution completed. If DBCC printed error messages, contact your system administrator." I can successfully reindex other tables in this database. I thought
3
1980
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 environment this architecture works beautifully -- caller application gets negative HRESULT and error description from IErrorInfo object, and additional "log" information from that "ref" parameter: In .Net it just never returns that paramter...
1
2089
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 client references the driver directly. It uses a connection string to access the driver, and does not provide this string until the user has filled in a login dialog. But when I debug the .exe, I see that the ODBC driver .dll is being loaded very...
0
1254
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 btnModifyGrouping_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnModifyGrouping.Click
19
25466
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 sort columns based on the column header the user has clicked in both Ascending and Descending formats.
5
1997
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 called "students". I need to update a field called "status" on this table for all members that have never attended a class. Class attendance is recorded by another table (which represents the many to
10
3944
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 my method. How can I do this?
1
1729
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 SelectedRecs I want to get results by grouping by the field LocalPort and RemotePort, I try it like this: SELECT CTEnr AS ID, TimeStamp, DatagramSize, Source AS LocalIP,
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.