Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why is this SELECT invalid?

Posted on 2011-09-21
7
Medium Priority
?
244 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

609 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