Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add values of two rows if certain column values match

Status
Not open for further replies.

jase2006

Technical User
Nov 17, 2006
53
GB
I have a table which contains LoginID, WC, ProjectID, Mon, Tues, Wed, ......,Sun. More than one row can have the same LoginID, WC and ProjectID and if that is the case then I want to merge these rows' value together. Can anyone show me a sample code of doing this? The Values in Mon - Sun column is either 1 or 0.
 

Try MAX() or SUM() function:
Code:
Select LoginID, WC, ProjectID
     , MAX(Mon) Mon
     , MAX(Tue) Tues
     , MAX(Wed) Wed
     , ......
     , MAX(Sun) Sun
  From MyTable
 Group By LoginID, WC, ProjectID;
-- Or --
Code:
Select LoginID, WC, ProjectID
     , SUM(Mon) Mon
     , SUM(Tue) Tues
     , SUM(Wed) Wed
     , ......
     , SUM(Sun) Sun
  From MyTable
 Group By LoginID, WC, ProjectID;
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
It's not seem to be work, I actually forgot to mention "comment" field which contain a random txt. Would that cause a problem?
 

Use MAX(comment).


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks LKBrwnDBA, it's working now. Another question came up, what if I want to concatenate the "comments" together in the new merge row. Is that possible?
 
I don't believe there is an ansi solution to that problem.

What database are you using, because there almost surely is a solution in that particular SQL Dialect.

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Check out this thread: thread183-1159740

Hope it helps,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top