Results 1 to 6 of 6

Thread: SQL Syntax

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    SQL Syntax

    Hi,

    I need some help with SQL to do the following:

    Table1

    1 John Smith
    2 Jess Green

    Table2

    1 One
    1 Two
    2 Three
    2 Four


    What I need is a SQL statement which will give me the following.

    John Smith One, Two
    Jess Green Three, Four


    Is this possible with just SQL Syntax?



    Thanks in advance.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL Syntax

    Google for CROSSTAB and SQL Server, it may give you a hint on how it can be done.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Lively Member
    Join Date
    Dec 2007
    Posts
    76

    Re: SQL Syntax

    Hello all. This is a very interesting problem.
    http://msdn.microsoft.com/en-us/library/ms177410.aspx

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    Re: SQL Syntax

    Hi thanks for the reply.

    The examples above show me how to do this with a set number of fields, is it possible to do this with a undefined number.
    Last edited by Developer2007; Jan 25th, 2010 at 04:48 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    Re: SQL Syntax

    By the way it suppose to look like this. however the number of rows for each person can be different also.
    HTML Code:
    <table>
      <tr>
        <td>John Smith</td>
        <td>One, Two</td>
      </tr>
      <tr>
        <td>Jess Green</td>
        <td>Three, Four</td>
      </tr>
    </table>

  6. #6
    Lively Member
    Join Date
    Dec 2007
    Posts
    76

    Re: SQL Syntax

    I used MS Access 2000.
    Code:
    Table1
    ID   Name
    1   John Smith
    2   Jess Green
    3   Dorian Gray
    Code:
    Table2
    idName 	in_words
    1       One
    1       Two
    2       Three
    2       Four
    3       Five
    3       Six
    3       Seven
    This query is slow, but it works.
    Code:
    TRANSFORM First(SQL.in_words) AS FD
    SELECT SQL.Name
    FROM
        [SELECT T1.Name, Count(*) AS SN, T22.in_words
            FROM (Table2 AS T21
            INNER JOIN Table2 AS T22
            ON (T21.in_words<=T22.in_words) AND (T21.idName=T22.idName))
            INNER JOIN Table1 AS T1
            ON T1.ID=T22.idName
            GROUP BY T1.Name, T22.in_words].
        AS SQL
    GROUP BY SQL.Name
    PIVOT SQL.SN
    Last edited by klen_; Jan 30th, 2010 at 12:13 PM.

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