CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    SQL statement question

    I have a table with two fields ID and Name, with possible multiple entries for the same name. Here is an example:
    Code:
    ID   Name
    1    aaa
    2    bbb
    3    bbb
    4    ccc
    5    aaa
    Now, I want to know if there is possible to get with a single SELECT all the names and how many times they appear in the table:
    Code:
    aaa    2
    bbb    2
    ccc    1
    Thank you.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #2
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: SQL statement question

    Code:
    SELECT Name,COUNT(*) FROM MyTable GROUP BY Name ORDER BY Name
    Why not?

    Best regards,
    Krzemo.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SQL statement question

    So GROUP BY is the key... Thank you.

    Now, this is question part 2:
    I have tree tables, let's call them table1, table 2 and table 3:
    Code:
    TABLE1:
    ID   Name
    1    aaa
    2    bbb
    3    ccc
    
    TABLE2
    ID   Name
    1    pro1
    2    pro2
    3    pro3
    
    TABLE3
    ID   ID_TABLE1   ID_TABLE2
    1    1                   2
    2    2                   2
    3    3                   1
    I want to make a single SELECT and retrieve this:
    Code:
    [1]   aaa    pro2
    [2]   bbb    pro2
    [3]   ccc     pro1
    I tried this but it doesn't work:
    Code:
    SELECT  T1.Name, T2.Name FROM Table1 as T1, Table2 as T2, Table3 as T3
       WHERE T3.ID_Table1 = T1.ID and T3.ID_Table2 = T2.ID
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: SQL statement question

    I tried this but it doesn't work:

    Really?
    For me it works perfectly see below(MsSQL database Northwind):
    Code:
    CREATE TABLE Table1 
     (
       ID INT PRIMARY KEY
      ,Name VARCHAR(50)
     )
    GO
    CREATE TABLE Table2 
     (
       ID INT PRIMARY KEY
      ,Name VARCHAR(50)
     )
    GO
    CREATE TABLE Table3 
     (
       ID INT PRIMARY KEY
      ,ID_Table1 INT REFERENCES Table1(ID)
      ,ID_Table2 INT REFERENCES Table2(ID)
     )
    GO
    INSERT INTO Table1 VALUES( 1,'aaa')
    INSERT INTO Table1 VALUES( 2, 'bbb')
    INSERT INTO Table1 VALUES( 3 ,'ccc')
    INSERT INTO Table2 VALUES( 1,'pro1')
    INSERT INTO Table2 VALUES( 2,'pro2')
    INSERT INTO Table2 VALUES( 3,'pro3')
    INSERT INTO Table3 VALUES(1 , 1  , 2)
    INSERT INTO Table3 VALUES(2 , 2  ,  2)
    INSERT INTO Table3 VALUES(3 , 3  , 1)
    GO
    SELECT  T1.Name, T2.Name FROM Table1 as T1, Table2 as T2, Table3 as T3
       WHERE T3.ID_Table1 = T1.ID and T3.ID_Table2 = T2.ID
    Output:
    Code:
     
    Name Name
    aaa pro2
    bbb pro2
    ccc pro1
    (3 row(s) affected)
    Best regards,
    Krzemo.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SQL statement question

    OK. My mistake. That works, but actually I want something else. I'm sorry for posting the wrong thing.

    Here is what I actually want (this is much more complicated):
    Code:
    TABLE1:
    ID   Name
    1    aaa
    2    bbb
    3    ccc
    
    TABLE2
    ID   Name
    1    pro1
    2    pro2
    3    pro3
    
    TABLE3
    ID   ID_TABLE1   ID_TABLE2
    1    1                   1
    2    1                   2
    3    1                   3
    4    2                   1
    Can I do something like this:
    Code:
    aaa        pro1,pro2,pro3
    bbb        pro1
    This one means a string "pro1,pro2,pro3", but there can be another delimiter than ','.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: SQL statement question

    Code:
    CREATE FUNCTION dbo.FN_GetConcatedNames(@i_ID INT) RETURNS VARCHAR(4000)
    AS
    BEGIN
    DECLARE
    	@_Str VARCHAR(4000)
    SELECT @_Str=''
     
    SELECT @_Str=@_Str+IsNull(','+t2.[Name],'') 
    	 FROM Table3 t3,Table2 t2
    	 WHERE t3.ID_Table2=t2.ID AND T3.ID_Table1=@i_ID
     
    RETURN SUBSTRING(@_Str,2,4000)
    END
     
    GO
     
    SELECT T1.Name,dbo.FN_GetConcatedNames(T1.ID) Names2
    FROM Table1 T1
    Hope it helps.

  7. #7
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: SQL statement question

    Are you talking about a cross tab (or maybe its called pivot) query?
    JP

    Please remember to rate all postings.

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SQL statement question

    Quote Originally Posted by Krzemo
    Code:
    CREATE FUNCTION dbo.FN_GetConcatedNames(@i_ID INT) RETURNS VARCHAR(4000)
    AS
    BEGIN
    DECLARE
    	@_Str VARCHAR(4000)
    SELECT @_Str=''
     
    SELECT @_Str=@_Str+IsNull(','+t2.[Name],'') 
    	 FROM Table3 t3,Table2 t2
    	 WHERE t3.ID_Table2=t2.ID AND T3.ID_Table1=@i_ID
     
    RETURN SUBSTRING(@_Str,2,4000)
    END
     
    GO
     
    SELECT T1.Name,dbo.FN_GetConcatedNames(T1.ID) Names2
    FROM Table1 T1
    Hope it helps.
    I'm not sure... I mean, I was hoping for something easier. I have this app in VC++ and I'm using CDaoDatabase and CDaoRecordset and I'm not sure how can use your code.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SQL statement question

    Quote Originally Posted by jp140768
    Are you talking about a cross tab (or maybe its called pivot) query?
    Sorry, I don't remember what a cross table is...

    I have three tables, one with movies, one with actors and one that links actors to movies, since more than one actor plays in a movie. So basically what I want is select a movie name from table 1 and all the actors that plays in it from table 2 but the result be a single record:
    Code:
    movie1         actor1, actor2, actor3
    movie2         actor5, actor100
    Hope you got the picture...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Nov 2004
    Posts
    5

    Re: SQL statement question

    poti face in felul asta:

    select name, count(name)as number from MyTable group by name
    toate bune, all the best
    Cornelia

    Quote Originally Posted by cilu
    I have a table with two fields ID and Name, with possible multiple entries for the same name. Here is an example:
    Code:
    ID   Name
    1    aaa
    2    bbb
    3    bbb
    4    ccc
    5    aaa
    Now, I want to know if there is possible to get with a single SELECT all the names and how many times they appear in the table:
    Code:
    aaa    2
    bbb    2
    ccc    1
    Thank you.

  11. #11
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: SQL statement question

    Are You using Access (.mdb) database?

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SQL statement question

    Quote Originally Posted by Krzemo
    Are You using Access (.mdb) database?
    Yes I do.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: SQL statement question

    I set up a small Access database, with three tables, one has an example similar to an entry earlier, where we have one table, with the name listed multiple times, and we wanted to count it. This can be seen using Query 1.

    I think though the main question was in relation to having a table of films, a table of actors and then a table which linked the two together. I set these up, and then set up query 2, which will give you something close to what you're looking for, or maybe not.

    HTH
    Attached Files Attached Files
    JP

    Please remember to rate all postings.

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: SQL statement question

    Right now I can't test it, because I have Access '97 on this computer and it doesn't recognize the format, but I'll test it tonight and let you know about it tomorrow.

    PS: problem one in now solved.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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

Featured