Solved

datediff and group by

Posted on 2011-02-11
3
321 Views
Last Modified: 2012-05-11
Hi experts,

The attached stored proc is supposed to create a resultset that contains two columns, one showing the number of orders and the second showing the timetaken.

However, it errors stating that timetaken is an invalid column name!?

What am I doing wrong?

Help appreciated.
SELECT COUNT(o.orders_id) AS ordercount, (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp)) AS timetaken
FROM orders O, dbo.tblPS_orders_status_history OSH WHERE
o.orders_status = 4
AND o.paymentMethodID = 1
AND o.orders_statusID = OSH.orders_status_history_id
AND DATEDIFF(DAY, o.date_purchased, GETDATE()) < 31
GROUP BY timetaken
ORDER BY timetaken

Open in new window

0
Comment
Question by:jammy-d0dger
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34872140
indeed. while you can ORDER BY an alias, you cannot GROUP BY ...
SELECT COUNT(o.orders_id) AS ordercount, (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp)) AS timetaken
FROM orders O, dbo.tblPS_orders_status_history OSH WHERE
o.orders_status = 4
AND o.paymentMethodID = 1
AND o.orders_statusID = OSH.orders_status_history_id
AND DATEDIFF(DAY, o.date_purchased, GETDATE()) < 31
GROUP BY  (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp))
ORDER BY timetaken

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34872154
Alias cannot be used in the same query
Instead try its values

SELECT COUNT(o.orders_id) AS ordercount, (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp)) AS timetaken
FROM orders O, dbo.tblPS_orders_status_history OSH WHERE
o.orders_status = 4
AND o.paymentMethodID = 1
AND o.orders_statusID = OSH.orders_status_history_id
AND DATEDIFF(DAY, o.date_purchased, GETDATE()) < 31
GROUP BY (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp))
ORDER BY (DATEDIFF(DAY,  o.date_purchased, osh.orders_status_history_timestamp))
0
 

Author Closing Comment

by:jammy-d0dger
ID: 34872196
Perfect, thanks Angellll.  Seems pretty lame that you can use it for order by but not group by?  Thanks for the work-around, working perfectly.
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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

820 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