Solved

How to add more fields to the query below?

Posted on 2012-03-29
6
323 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I find this data? 3 24
SQL Syntax 6 27
Related to SQL Query 5 19
Need some alteration to below mention query 2 10
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

740 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