Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts

Consider this table result:
Message_ID   Contact_ID
------------------------
    1            2
    2            5
    2            1
    1            2
    2            1

Now I want the below view:
Message_ID    TotalSent
------------------------
    1            2
    2            3

How can I achieve this goal?

Thank you for your help.
Posted

1 solution

Try:
SQL
SELECT Message_ID, COUNT(Message_ID) AS TotalSent FROM myTable
GROUP BY Message_ID
 
Share this answer
 
Comments
Meysam Toluie 15-Dec-13 12:58pm    
Thank you for your time.
What if I want to get the message content from the table 'Message' using inner join and also other columns from the current table?

The error message:
Column 'column1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
OriginalGriff 15-Dec-13 13:51pm    
What's the query you are using?
Meysam Toluie 15-Dec-13 15:24pm    
The below query gave me this:

Title Content RowNumber ID
----------------------------------------------------
first firstContent 1 1
first firstContent 2 4
first firstContent 3 5
second secondContent 1 9
second secondContent 2 2

But I need this:

Title Content Count
-------------------------------------------
first firstContent 3
second secondContent 2
Meysam Toluie 16-Dec-13 0:02am    
In fact I used row_number to get the maximum of it for each message.
Meysam Toluie 15-Dec-13 15:20pm    
With CTE As
( Select Row_Number() Over(Partition By [Log].Message_ID Order By [Log].ID) As RowNumber,
Message.Title,
Message.Content,
[Log].ID
From [Log]
Left Outer Join
Message
On Message.ID = [Log].Message_ID
) Select Title,
Content,
RowNumber,
ID
From CTE As A

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900