Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trouble with GROUP BY and ORDER BY
Message
 
 
To
28/04/2005 08:30:36
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01009167
Message ID:
01009171
Views:
16
Assuming that 'jobinfo.costdate' and 'jobinfo.starttime' are the same for all records in a group, you can wrap them into MAX() or MIN() functions.

>In the following query, I need to order by costdate desc and starttime desc, but I only want to group by ljob and id, which you cannot do because starttime and costdate are in the SELECT
>DECLARE @tcEmpno char(9)
>SET @tcEmpno = '000000298'
>SELECT jobinfo.ljob, COUNT(jobinfo.ljob) AS Weight, jobinfo.costdate, jobinfo.starttime,
>		CASE WHEN bbjthead.id = '' THEN '(No-ID)' ELSE bbjthead.id END AS ID FROM
>			(
>
>				SELECT ljob, costdate, starttime FROM bbjobcst
>				WHERE ljob <> ' ' AND ljob <> 'None ' 
AND costdate >= GETDATE() - 15 AND empno = @tcEmpno
>				--ORDER BY costdate desc, starttime desc
>				
>				UNION ALL
>				
>				SELECT ljob, costdate, starttime
>				FROM bbmatdc
>				WHERE ljob <> ' ' AND ljob <> 'None ' 
AND
>				costdate >= GETDATE() - 15 AND empno = @tcEmpno
>				--ORDER BY costdate desc, starttime desc
>
>				UNION ALL
>				
>				SELECT ljob, costdate, starttime
>				FROM bbrolldc
>				WHERE ljob <> '' AND ljob <> 'None ' 
AND costdate >= GETDATE() - 15 AND empno = @tcEmpno
>				--ORDER BY costdate desc, starttime desc
>
>			)jobinfo
>
>	   INNER JOIN BBJTHEAD ON BBJTHEAD.LJOB = jobinfo.ljob
>GROUP BY jobinfo.ljob, BBJTHEAD.ID, jobinfo.costdate, jobinfo.starttime
>ORDER BY jobinfo.costdate desc, jobinfo.starttime desc
>Does anyone have any ideas on how I can accomplish this???
>
>Thanks in advance.
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform