Solved

How to add more fields to the query below?

Posted on 2012-03-29
6
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

752 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