Solved

How to add more fields to the query below?

Posted on 2012-03-29
6
312 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why Delete * statement wont work with sql server ? 6 41
Help creating a spatial object in SQL Server 4 22
Query Syntax 17 34
sql server tables from access 18 18
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

770 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