Solved

How to add more fields to the query below?

Posted on 2012-03-29
6
335 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:
Nathan Riley 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

622 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