Solved

Why is this SELECT invalid?

Posted on 2011-09-21
7
233 Views
Last Modified: 2012-06-21
In the query below, b.mydateutc triggers this error:

Column 'tickets.mydateutc' is invalid in the select list because it is not contained in either and aggregate function or the GROUP BY clause.

How do I resolve this?
select dateadd(day, 0, datediff(day, 0, b.mydateutc)) mydate,
count(b.ticketid), b.empid
from temp1 a inner join tickets b
on a.ticketid = b.ticketid
and a.ticketsessionid = b.ticketsessionid

Open in new window

0
Comment
Question by:brettr
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 36574624
you need a group by clause
select dateadd(day, 0, datediff(day, 0, b.mydateutc)) mydate,
count(b.ticketid), b.empid
from temp1 a inner join tickets b on a.ticketid = b.ticketid and a.ticketsessionid = b.ticketsessionid
grouo by b.empid

Open in new window

0
 

Author Comment

by:brettr
ID: 36574715
Unless you've changed something else in the query, which it doesn't seem you have, I'm still getting the same error.
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 300 total points
ID: 36574913
You need to group by all fields in the select list including the date one

select dateadd(day, 0, datediff(day, 0, b.mydateutc)) mydate,
count(b.ticketid), b.empid
from temp1 a inner join tickets b on a.ticketid = b.ticketid and a.ticketsessionid = b.ticketsessionid
grouo by b.empid,  ateadd(day, 0, datediff(day, 0, b.mydateutc))
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:brettr
ID: 36575025
Thanks.  That works.

There are some additional columns I want added to the SELECT so I can identify these rows in the result set.  But that means I have to also add them to the GROUP BY.  Will those new columns affect the result set?  Or, is there a way to output additional attributes without affecting the result set?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36575378
>>Will those new columns affect the result set? <<
Yes, it could.
>>Or, is there a way to output additional attributes without affecting the result set? <<
Yes you can, but it depends on your requirements.  For example, the simplest way is to add MIN or MAX to the new columns.  But there are other more sophisticated methods.
0
 

Author Comment

by:brettr
ID: 36575418
But there are other more sophisticated methods.

Can you provide some references to these?

Seems MIN and MAX would affect the result set and probably shouldn't be used for columns that should be benign (displayed only).
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
ID: 36577542
>>Can you provide some references to these?<<
There are at least a couple of ways and I am sure there are more:
You can use a CTE with ROW_NUMBER() or RANK() or DENSE_RANK() to include the "first" row in the resultset.
You can use a derived table to filter the resultset based on the values returned in that derived table.

>>Seems MIN and MAX would affect the result set and probably shouldn't be used for columns that should be benign (displayed only). <<
MIN and MAX have no effect whatsoever on the number of rows output.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

8 Experts available now in Live!

Get 1:1 Help Now