Solved

datediff and group by

Posted on 2011-02-11
3
313 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 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

10 Experts available now in Live!

Get 1:1 Help Now