Solved

SQL Select to merge multiple rows into one

Posted on 2011-09-28
4
194 Views
Last Modified: 2012-06-21
I have a view that return multiple records for the same account. I would like to return a single record instead with all the available values in the various columns

See screen shot. sleiman-505212.flv
0
Comment
Question by:sleiman
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 36715669
can you share your query ?

Try some thing like this on your query


Select X.Id,X.AccountName, X.Db, Max(X.MEStarts),Max(X.MEEnds),Max(X.FYStarts),Max(X.FYEnds),Max(X.CYStarts),Max(X.CYEnds) From
(
Your Query here

) X
Group by X.Id,X.AccountName, X.Db
0
 
LVL 3

Expert Comment

by:Srm74
ID: 36715776
You could do something like this..

select
	(select sum(value1) from TableA where ....)  as 'SumValue1',
        (select sum(value2) from TableA where ....)  as 'SumValue2',
        (select sum(value3) from TableA where ....)  as 'SumValue3' 

Open in new window

0
 
LVL 3

Expert Comment

by:Srm74
ID: 36715803
Just saw your scrrenshot.. :-)
Try this..

select
	Accountname,
	Min(Db),
	Sum(MEStarts) as 'MEStarts',
	Sum(MEEnds) as 'MEEnds',
	Sum(FYStarts) as 'FYStarts',
	Sum(FYEnds) as 'FYEnds',
	Sum(CYStarts) as 'CYStarts',
	Sum(CYEnds) as 'CYEnds'
From
	tableX
where
	Accountname = 'XYZ'
Group by
	Accountname

Open in new window

0
 

Author Closing Comment

by:sleiman
ID: 36805225
Perfect!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question