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

Author  Topic 

craigmacca
Posting Yak Master

142 Posts

Posted - 2008-01-22 : 15:05:15
Hi just having trouble with a group clause

i want to select all from my table but group the selection by a single column which is SchGroupView

so if SchGroupView contains 10 rows with a value of 4478 then i only want to show the first record.

any ideas how i can do this?

SELECT
SchGroupView, SchSMS_Date, SchSMS_Time, SchProcessID, SchMessageText, SchMessageTo, SchMessageFrom, SchMessageClickaID,
SchWay, SchServerDateTime

FROM Scheduled
GROUP BY SchGroupView

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-22 : 15:10:54
What would you want your output to be from the following dataset?

SchGroupView SchSMS_Date SchSMS_Time
4478 20080101 09:14:42
4478 20080102 13:29:01
4478 20080102 16:57:33
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 23:13:12
quote:
Originally posted by craigmacca

Hi just having trouble with a group clause

i want to select all from my table but group the selection by a single column which is SchGroupView

so if SchGroupView contains 10 rows with a value of 4478 then i only want to show the first record.

any ideas how i can do this?

SELECT
SchGroupView, SchSMS_Date, SchSMS_Time, SchProcessID, SchMessageText, SchMessageTo, SchMessageFrom, SchMessageClickaID,
SchWay, SchServerDateTime

FROM Scheduled
GROUP BY SchGroupView




SELECT 
SchGroupView, SchSMS_Date, SchSMS_Time, SchProcessID, SchMessageText, SchMessageTo, SchMessageFrom, SchMessageClickaID,
SchWay, SchServerDateTime

FROM Scheduled s
INNER JOIN (SELECT SchGroupView,MIN(SchSMS_Date) AS SchSMS_Date
FROM Scheduled GROUP BY SchGroupView)t
ON t.SchGroupView=s.SchGroupView
AND t.SchSMS_Date =s.SchSMS_Date
Go to Top of Page
   

- Advertisement -