Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default SQL Distinct

    I want to select distinct value from one field of the table and select rest of
    the corresponding fields accordingly. I have a table that has fields:
    Fname, Lname, Address, Postal, Phone

    I want to select all the records with distinct address field. I tried select
    distinct(address), fname, lname, postal, phone from tblmaster. It didnt work:(

    I want distinctness only based upon the address field. Rest of the fields
    should populate accordingly.

    Thank you

    DDhillon Guest

  2. Similar Questions and Discussions

    1. SUM (DISTINCT) INNER JOIN
      I have two tables recording the sales transactions with purchase_id the common key Table 1 (Recording total amounts and customer info) ...
    2. Using DISTINCT for some but not others in 1 query
      I feel really stupid. That doesn't happen too often.. but I feel really stupid. SQL is completely kicking my face in right now. So to get...
    3. need help on DISTINCT
      Hi ALl, I need some help on DISTINCT in a query I'm trying to run. I have two tables which are linked. This is the query I use: SQL = "SELECT...
    4. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
      Following is my stored procedure. If I take the DISTINCT out then everything works fine. BUT I need the distinct because it returns duplicate...
    5. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items mustappear in the select list if SELECT DISTINCT is specified.
      Dan, You should be able to do this: SELECT Id, FaxID, ReceivedTime, Pages FROM ( SELECT DISTINCT .Id AS Id,
  3. #2

    Default Re: SQL Distinct

    I want distinctness only based upon the address field. Rest of the fields
    should populate accordingly.
    I'm not sure what you mean by "distinctness Based on the address field". Does
    the table contain more than one row with the same: Fname, Lname, Address,
    Postal, Phone? For example

    [Record 1] Jack, Spry, 80 Sycamore Street, 01234, 987654321
    [Record 2] Jack, Spry, 80 Sycamore Street, 01234, 987654321

    If so, you could use a group by clause:

    -- not tested
    SELECT fname, lname, postal, phone, count(*)
    FROM tblmaster
    GROUP BY fname, lname, postal, phone





    mxstu Guest

  4. #3

    Default Re: SQL Distinct

    Does the table contain more than one row with the same: Fname, Lname, Address,
    Postal, Phone?

    Table does contain more than one row with same address. I want all the records
    with distinct address. Say if I have 5 records in table two of them have same
    address. So I have 4 records with distinct address and I want to populate all 4
    of them with their respective fields.

    Davinder

    DDhillon Guest

  5. #4

    Default Re: SQL Distinct

    Does the table contain more than one row with the same: Fname, Lname, Address,
    Postal, Phone?

    Table does contain more than one row with same address. I want all the records
    with distinct address. Say if I have 5 records in table two of them have same
    address. So I have 4 records with distinct address and I want to populate all 4
    of them with their respective fields.

    Davinder

    DDhillon Guest

  6. #5

    Default Re: SQL Distinct

    If I'm understanding you correctly, if your table contains these two records

    [Record 1] Jack, Spry, 80 Sycamore Street, 01234, 987654321
    [Record 2] Jack, Spry, 80 Sycamore Street, 01234, 987654321
    [Record 3] Alan, Muller, 80 Sycamore Street, 01234, 987654321

    The results you want are just:

    Jack, Spry, 80 Sycamore Street, 01234, 987654321
    Alan, Muller, 80 Sycamore Street, 01234, 987654321


    In which case, the SELECT statement I posted should do that.



    mxstu Guest

  7. #6

    Default Re: SQL Distinct

    SELECT fname, lname, postal, phone, count(*)
    FROM tblmaster
    GROUP BY fname, lname, postal, phone

    This sql statement doesnt contain records from address field. I want to
    populate the results with address as well.


    [Record 1] Dave, Spry, 80 Sycamore Street, 2233, 983454321
    [Record 2] Jack, Spry, 80 Sycamore Street, 01234, 987654321
    [Record 3] Alan, Muller, 34 58th Avenue, 01234, 985654321
    [Record 4] John, Miller, 1178 154 Street, 01234, 985654321
    [Record 5] Alan, Muller, 3456 Mc Bride Blvd, 01234, 985654321

    The results I want is:

    Dave, Spry, 80 Sycamore Street, 2233, 983454321
    Alan, Muller, 34 58th Avenue, 01234, 985654321
    John, Miller, 1178 154 Street, 01234, 985654321
    Alan, Muller, 3456 Mc Bride Blvd, 01234, 985654321

    I hope I m clear now.




    DDhillon Guest

  8. #7

    Default Re: SQL Distinct

    Apologies for the confusion. I accidentally omitted the address column when
    posting the SQL statement. It should of course be:

    SELECT fname, lname, Address, postal, phone, count(*)
    FROM tblmaster
    GROUP BY fname, lname, Address, postal, phone

    Is this a typo:

    [Record 2] Jack, Spry, 80 Sycamore Street, 01234, 987654321



    mxstu Guest

  9. #8

    Default Re: SQL Distinct

    Distinct does not apply to one column. Distinct applies to the set of
    columns in the query. Most people fall into this trap. If you want the
    unique ADDRESSes then you need to start with a Group By Address. If add
    another column to the Group By then uniqueness is determined by the TWO
    columns. It sounds like you want to take the answer set from that first
    query (Group By Address) and join it back to the base table to get the
    corresponding data ... you may need to work around those Addresses that have
    multiple rows. hth

    -brian

    "DDhillon" <webforumsuser@macromedia.com> wrote in message
    news:dc0rho$jhh$1@forums.macromedia.com...
    > SELECT fname, lname, postal, phone, count(*)
    > FROM tblmaster
    > GROUP BY fname, lname, postal, phone
    >
    > This sql statement doesnt contain records from address field. I want to
    > populate the results with address as well.
    >
    >
    > [Record 1] Dave, Spry, 80 Sycamore Street, 2233, 983454321
    > [Record 2] Jack, Spry, 80 Sycamore Street, 01234, 987654321
    > [Record 3] Alan, Muller, 34 58th Avenue, 01234, 985654321
    > [Record 4] John, Miller, 1178 154 Street, 01234, 985654321
    > [Record 5] Alan, Muller, 3456 Mc Bride Blvd, 01234, 985654321
    >
    > The results I want is:
    >
    > Dave, Spry, 80 Sycamore Street, 2233, 983454321
    > Alan, Muller, 34 58th Avenue, 01234, 985654321
    > John, Miller, 1178 154 Street, 01234, 985654321
    > Alan, Muller, 3456 Mc Bride Blvd, 01234, 985654321
    >
    > I hope I m clear now.
    >
    >
    >
    >

    Brian Hogue Guest

  10. #9

    Default Re: SQL Distinct

    Originally posted by: DDhillon
    [Record 1] Dave, Spry, 80 Sycamore Street, 2233, 983454321
    [Record 2] Jack, Spry, 80 Sycamore Street, 01234, 987654321
    ....
    The results I want is:

    [Record 1] Dave, Spry, 80 Sycamore Street, 2233, 983454321
    ....


    I think I see the problem now. I was thinking of DISTINCT in the strict sense
    (as Brian described it).

    So if an address has more than one record associated with it, you only want to
    include the first one, right? One method is to JOIN with an aggregate
    sub-query that returns only one record per address:

    -- assumes table has a unique numeric id column
    SELECT m.fname, m.lname, m.postal, m.phone, m.address
    FROM tblmaster m INNER JOIN
    ( SELECT Address, MIN(numericIDColumn) AS numericIDColumn
    FROM tblmaster
    GROUP BY Address
    ) AS d ON m.numericIDColumn = d.numericIDColumn



    mxstu Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139