Solved

How to add more fields to the query below?

Posted on 2012-03-29
6
299 Views
Last Modified: 2012-03-30
SELECT  YEAR(start) AS 'Year', MONTH(start) AS 'Month'
FROM dbo.Events
Group By Year(start), Month(start) 
ORDER BY Month ASC

Open in new window


What I am trying to do is get my events to display grouped by Months so that it comes up something like this:

January 2012
Jan 3, 2012 Event 1    Zone 1 field 2 field 3
Jan 19, 2012 Event 2  Zone 2 field 2 field 3
Jan 28, 2012 Event3  Zone 3 field 2 field 3

February 2012
Feb 6, 2012 Event A  Zone 1 field 2 field 3
Feb 22, 2012 Event B   Zone 2 field 2 field 3
Feb 24, 2012 Event C  Zone 3 field 2 field 3

So I found a statement( above ) that I was able to sort the data by year and month but it won't let me add more fields to the SELECT. ( Must be a better way? )
I get this error:
"Column 'blah' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

Very grateful for your assistance experts.
Thank you in advance.
0
Comment
Question by:colonelblue
6 Comments
 
LVL 11

Accepted Solution

by:
N R earned 250 total points
ID: 37784623
You can add more fields to the select but you must also add them to you "Group By" clause.  Unless you use an aggregate function on them such as MIN,MAX,SUM ect....
0
 
LVL 9

Assisted Solution

by:rajeevnandanmishra
rajeevnandanmishra earned 250 total points
ID: 37785112
Like:
SELECT MIN(MONTH(start)) AS 'Month', MIN(YEAR(start)) as 'Year', 
	Start, Event, Zone, Field2, Field3 
FROM dbo.Events 
GROUP BY Start, Event, Zone, Field2, Field3 
ORDER BY 2, 1, 3

Open in new window

0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37785970
might accoriding to result that you are getting there is no rolw of groupby remove that.
else follow rajeevnandanmishra's post.


regards.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Closing Comment

by:colonelblue
ID: 37789164
Thank you , you guys rock!<br />However please if you can, how do I insert a Month/Year heading followed by a line break before every group?<br /><br />Thank you so much.
0
 
LVL 27

Expert Comment

by:tliotta
ID: 37789345
Your desired output has two very different kinds of lines. One line simply has month and year. I.e.:

January 2012

The other kind of line has all of the detail columns that you want to see. I.e.:

Jan 3, 2012 Event 1    Zone 1 field 2 field 3

A SELECT statement will give one of those or it will give the other. A single SELECT statement can't supply both (not without some fairly advanced manipulation).

There are two general alternatives.

First, you can create a kind of UNION between two SELECT statements; one provides the group heading while the other provides the group details.

And second, you might feed the result from a SELECT for the details through either a program or a reporting function. The formatting of your output would be done by something other than the SQL itself.

It depends on what tools you have available.

Tom
0
 

Author Comment

by:colonelblue
ID: 37789426
Thanks Tom.
I apologize.
Yes the end result would be
"Jan 3, 2012 Event 1    Zone 1 field 2 field 3"

If I were to use the Union Approach, I find it difficult to wrap my head around  "the group heading" part. Would that entail creating a table for headers or is that created dynamically.
Perhaps there is some sort of ASP Script I can utilize.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now