Results 1 to 11 of 11

Thread: [2005] sql question

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Question [2005] sql question

    i've got a database with 12 tables, with about 1200 items in each table.
    there are about 300 distinct "areas" in the database.

    i can get a list of these distinct areas using the distinct keyword.

    how can i get a list of the number of times each of these distinct areas are listed without querying each distinct area?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] sql question

    SQL questions belong in the Database Development forum, not VB.NET. Just because this query will be executed from a VB.NET application doesn't make it a VB.NET question.
    Code:
    SELECT Name, COUNT(Name) FROM Area GROUP BY Name
    I'm no database guru but I'm pretty sure that should work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] sql question

    it would be something more like

    Code:
    select area, count(area) from month1 group by area
    the problem is theres 1200 records in each table, with approx 300 different areas. i don't want to have to iterate through 300*12 tables. i'll give it a try

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] sql question

    What's an "area"?

    You'll have to be more specific about your table schema.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] sql question

    Quote Originally Posted by .paul.
    the problem is theres 1200 records in each table, with approx 300 different areas. i don't want to have to iterate through 300*12 tables.
    You're not. The beauty of SQL is that it's declarative. You don't have to care how it works, it just does.

    In any case, 1200 records is nothing, to any half-capable DBMS at least.

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] sql question

    an "area" is 1 of the columns (fields) in the table.
    there are 12 tables in the database

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] sql question

    i've been thinking about this

    Code:
    SELECT Name, COUNT(Name) FROM Area GROUP BY Name
    wouldn't i have to do 300 querys per table?

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] sql question

    If you ever have to do more than one or two queries to extract the required data something's seriously wrong with your table structure.

    Run the query that J provided and see if that works. If it doesn't, describe your table structure in detail, preferably with a diagram and example record, and example of the result you want from the query.

  9. #9

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] sql question

    its a very simple database. its not relational, it has no links.
    the 300 distinct areas are towns.
    out of the 1200 records in a table, i need to know how many of each town there are. i'm not sure how to use that sql statement

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] sql question

    How do you use any SQL query? You execute it and get the result set either via a DataReader or use a DataAdapter to populate a DataTable. Using my original query you'd get two columns: one containing the names and the other containing the number of times that name appears in the table.

    Having said that, I'm a bit concerned by this:
    Code:
    select area, count(area) from month1 group by area
    You don't have a different table for each month do you?

    Also, you should give the second column a name, so I'd change my query from before to something like this:
    Code:
    SELECT Name, COUNT(Name) AS [Count] FROM Area GROUP BY Name
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] sql question

    Quote Originally Posted by .paul.
    its a very simple database. its not relational, it has no links.


    Non-relational databases are the most complex to manage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width