| Author |
Topic |
|
craigmac999
Starting Member
14 Posts |
Posted - 2007-12-31 : 21:02:10
|
| Hi i have a sql sp as below, and i cant get it to group by the GroupMessageID, i want to show all records and group them by GroupMessageID, but i only want to show the cost, date etc of the first record.so really i need to show all records and only show the first record in the GroupMessageIDSELECT SUM(Cost)AS TotCost, Date, ID, RCODE, MessageText, GroupMessageIDFROM Records WHERE GroupMessageID <> 0GROUP BY GroupMessageID, Cost, Date, ID, RCODE, MessageTextORDER BY Date DESC |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-01 : 01:23:01
|
| Can you please post some sample data & expected o/p to illustrate what you are trying to achieve? |
 |
|
|
craigmac999
Starting Member
14 Posts |
Posted - 2008-01-01 : 06:07:10
|
| ok DATABASE ROWSID, Cost, Date, MessageText, GroupMessageID1, 2, 12/12/2007 12:47:01, TEST MESSGE, 22442, 2, 12/12/2007 12:47:02, TEST MESSGE, 22443, 2, 12/12/2007 12:47:03, TEST MESSGE, 22444, 2, 12/12/2007 12:47:04, TEST MESSGE, 22445, 1, 15/12/2007 12:47:50, NEXT MESSGE, 22456, 1, 15/12/2007 12:47:51, NEXT MESSGE, 2245So what i want to show is1, 2, 12/12/2007 12:47:01, TEST MESSGE, 22445, 2, 15/12/2007 12:47:50, NEXT MESSGE, 2245 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-01 : 08:38:46
|
If SQL 2005:-SELECT tmp.ID, tmp.Cost, tmp.Date, tmp.MessageText, tmp.GroupMessageIDFROM(SELECT ROW_NUMBER() OVER (PARTITION BY GroupMessageID ORDER BY ID) AS 'RowNo',ID, Cost, Date, MessageText, GroupMessageIDFROM Records)tmpWHERE tmp.RowNo=1 In SQL 2000:-SELECT tmp.ID, tmp.Cost, tmp.Date, tmp.MessageText, tmp.GroupMessageIDFROM( SELECT t1.ID, t1.Cost, t1.Date, t1.MessageText, t1.GroupMessageID, ( SELECT COUNT(*) + 1 FROM Records WHERE GroupMessageID=t1.GroupMessageID AND ID <t1.ID ) AS 'RowNo' FROM Records t1)tmpWHERE tmp.RowNo=1 |
 |
|
|
kiruthika
Yak Posting Veteran
67 Posts |
Posted - 2008-01-02 : 02:50:02
|
| Hi! Try thisselect id,d.cost,d.date,d.messagetext,d.groupid from grouprows as e join (select cost,min(date) as [date],messagetext,groupid from grouprows group by cost,messagetext,groupid) as d on e.[date]=d.[date]I think this will help Youkiruthika!http://www.ictned.eu |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-02 : 03:54:36
|
quote: Originally posted by craigmac999 ok DATABASE ROWSID, Cost, Date, MessageText, GroupMessageID1, 2, 12/12/2007 12:47:01, TEST MESSGE, 22442, 2, 12/12/2007 12:47:02, TEST MESSGE, 22443, 2, 12/12/2007 12:47:03, TEST MESSGE, 22444, 2, 12/12/2007 12:47:04, TEST MESSGE, 22445, 1, 15/12/2007 12:47:50, NEXT MESSGE, 22456, 1, 15/12/2007 12:47:51, NEXT MESSGE, 2245So what i want to show is1, 2, 12/12/2007 12:47:01, TEST MESSGE, 22445, 2, 15/12/2007 12:47:50, NEXT MESSGE, 2245
I think you need to explain why uou want to get 2 for id 5?MadhivananFailing to plan is Planning to fail |
 |
|
|
craigmac999
Starting Member
14 Posts |
Posted - 2008-01-02 : 14:53:40
|
| Hi visakh16that did work, but just getting an error now because of the GroupMessageID as a varchar and i am getting a error of can not convert to int, any ideas why this is?my database column is a varchar ?i am using SELECT tmp.ID, tmp.Cost, tmp.DateofSM, tmp.MessageText, tmp.GroupMessageID,tmp.ClubID,tmp.RCODEFROM( SELECT t1.ID, t1.Cost, t1.DateofSM, t1.ClubID, t1.MessageText, t1.GroupMessageID, t1.RCODE, ( SELECT COUNT(*) + 1 FROM SMS_Records WHERE GroupMessageID=t1.GroupMessageID AND ID <t1.ID ) AS 'RowNo' FROM SMS_Records t1)tmpWHERE tmp.RowNo=1 AND GroupMessageID <> 0AND DATENAME(month, DateOfSM)= @DateAND ClubID = @ClubID |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-02 : 23:21:55
|
quote: Originally posted by craigmac999 Hi visakh16that did work, but just getting an error now because of the GroupMessageID as a varchar and i am getting a error of can not convert to int, any ideas why this is?my database column is a varchar ?i am using SELECT tmp.ID, tmp.Cost, tmp.DateofSM, tmp.MessageText, tmp.GroupMessageID,tmp.ClubID,tmp.RCODEFROM( SELECT t1.ID, t1.Cost, t1.DateofSM, t1.ClubID, t1.MessageText, t1.GroupMessageID, t1.RCODE, ( SELECT COUNT(*) + 1 FROM SMS_Records WHERE GroupMessageID=t1.GroupMessageID AND ID <t1.ID ) AS 'RowNo' FROM SMS_Records t1)tmpWHERE tmp.RowNo=1 AND CAST(GroupMessageID AS int) <> 0AND DATENAME(month, DateOfSM)= @DateAND ClubID = @ClubID
Are you telling GroupMessageID is of type varchar?then change like above.. |
 |
|
|
|
|
|