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!
2 2594
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: -
select X.Title,
-
sum(case when X.Month=7 then X.Info else 0 end) as '7',
-
sum(case when X.Month=8 then X.Info else 0 end) as '8'
-
from (
-
select Title, Month, sum(Info)
-
from yourTable
-
group by Title, Month ) as X
-
group by X.Title
-
Otherwise you need more elaborate solutions.
...
Otherwise you need more elaborate solutions.
I've found this link useful: http://www.mssqltips.com/tip.asp?tip=937
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |