Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 GROUP BY ORDER BY problem

Author  Topic 

matru
Starting Member

4 Posts

Posted - 2009-01-21 : 16:37:08
Hello,

I have to got

February 2009

January 2009 & Date With Desc But there is problem with

Please Anyone Help

SELECT iNewsId ,DATE_FORMAT(dDate,'%M %Y') as month_yy FROM news WHERE eStatus = 'Active'
GROUP BY month_yy ORDER BY dDate DESC

I got the approximete result but not get iNewsId According to Date Desc.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-21 : 16:40:51
You are GROUPing by dDate so what do you want to do with iNewsID column?
There must be some kind of aggregation involved.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

matru
Starting Member

4 Posts

Posted - 2009-01-21 : 17:19:19
To get iNewsId According to above query.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-21 : 17:25:27
From this sample data
DECLARE	@Sample TABLE
(
iNewsID INT,
dDate DATETIME,
eStatus VARCHAR(20)
)

INSERT @Sample
(
iNewsID,
dDate,
eDate
)
SELECT 1, '2009-01-01', 'Active' UNION ALL
SELECT 2, '2009-01-31', 'Active'
what is your expected output?


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

matru
Starting Member

4 Posts

Posted - 2009-01-21 : 18:44:39
Please Run this query below

SELECT dDate FROM news WHERE eStatus = 'Active' GROUP BY DATE_FORMAT(dDate,'%y %m') ORDER BY dDate DESC

I got Wrong value.Please Suggrst.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-21 : 18:53:49
What. Is. Your. Expected. Output. From. The. Sample. Data. I. Provided. In. My. Previous. Post?
I. Can't. Access. Your. Database. So. We. Have. To. Use. Sample. Data. Instead.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

matru
Starting Member

4 Posts

Posted - 2009-01-21 : 19:31:54
Ok it is solved. Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-22 : 01:24:36
Great!



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -