September 7, 2006 at 8:53 am
SET NOCOUNT ON
DECLARE @MyTable TABLE
(
[ID] INT,
[Name] VARCHAR(25)
)
INSERT @MyTable
SELECT 1, 'SomeName1_1' UNION ALL
SELECT 1, 'SomeName1_2' UNION ALL
SELECT 2, 'SomeName2_1' UNION ALL
SELECT 2, 'SomeName2_2' UNION ALL
SELECT 2, 'SomeName2_2' UNION ALL
SELECT 2, 'SomeName2_2' UNION ALL
SELECT 2, 'SomeName2_3' UNION ALL
SELECT 3, 'SomeName3_2' UNION ALL
SELECT 3, 'SomeName3_1' UNION ALL
SELECT 3, 'SomeName3_3' UNION ALL
SELECT 4, 'SomeName4_2' UNION ALL
SELECT 4, 'SomeName4_3' UNION ALL
SELECT 4, 'SomeName4_3' UNION ALL
SELECT 5, 'SomeName5_1' UNION ALL
SELECT 6, 'SomeName6_2' UNION ALL
SELECT 6, 'SomeName6_3'
/*
I had to do it with loop. Is it possible to get it set based.
Result needed Each ID should have only one row.
Name Should be the name which has MAX(COUNT(Name)) GROUP BY ID
IF MAX(COUNT(Name)) Group By id is same MIN(Name) is needed
Expected result
ID NAME
1 SomeName1_1
2 SomeName2_2
3 SomeName3_1
4 SomeName4_3
5 SomeName5_1
6 SomeName6_2
*/
Regards,
gova
September 7, 2006 at 9:25 am
I don't believe this so for no one even read this post. No one like Remi is hanging around any more??
I did it like this. I would like to know if there is an easier way.
SELECT [ID], [Name], NameCount
INTO
#ErrNameCount
FROM
(
SELECT [ID], [Name], COUNT([Name]) NameCount
FROM
@MyTable
GROUP BY [ID], [Name]) A
SELECT A.[ID], MIN(A.[Name])
FROM
#ErrNameCount A
JOIN
(
SELECT [ID], MAX(NameCount) NameCount
FROM
#ErrNameCount
GROUP BY [ID] ) B
ON
A.[ID] = B.[ID]
AND A.NameCount = B.NameCount
GROUP BY A.[ID]
Regards,
gova
September 11, 2006 at 11:53 am
So now I'm supposed to answer all messages under 10 minutes .
I don't think there's a much better way to do this unless you have access to CTE.
September 11, 2006 at 12:35 pm
same result using CTE's
WITH ACTE ([ID],[Name],[NameCount]) AS
(
SELECT [ID], [Name], COUNT([Name]) NameCount FROM MyTable1 GROUP BY [ID], [Name]
 
SELECT B.[ID],MIN([NAME]) AS [NM]
FROM ACTE A INNER JOIN
(SELECT ID,MAX([NameCount]) AS MXCNT FROM ACTE GROUP BY ID ) B
ON A.ID=B.ID AND A.NAMECOUNT=B.MXCNT
GROUP BY B.ID
September 11, 2006 at 1:11 pm
I have to wait for about an year (hope) to use CTE.
Remi 10 minutes took 4 days for you. I was wondering at that point no one even read the post.
Regards,
gova
September 11, 2006 at 1:19 pm
"
I don't believe this so for no one even read this post. No one like Remi is hanging around any more??
"
Was reffering to this... Took only 30 mins to answer your own post. Not always enough time to figure it out... even if you read it just as it is posted.
September 11, 2006 at 5:50 pm
JUST in case you need it as a single query (In the absence of CTE's, I do like the temp table solution better which is why I didn't answer previously )...
SET NOCOUNT ON
DECLARE @MyTable TABLE
(
[ID] INT,
[Name] VARCHAR(25)
)
INSERT @MyTable
SELECT 1, 'SomeName1_1' UNION ALL
SELECT 1, 'SomeName1_2' UNION ALL
SELECT 2, 'SomeName2_1' UNION ALL
SELECT 2, 'SomeName2_2' UNION ALL
SELECT 2, 'SomeName2_2' UNION ALL
SELECT 2, 'SomeName2_2' UNION ALL
SELECT 2, 'SomeName2_3' UNION ALL
SELECT 3, 'SomeName3_2' UNION ALL
SELECT 3, 'SomeName3_1' UNION ALL
SELECT 3, 'SomeName3_3' UNION ALL
SELECT 4, 'SomeName4_2' UNION ALL
SELECT 4, 'SomeName4_3' UNION ALL
SELECT 4, 'SomeName4_3' UNION ALL
SELECT 5, 'SomeName5_1' UNION ALL
SELECT 6, 'SomeName6_2' UNION ALL
SELECT 6, 'SomeName6_3'
SELECT c1.ID,MIN(c1.Name) AS Name,c1.NameCount
FROM (
SELECT ID,Name,COUNT(*) AS NameCount
FROM @MyTable
GROUP BY ID,Name
)c1
,
(
SELECT m1.ID, MAX(m1.NameCount) AS MaxNameCount
FROM (SELECT ID,Name,COUNT(*) AS NameCount
FROM @MyTable
GROUP BY ID,Name
)m1
GROUP BY m1.ID
)m2
WHERE c1.ID = m2.ID
AND c1.NameCount = m2.MaxNameCount
GROUP BY c1.ID,c1.NameCount
ORDER BY c1.ID,c1.NameCount
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply