Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to merge one column values with same primary key


how to merge one column values with same primary key

Author
Message
asita
asita
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 328
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
asita
asita
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 328
Could Sombody please assist me

I Greatful to you

thank you
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16540 Visits: 16993
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. Cool


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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search