INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Just a quick note to say, "THANKS!" for these forums...The site is very well layed out and easy to use. Thanks for bringing us together - we need each other."

Geography

Where in the world do Tek-Tips members come from?
Tinkerers (TechnicalUser)
6 Nov 07 22:06
Hi,

I have a table like this:

ColumnA | ColumnB
C            1
C            1
C            1
C            1
C            3
C            2
D            6
D            6
D            6
D            5

I need to write a query that will show only the values that have the majority of the rows. Example C1, D6, but NOT C2,C3 or D5.
This is much tougher than I thought. The number of rows can differ.

Thanks for any help,
Paul
TheSQL (Programmer)
6 Nov 07 23:32
How do you define "the majority of rows"?

You could start with

SELECT ColumnA, ColumnB, COUNT(*) AS NbrOfRows
FROM MyTable
GROUP BY ColumnA, ColumnB
ORDER BY Count(*) DESC
PerlyGates (Programmer)
7 Nov 07 11:36
I thought someone would have a slick answer to this. This is the only thing I could come up which is a bit convoluted.


CODE

DECLARE @tv TABLE (
   ColumnA char(1),
   ColumnB int
)

INSERT INTO @tv
SELECT 'C',            1
UNION ALL
SELECT 'C',            1
UNION ALL
SELECT 'C',            1
UNION ALL
SELECT 'C',            1
UNION ALL
SELECT 'C',            2
UNION ALL
SELECT 'C',            2
UNION ALL
SELECT 'C',            3
UNION ALL
SELECT 'C',            2
UNION ALL
SELECT 'D',            6
UNION ALL
SELECT 'D',            6
UNION ALL
SELECT 'D',            6
UNION ALL
SELECT 'D',            5
UNION ALL
SELECT 'E',            1
UNION ALL
SELECT 'E',            8
UNION ALL
SELECT 'E',            1
UNION ALL
SELECT 'E',            8

      

/*
   The following will also bring back duplicate
   ColumnA's i.e. if there are the same number of
   ColumnB's per ColumnA - E8 and E1 in the
   example data above (2 occurences each).  
*/

 SELECT t1.ColumnA AS columnA
       ,t1.ColumnB AS columnB
       ,t1.myCount AS finalCount
   FROM (SELECT COUNT(*) AS myCount,
                ColumnA,
                ColumnB
           FROM @tv
       GROUP BY ColumnA,ColumnB ) t1
INNER JOIN
        ( SELECT MAX( in1Count ) AS myCount
                ,ColumnA
            FROM( SELECT COUNT(*) AS in1Count,
                         ColumnA,
                         ColumnB
                    FROM @tv
                GROUP BY ColumnA,ColumnB ) in1
          GROUP BY ColumnA ) t2
   ON t1.ColumnA              = t2.ColumnA
  AND t1.myCount              = t2.myCount



/*
   The following will only bring back unique ColumnA
   values.   
*/


 SELECT t1.ColumnA columnA
       ,MIN(t1.ColumnB) columnB
       ,MAX(t1.myCount) finalCount
   FROM (SELECT COUNT(*) AS myCount,
                ColumnA,
                ColumnB
           FROM @tv
       GROUP BY ColumnA,ColumnB ) t1
INNER JOIN
        ( SELECT MAX( in1Count ) AS myCount
                ,ColumnA
            FROM( SELECT COUNT(*) AS in1Count,
                         ColumnA,
                         ColumnB
                    FROM @tv
                GROUP BY ColumnA,ColumnB ) in1
          GROUP BY ColumnA ) t2
   ON t1.ColumnA              = t2.ColumnA
  AND t1.myCount              = t2.myCount
GROUP BY t1.ColumnA
Helpful Member!SQLSister (Programmer)
7 Nov 07 14:00
It's not pretty but it works

CODE

SELECT ColumnA, ColumnB, COUNT(*) AS NbrOfRows into #temp
FROM table
GROUP BY ColumnA, ColumnB

select t.* from #temp t join
(select columna, Max(NbrOfRows) as NBr from #temp group by columna) a
on a.columna = t.columnA and a.nbr = t.NbrOfRows

Note this will give you two (or more) rows for a value in columnA if they have the same count. You would need some other way of selecting the one you want in that case if you only want one row. You could do it without the temp table but then you get a derived table inside a derived table and it gets harder to follow.

"NOTHING is more important in a database than integrity." ESquared

TheSQL (Programmer)
7 Nov 07 15:56
How about:
--set up the table to test
CREATE TABLE MYTABLE
(ColumnA char(1), ColumnB int)

INSERT MyTable VALUES ('C',1)
INSERT MyTable VALUES ('C',1)
INSERT MyTable VALUES ('C',1)
INSERT MyTable VALUES ('C',1)
INSERT MyTable VALUES ('C',3)
INSERT MyTable VALUES ('C',2)
INSERT MyTable VALUES ('D',6)
INSERT MyTable VALUES ('D',6)
INSERT MyTable VALUES ('D',6)
INSERT MyTable VALUES ('D',5)

GO
--actual script
SELECT a.ColumnA, a.ColumnB
FROM (SELECT ColumnA, ColumnB, Count(*) AS NbrOfRows
      FROM MyTable
      GROUP BY ColumnA, ColumnB) a
JOIN (SELECT MAX(x.NbrOfRows) AS MaxRows
      FROM
    (SELECT ColumnA, ColumnB, Count(*) AS NbrOfRows
          FROM MyTable
          GROUP BY ColumnA, ColumnB) AS x) as b ON a.NbrOfRows = b.MaxRows
Tinkerers (TechnicalUser)
7 Nov 07 17:22
Thankyou to all who went after this brain teaser. Wow, for such a simple thing, it turned into a quite complex query. I did use the version from SQLSister, as it was the shortest and easiestsmile.
SELECT ColumnA, ColumnB, COUNT(*) AS NbrOfRows into #temp
FROM table
GROUP BY ColumnA, ColumnB

select t.* from #temp t join
(select columna, Max(NbrOfRows) as NBr from #temp group by columna) a
on a.columna = t.columnA and a.nbr = t.NbrOfRows

Start A New Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

LINK TO THIS FORUM!
(Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum)
TITLE: Microsoft SQL Server: Programming Forum at Tek-Tips
URL: http://www.tek-tips.com/threadminder.cfm?pid=183
DESCRIPTION: Microsoft SQL Server: Programming technical support forum and mutual help system for computer professionals. Selling and recruiting forbidden.