May 8, 2013 at 2:49 pm
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
May 8, 2013 at 3:11 pm
Could Sombody please assist me
I Greatful to you
thank you
May 8, 2013 at 3:23 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply