Solved

datediff and group by

Posted on 2011-02-11
3
326 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
[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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running total between 2 sql tables in Sql 6 50
SQL Server Agent Job Error 13 83
T-SQL: Wrong Result 7 39
Populate fields in group based on first record in group 4 16
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

752 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