We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

SQL counting problem

gr8life
gr8life asked
on
Medium Priority
228 Views
Last Modified: 2010-08-05
I have an Access database with one table and one column.  The column has duplicate entries.  I want to have a count of each duplicate as well as the name of the duplicate.

Database name Products
Table name Items
Column name Stuff
Column Information:

Sample Data:
Cat
Cat
Dog
Dog
Mouse

Output:
Cat, 2
Dog, 2
Mouse, 1    


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New OleDb.OleDbConnection
        Dim cmd As New OleDb.OleDbCommand
        Dim dr As OleDb.OleDbDataReader
        Dim sql As String
        Dim sw As IO.StreamWriter

        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Products.mdb"
        sql = "SELECT (Items.Stuff count(*)) FROM Items"
        With cmd
            .Connection = con
            .CommandText = sql
        End With

        sw = New System.io.StreamWriter("C:\results.txt")

        con.Open()
        dr = cmd.ExecuteReader()
        Do While dr.Read()
            sw.WriteLine(dr(0).ToString & "," & dr(1).ToString)
        Loop

        dr.Close()
        sw.Flush()
        con.Close()
        MsgBox("File complete")
    End Sub
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Select stuff, Count(*) As cnt
  From Items
  Group By stuff
  Having Count(*) > 1

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
SELECT Stuff , COUNT(Stuff ) FROM Items GROUP BY Stuff

That will give you the name, and the amount of times it is defined

Author

Commented:
I was able to make both solutions work.

Thank you very much for expertise,
Gr8life
I think we answered simultaneously :D
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.