?
Solved

Why is this SELECT invalid?

Posted on 2011-09-21
7
Medium Priority
?
242 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
[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
  • 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 1200 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 800 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

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…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 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