Solved

Why is this SELECT invalid?

Posted on 2011-09-21
7
239 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:Ephraim Wangoya
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:
Ephraim Wangoya 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
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 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

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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