1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

SQL COUNT and Inner Join Issue

Discussion in 'Databases' started by suraj_ajax, Aug 13, 2008.

  1. #1
    Hi

    I have two tables

    1. Members - which contains all records of members including city_id
    2. city_list - which contains city name and city_id

    Now i need to generate report as below

    List of city with numbers of members

    This will will let me know how popular is my site in citywise .


    --------------------
    I had tried this sql query but no results

    select city_id,count(*) ,city_list.city_name from members inner join city_list on city_list.city_id=members.city_id group by city_id order by count(*) desc


    -----------------
    My database is in MS SQL Server 2003


    Thanks in advance

    Suraj jain
     
    suraj_ajax, Aug 13, 2008 IP
  2. suraj_ajax

    suraj_ajax Peon

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ohh .. i myself got the solution ... here is the exact query ..

    ==========================
    select city_name, (select count(members.city_id) from members where city_list.city_id=members.city_id) as tagcount from city_list order by tagcount desc
    ==========================

    Do lemme .. if there is still a better way to do it



    Thanks for reading ..
    bye
     
    suraj_ajax, Aug 13, 2008 IP
  3. net-split

    net-split Peon

    Messages:
    29
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Shouldn't this work (you must include all fields in the group by).

    SELECT C.city_id, C.city_name, Count(1) FROM city_list C
    JOIN members M ON M.city_id = C.city_id
    GROUP BY C.city_id, C.city_name ORDER BY Count(1) DESC
     
    net-split, Aug 18, 2008 IP
  4. gramyla

    gramyla Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4


    what is the meaning of tagcount here could you pls tell.I always mess in this type of generall problem.
     
    gramyla, Aug 19, 2008 IP
  5. Erriku Saki

    Erriku Saki Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thx for this infor too it works well, as im not sure about the tag count
     
    Erriku Saki, Jul 11, 2009 IP
  6. Traffic-Bug

    Traffic-Bug Active Member

    Messages:
    1,866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    80
    #6
    tagcount is alias name.
     
    Traffic-Bug, Jul 11, 2009 IP