Solved

Why is this SELECT invalid?

Posted on 2011-09-21
7
235 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
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 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dataset not reading table data 12 46
Rebuild index results in duplicate key violation 9 42
if and else in stored procedure 19 42
2 comma seperated list - SQL Server 12 38
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 …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

16 Experts available now in Live!

Get 1:1 Help Now