Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to merge one column values with same primary key Expand / Collapse
Author
Message
Posted Wednesday, May 8, 2013 2:49 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, December 7, 2014 7:04 PM
Points: 142, Visits: 284
Hello,


Please help me here with below Query

SELECT AcctNo,
PlanCount = COUNT(Model_no),
PlanName = Case COUNT(Model_no) When 1 then a.Model_no when 0 then NULL else 'Multi Model' end
FROM dbo.cpDetails D LEFT JOIN TBACTDetails A ON D.AcctNo = A.Social
WHERE model_NAME IS NULL
GROUP BY AcctNo --, Case when COUNT(Model_no)= 1 then a.Model_no else 'Multi Model' end
ORDER BY 2 DESC,1

i got below error

---Column 'TBACTDetails.Model_no' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

then i placed max(a.model) it is worked, i do get results but the problem

First Question is is is it correct way of doing or any otherway without use Max() function ?

Second Question is along with abouve results, if COUNT(model_no) > 1 then (i.e., this model has more than one model name values in model name columns) how can i show both values in one column (seperated by ; )


for example

Model_No, Model_Name
100, Zirrard
100, Modached


in the results i would like to get as
100,2,'Multi Model','Zirrard; ModaChed'


Please assist me how to get this new column ('Zirrard; ModaChed') where count(*) > 1

Thanks in advance
MIlan
Post #1450803
Posted Wednesday, May 8, 2013 3:11 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, December 7, 2014 7:04 PM
Points: 142, Visits: 284
Could Sombody please assist me

I Greatful to you

thank you
Post #1450814
Posted Wednesday, May 8, 2013 3:23 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:05 PM
Points: 13,327, Visits: 12,820
Something like this should work.

Notice how I posted ddl and sample data in a consumable format? That is something you should do in the future. It makes it easy for us volunteers to work on your issue. It will also help eliminate the need for you to beg us for help.

create table #Something
(
Model_No int,
ModelName varchar(25)
)

insert #Something
select 100, 'Zirrard' union all
select 100, 'Modached'

select Model_No, COUNT(*), case when COUNT(*) > 1 then 'Multi Model' else 'Single Model' end as ModelType,
STUFF((select ', ' + ModelName
from #Something s2
where s2.Model_No = s.Model_No
order by s2.ModelName
for XML PATH('')), 1, 1, ' ')
from #Something s
group by Model_No

drop table #Something



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1450818
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse